Correct aspect ratio without re-encoding video file

18

4

I have a video stream with the following properties:

Stream #0:0: Video: mpeg4 (Advanced Simple Profile) (XVID / 0x44495658), yuv420p, 720x416 [SAR 1:1 DAR 45:26], 1908 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc

When I run it in VLC, I have to press "A" to change aspect ratio to "4:3" to make the video show with the correct aspect ratio.

Looking at the video facts, Is the error that a) the actual video has been incorrectly stretched in the pixel data, or b) there is simply some metadata value that has been incorrectly set?

If the former, I know I can re-encode the video and change the width and height. But if the latter, what ffmpeg command to I run to fix the metadata without re-encoding the video itself?

forthrin

Posted 2015-04-30T06:24:03.627

Reputation: 1 047

Answers

35

There is a difference between Sample Aspect Ratio (SAR) and Display Aspect Ratio (DAR). If you want to change the video to display at 4:3, you will either need to change the actual pixels in the image (by scaling the pixels and changing SAR), or by setting a metadata flag that at the container level that tells external media players to stretch the image to your desired DAR.

You will not be able to scale the pixels and change SAR without applying a video filter. If you choose this method, you will be required to transcode the file - since you cannot "stream copy" the video stream while applying a video filter.

To scale the image and change SAR (while transcoding), try:

ffmpeg -i <INPUT_FILE> -vf scale=720:540 -c:v <Video_Codec> <OUTPUT_FILE>

On the other hand, if you just want to change the metadata flag and adjust the DAR, you will be able to stream copy the video. To do this, try:

ffmpeg -i <INPUT_FILE> -aspect 720:540 -c copy [OUTPUT_FILE]

occvtech

Posted 2015-04-30T06:24:03.627

Reputation: 980

Excellent! Changing the aspect worked straight away! – forthrin – 2015-04-30T18:04:53.890

1You can't use -c copy and scale at the same time; however you can use -aspect to change the aspect ratio at the container level (but not the stream level). – llogan – 2015-04-30T18:56:20.390

@LordNeckbeard, using -aspect does work for playback in ffplay, but not in WMP or MPC-HC. It adds an additional SAR/DAR item to the Stream #… info line in FFmpeg/probe/play but doesn't seem to be solution for general playback. Stretching the video with the window seems like the easiest solution for MPC-HC playback. – Lumi – 2016-01-03T14:10:59.860

didn't work for me for MPEG2 without reencoding – Mikhail V – 2016-07-01T11:33:25.020

I don't know why it didn't work for me. It just outputs the same video! – Tina J – 2016-07-22T01:02:08.707

As was stated above, you can't just copy the stream and expect it to work (everywhere), you're going to have to re-encode, e.g.: ffmpeg -strict -2 -i [INPUT] -aspect 720:540 -strict -2 [OUTPUT]. I added -strict -2 (note that's -2, not just 2) to get ffmpeg to stop complaining about the aac audio in the original. This re-encoded a square source into an output at 4:3. – Andrew Beals – 2017-05-20T10:47:32.657

Unfortunately, did not work for AVI container 1280x720 [SAR 12:11 DAR 64:33]… – AnrDaemon – 2018-09-09T20:20:49.547

4

Delgado's answer is correct that MP4Box can do this, but the -par option doesn't work quite as described. With an -out parameter (so as not to disturb your original file):

mp4box source.mp4 -out target.mp4 -par stream-number=width:height

When you use -par stream-number=width:height, you define the pixel aspect ratio – that is, the result of dividing the device aspect ratio by the storage aspect ratio. (Equivalently, you're describing the aspect ratio of a source pixel.) For example, suppose you have a DVD source that's 720×480, and the correct display aspect ratio is 4:3. For this case, you need:

mp4box source.mp4 -out target.mp4 -par 1=8:9

because (4/3) / (720/480) = 8/9.

If the source represents true SD NTSC pixels (in which case only the central 704×480 pixels are supposed to map to a 4×3 screen, with 8 pixels overscan on either side), the correct command would be:

mp4box source.mp4 -out target.mp4 -par 1=10:11

because (4/3) / (704/480) = 10/11 – exactly the reference pixel aspect ratio for standard definition NTSC video.

For the case given in the question, if it's really 4:3, that gives a very odd pixel aspect ratio: (4/3)/(720/416) = 104/135. It's 720 wide, which suggests a DVD source; it's a 25 fps video, suggesting PAL, but the PAR works out to less than 1, suggesting NTSC. It could be 4:5, I suppose (very close to 104:135), but I don't know of anything that produces that pixel aspect ratio; maybe try that first, and then try 3:4 if it still looks a little too stretched horizontally. If you're certain it's exactly 4:3, of course, just use 104:135.

Coises

Posted 2015-04-30T06:24:03.627

Reputation: 41

3

ffmpeg can't change parameters of a video stream without re-encoding, MP4Box (part of gpac) and mkvmerge can. In case of one video stream and a real/correct aspect ratio of 4:3, you may want to try:

MP4Box -par 1=3:4 VideoFile.mp4

    "-par" : PixelAspectRatio (adjusts DAR + SAR with respect to the video resolution)
    "1"=   : stream number
    "3:4"  : aspect ratio (lower number 1st!)
    Changes are directly applied to "VideoFile.mp4", no copy

To verify before and after: ffmpeg -i VideoFile.mp4

Delgado

Posted 2015-04-30T06:24:03.627

Reputation: 39

0

Changing the SAR without reencoding also works with ffmpeg on .mp4 using the h264_metadata as Gyan pointed out here:

ffmpeg -i in.mp4 -c copy -bsf:v "h264_metadata=sample_aspect_ratio=4/3" out.mp4

drake7

Posted 2015-04-30T06:24:03.627

Reputation: 1

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

– MMM – 2020-02-28T11:15:12.633