小编Ale*_*904的帖子

如何使用从CMSampleBufferRef(AVFoundation)获得的数据填充音频AVFrame(ffmpeg)?

我正在编写用于将实时音频和视频从webcamera流式传输到rtmp-server的程序.我在MacOS X 10.8中工作,因此我使用AVFoundation框架从输入设备获取音频和视频帧.这个框架进入代表:

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer: (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection ,

其中sampleBuffer包含音频或视频数据.

当我收到音频数据时sampleBuffer,我试图将这些数据转换为libavcodec AVFrame并用其编码AVFrame:

    aframe = avcodec_alloc_frame();  //AVFrame *aframe;
    int got_packet, ret;
    CMItemCount numSamples = CMSampleBufferGetNumSamples(sampleBuffer); //CMSampleBufferRef

    NSUInteger channelIndex = 0;

    CMBlockBufferRef audioBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);

    size_t audioBlockBufferOffset = (channelIndex * numSamples * sizeof(SInt16));

    size_t lengthAtOffset = 0;

    size_t totalLength = 0;

    SInt16 *samples = NULL;

    CMBlockBufferGetDataPointer(audioBlockBuffer, audioBlockBufferOffset, &lengthAtOffset, &totalLength, (char **)(&samples));

            const AudioStreamBasicDescription *audioDescription = CMAudioFormatDescriptionGetStreamBasicDescription(CMSampleBufferGetFormatDescription(sampleBuffer));

    aframe->nb_samples =(int) numSamples;

    aframe->channels=audioDescription->mChannelsPerFrame;

    aframe->sample_rate=(int)audioDescription->mSampleRate;

     //my webCamera configured to …
Run Code Online (Sandbox Code Playgroud)

ffmpeg avcodec cmsamplebuffer

6
推荐指数
0
解决办法
5911
查看次数

当输入pcm样本计数不等于1024时,如何使用ffmpeg-API将重采样的PCM音频编码为AAC

我正在努力捕获并将音频流传输到RTMP服务器.我在MacOS下工作(在Xcode中),所以为了捕获音频样本缓冲区我使用AVFoundation-framework.但是对于编码和流媒体,我需要使用ffmpeg-API和libfaac编码器.因此输出格式必须是AAC(用于支持iOS设备上的流播放).

我遇到了这样的问题:音频捕获设备(在我的案例中是罗技相机)为我提供了512个LPCM样本的样本缓冲区,我可以选择16000,24000,36000或48000 Hz的输入采样率.当我将这512个样本提供给AAC编码器(配置为适当的采样率)时,我听到一个缓慢而抽搐的音频(在每帧之后看起来像是沉默的骰子).

我想通了(也许我错了),libfaac编码器只接受1024个样本的音频帧.当我将输入采样率设置为24000并在编码之前将输入采样缓冲区重采样为48000时,我获得1024个重采样样本.将这些1024个样本编码到AAC后,我听到输出声音正确.但是,当输出采样率必须为48000 Hz时,我的网络摄像头会在缓冲区中为任何输入采样率生成512个样本.所以我需要在任何情况下进行重采样,重新采样后我不会在缓冲区中获得1024个样本.

有没有办法在ffmpeg-API功能中解决这个问题

我将不胜感激任何帮助.

PS:我想我可以累积重采样缓冲区,直到样本数变为1024,然后对其进行编码,但这是流,因此会产生时间戳和其他输入设备的麻烦,并且这种解决方案不合适.

当前问题出自[问题]中描述的问题:如何使用从CMSampleBufferRef(AVFoundation)获得的数据填充音频AVFrame(ffmpeg)?

这是一个带有音频编解码器配置的代码(还有视频流,但视频工作正常):

    /*global variables*/
    static AVFrame *aframe;
    static AVFrame *frame;
    AVOutputFormat *fmt; 
    AVFormatContext *oc; 
    AVStream *audio_st, *video_st;
Init ()
{
    AVCodec *audio_codec, *video_codec;
    int ret;

    avcodec_register_all();  
    av_register_all();
    avformat_network_init();
    avformat_alloc_output_context2(&oc, NULL, "flv", filename);
    fmt = oc->oformat;
    oc->oformat->video_codec = AV_CODEC_ID_H264;
    oc->oformat->audio_codec = AV_CODEC_ID_AAC;
    video_st = NULL;
    audio_st = NULL;
    if (fmt->video_codec != AV_CODEC_ID_NONE) 
      { //…  /*init video codec*/}
    if (fmt->audio_codec != AV_CODEC_ID_NONE) {
    audio_codec= avcodec_find_encoder(fmt->audio_codec);

    if (!(audio_codec)) {
        fprintf(stderr, "Could …
Run Code Online (Sandbox Code Playgroud)

ffmpeg aac resampling libavcodec libfaac

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

ffmpeg ×2

aac ×1

avcodec ×1

cmsamplebuffer ×1

libavcodec ×1

libfaac ×1

resampling ×1