Knossos
Knossos

Reputation: 16038

FFmpeg - C - Encoding video - Set aspect ratio

I am decoding a video from mp2 and encoding to mp4.

The original file:

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

The resulting file:

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

As you can see, the resolution has not changed, but the aspect ratio has.

My question, is how can I set these values (SAR and/or DAR)?

Upvotes: 1

Views: 5158

Answers (2)

praks411
praks411

Reputation: 1992

You can set aspect ratio when trying to add new video stream for encoding. Just after adding new stream using avformat_new_stream().Something like this.

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;
            }
        }
    }
}

Upvotes: 0

alexbuisson
alexbuisson

Reputation: 8469

to set the output aspect ratio, you can use the `-aspect' option, see 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.

Upvotes: 1

Related Questions