如何使用ffmpeg对视频进行编码以便在Android上播放?

时间:2012-01-02 08:21:39

标签: android ffmpeg

我有一个c ++库,可以实时编码视频,从网络摄像头到mp4文件(H264)。我得到的设置如下:

    codecContex->profile=FF_PROFILE_H264_BASELINE; //Baseline
    codecContex->gop_size=250;
    codecContex->max_b_frames=0;
    codecContex->max_qdiff=4;
    codecContex->me_method=libffmpeg::ME_HEX;
    codecContex->me_range=16;
    codecContex->qmin=10;
    codecContex->qmax=51;
    codecContex->qcompress=0.6;
    codecContex->keyint_min=10;
    codecContex->trellis=0;
    codecContex->level=13; //Level 1.3
    codecContex->weighted_p_pred = 2;
    codecContex->flags2|=CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT;

这会创建在iOS设备和Windows Phone 7设备上播放但在Android设备上播放的MP4文件。我读过Android只支持使用基线配置文件编码的电影。这些设置应该会产生一个基线电影但是当我用MediaInfo查看生成的MP4文件时,它说它是AVC(High@L1.3)。这可能是它无法正常工作的原因,但我似乎无法用AVC生成东西(Baseline@L1.3)......

如果我删除最后一行:

codecContex->flags2|=CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT;

然后MediaInfo将文件报告为“AVC(Main@L1.3)” - 但这些标志是Baseline配置文件的一部分!

1 个答案:

答案 0 :(得分:2)

我通过以下选项获得了基线编码:

    codecContex->coder_type = 0;
    codecContex->flags|=CODEC_FLAG_LOOP_FILTER;
    codecContex->profile=FF_PROFILE_H264_BASELINE; //Baseline
    codecContex->scenechange_threshold = 40; 
    codecContex->gop_size=250;
    codecContex->max_b_frames=0;
    codecContex->max_qdiff=4;
    codecContex->me_method=7;
    codecContex->me_range=16;
    codecContex->me_cmp|= 1;
    codecContex->me_subpel_quality = 6; 
    codecContex->qmin=10;
    codecContex->qmax=51;
    codecContex->qcompress=0.6;
    codecContex->keyint_min=25;
    codecContex->trellis=0;
    codecContex->level=13; //Level 1.3
    codecContex->refs = 1;
    codecContex->weighted_p_pred = 0;
    codecContex->crf = 20.0f;
    codecContex->flags2|=CODEC_FLAG2_BPYRAMID-CODEC_FLAG2_WPRED-CODEC_FLAG2_8X8DCT;

...视频不会在HTML5视频元素中播放,但如果您直接在浏览器中加载mp4文件。电影的宽度也必须是480像素或更低。