我现在录了麦克风的音频的Byte,根据网上说的是PCMA格式
然后我想转成AAC格式后RTP到服务器
现在PCMA格式的RTP到服务器,服务器也能接收到音频了
但现在要求是AAC格式,
我网上找的转只有FFMPEG,也是C的,没有学过C正在开始学
然后找了个SharpFFmpeg,不知道这个能不能把我的实时PCMA流,转为AAC流谢谢
然后我想转成AAC格式后RTP到服务器
现在PCMA格式的RTP到服务器,服务器也能接收到音频了
但现在要求是AAC格式,
我网上找的转只有FFMPEG,也是C的,没有学过C正在开始学
然后找了个SharpFFmpeg,不知道这个能不能把我的实时PCMA流,转为AAC流谢谢
FFmpeg 类中没有发现有音频编码返回byte[]的方法要怎么实现呢?难道C#就没有办法实现从PCM转到AAC吗?
这个是C++的
但我的是C#的,求解
谢谢
这是一个C#的,但是下载不下来(注册没反应),而且好像是要钱的
{
public const int FF_PROFILE_AAC_MAIN = 0;
public const int FF_PROFILE_AAC_LOW = 1;
public const int FF_PROFILE_AAC_SSR = 2;
public const int FF_PROFILE_AAC_LTP = 3;
public const int SAMPLE_SIZE = 2; private int _frameBytes;
private IntPtr _pInputBuffer; private IntPtr _pEncoderOutput;
private int _allocOutputSize;
private IntPtr _pCodecContext; public AACEncoder(int sampleRate, int channels, int audioBitrate)
{
FFmpeg.avcodec_register_all(); //Set up audio encoder
var pCodec = FFmpeg.avcodec_find_encoder(FFmpeg.CodecID.CODEC_ID_AAC);
if (pCodec == IntPtr.Zero)
throw new Exception("avcodec_find_encoder: ERROR\n"); _pCodecContext = FFmpeg.avcodec_alloc_context();
var context = (FFmpeg.AVCodecContext)Marshal.PtrToStructure(_pCodecContext, typeof(FFmpeg.AVCodecContext));
context.bit_rate = audioBitrate;
context.sample_fmt = FFmpeg.SampleFormat.SAMPLE_FMT_S16;
context.sample_rate = sampleRate;
context.channels = channels;
context.profile = FF_PROFILE_AAC_MAIN;
context.codec_type = FFmpeg.CodecType.CODEC_TYPE_AUDIO;
Marshal.StructureToPtr(context, _pCodecContext, false);
if (FFmpeg.avcodec_open(_pCodecContext, pCodec) < 0)
throw new Exception("avcodec_open: ERROR\n"); _allocOutputSize = FFmpeg.FF_MIN_BUFFER_SIZE * 10;
_pEncoderOutput = Marshal.AllocHGlobal(_allocOutputSize); if (_pEncoderOutput == IntPtr.Zero)
throw new Exception(string.Format("encoderOutput: malloc Error size({0})", _allocOutputSize)); context = (FFmpeg.AVCodecContext)Marshal.PtrToStructure(_pCodecContext, typeof(FFmpeg.AVCodecContext));
_frameBytes = context.frame_size * context.channels * SAMPLE_SIZE;
_pInputBuffer = Marshal.AllocHGlobal(_frameBytes);
}
public void Dispose()
{
Marshal.FreeHGlobal(_pInputBuffer);
Marshal.FreeHGlobal(_pEncoderOutput);
} public byte[] Encode(byte[] rawData, int rawDataSize)
{
int p = 0, encoderOutputSize = 0; // Note: revisit the while loop
// BUG-1 这里有个bug,输入音频的帧长度必须是1024
while (rawDataSize >= _frameBytes)
{
Marshal.Copy(rawData, p, _pInputBuffer, _frameBytes);
int packetSize = FFmpeg.avcodec_encode_audio(_pCodecContext, _pEncoderOutput, _allocOutputSize,_pInputBuffer);
encoderOutputSize += packetSize;
p += _frameBytes;
rawDataSize -= _frameBytes;
}
var result = new byte[encoderOutputSize];
Marshal.Copy(_pEncoderOutput, result, 0, encoderOutputSize);
return result;
}
}
ps. 那个StreamCoder的试用版可以下载,没有问题。
现在我是一步步来实现
谢谢