【问题标题】:FFmpeg - C - Encoding video - Set aspect ratioFFmpeg - C - 编码视频 - 设置纵横比
【发布时间】:2023-03-19 01:18:02
【问题描述】:

我正在从 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)?

【问题讨论】:

    标签: c ffmpeg video-encoding libavcodec libavformat


    【解决方案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.
    

    【讨论】:

    • 这仅与命令行 FFmpeg 有关,我正在使用 C。
    • Oups,是的,我看到了标签,但问题本身并不清楚您需要使用 ffmpeg 库的解决方案......也许添加用于初始化编码器的代码,并添加libav、libavcodec、libavformat 的标签......
    【解决方案2】:

    您可以在尝试添加新的视频流进行编码时设置纵横比。就在使用 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;
                }
            }
        }
    }
    

    【讨论】:

    • 代码的哪一部分设置了显示纵横比?这是在您的 sn-p 中设置其他变量的一部分自动完成的吗?
    • out_vid_strm->sample_aspect_ratio.den = 1;out_vid_strm->sample_aspect_ratio.num = 1。这应该设置纵横比。
    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2016-04-22
    • 2017-09-19
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多