FFmpeg - C - 编码视频 - 设置宽高比

时间:2013-08-22 09:17:37

标签: c ffmpeg video-encoding libavcodec libavformat

我正在从mp2解码视频并将其编码为mp4。

原始文件:

Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p, 720x576 [SAR 64:45 DAR 16:9]

生成的文件:

Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 720x576 [SAR 1:1 DAR 5:4]

如您所见,分辨率没有改变,但宽高比有。

我的问题是,如何设置这些值(SAR和/或DAR)?

2 个答案:

答案 0 :(得分:1)

要设置输出宽高比,可以使用`-aspect'选项,参见ffmpeg documentation

-aspect[:stream_specifier] aspect (output,per-stream)’

    Set the video display aspect ratio specified by aspect.

    aspect can be a floating point number string, or a string of the form num:den, where num and den are the numerator and denominator of the aspect ratio. For example "4:3", "16:9", "1.3333", and "1.7777" are valid argument values.

    If used together with ‘-vcodec copy’, it will affect the aspect ratio stored at container level, but not the aspect ratio stored in encoded frames, if it exists.

答案 1 :(得分:0)

您可以在尝试添加新视频流进行编码时设置宽高比。在使用avformat_new_stream()添加新流之后。就像这样。

AVOutputFormat *outfmt = NULL;
AVStream  *out_vid_strm;
AVCodec *out_vid_codec;
outformat = avformat_alloc_context();
if(outformat)
{
    PRINT_MSG("Got Output context ")
        outformat->oformat = outfmt;
    _snprintf(outformat->filename, sizeof(outformat->filename), "%s", (const char*)outfile);
    if(outfmt->video_codec != AV_CODEC_ID_NONE)
    {
        out_vid_codec = avcodec_find_encoder(outfmt->video_codec);
        if(NULL == out_vid_codec)
        {
            PRINT_MSG("Could Not Find Vid Encoder")
        }
        else
        {
            PRINT_MSG("Found Out Vid Encoder ")
                out_vid_strm = avformat_new_stream(outformat, out_vid_codec);
            if(NULL == out_vid_strm)
            {
                PRINT_MSG("Failed to Allocate Output Vid Strm ")
            }
            else
            {
                out_vid_strm->sample_aspect_ratio.den = 1;
                out_vid_strm->sample_aspect_ratio.num = 1;
                out_vid_strm->time_base.num = in_vid_strm->time_base.num;
                out_vid_strm->time_base.den = in_vid_strm->time_base.den;
                out_vid_strm->r_frame_rate.den = in_vid_strm->r_frame_rate.den;
                out_vid_strm->r_frame_rate.num = in_vid_strm->r_frame_rate.num;
            }
        }
    }
}