ffmpeg支持G711音频和H.264视频数据同步封装进MP4文件

1、编译参数 
在使用./configure编译ffmpeg库时,需要添加一些编译选项支持项目所需要的功能。具体参数如下:

--enable-encoder=pcm_alaw --enable-decoder=pcm_alaw //支持PCM和G711编解码
--enable-demuxer=mov  //支持mov,mp4,m4a,3gp,3g2,mj2解复用

2、代码模块 
ffmpeg本身是支持H264+AAC的mp4封装格式的,但是并不支持H264+G711的mp4封装格式。 
ffmpeg中对MP4的打包处理是在movenc.c中,为了能够使ffmpeg支持H264+G711的mp4封装格式,同时也可以支持音频原始数据封装,修改libavformat/movenc.c代码以下部分:

if (version == 2) {
        avio_wb16(pb, 3);
        avio_wb16(pb, 16);
        avio_wb16(pb, 0xfffe);
        avio_wb16(pb, 0);
        avio_wb32(pb, 0x00010000);
        avio_wb32(pb, 72);
        avio_wb64(pb, av_double2int(track->enc->sample_rate));
        avio_wb32(pb, track->enc->channels);
        avio_wb32(pb, 0x7F000000);
        avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
        avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
        avio_wb32(pb, track->sample_size);
        avio_wb32(pb, get_samples_per_packet(track));
    } else {
        if (track->mode == MODE_MOV) {
            avio_wb16(pb, track->enc->channels);
            if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
                track->enc->codec_id == AV_CODEC_ID_PCM_S8)
                avio_wb16(pb, 8); /* bits per sample */
            else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_G726)
                avio_wb16(pb, track->enc->bits_per_coded_sample);
            else
                avio_wb16(pb, 16);
            avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
        } else { /* reserved for mp4/3gp */
            avio_wb16(pb, track->enc->channels > 0 ? 
                          track->enc->channels : 1);
            track->enc->bits_per_coded_sample = av_get_bits_per_sample(track->enc->codec_id);
            avio_wb16(pb, track->enc->bits_per_coded_sample > 0 ? 
                          track->enc->bits_per_coded_sample : 16);
            avio_wb16(pb, 0);
        }

        avio_wb16(pb, 0); /* packet size (= 0) */
        avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
                      track->enc->sample_rate : 0);
        avio_wb16(pb, 0); /* Reserved */
    }
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag;

    if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
        tag = mp4_get_codec_tag(s, track);
    else if (track->mode == MODE_ISM) {
        tag = mp4_get_codec_tag(s, track);
        if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
            tag = MKTAG('w', 'm', 'a', ' ');
    } else if (track->mode == MODE_IPOD)
        tag = ipod_get_codec_tag(s, track);
    else if (track->mode & MODE_3GP)
        tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
    else if (track->mode == MODE_F4V)
        tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);

    if (tag == 0){
        tag = mov_get_codec_tag(s, track);
    }
    return tag;
}


--------------------- 
作者:珠雨妮儿 
来源:CSDN 
原文:https://blog.csdn.net/zhuyunier/article/details/80814227 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(FFmpeg)