[Home]Calculating Frame Size And Aspect Ratio

Showing revision 2
(Note: See aspect ratio for a description of the two types of aspect ratios, DAR (display aspect ratio) and PAR (pixel aspect ratio), and how they relate to frame size. In short, DAR is the 4:3, 16:9, etc. ratio that is usually called just "aspect ratio", while PAR describes the shape of the pixels themselves.)

When transcoding video, the usual goal is to keep the DAR constant, to avoid ending up with a "stretched" or "squished" video. For example, if you have a standard-definition video with a DAR of 4:3, you want to make sure that the output video also has a DAR of 4:3. Alternatively, if your input video is an AVI with a frame size of 640x320, then its DAR is probably(*) 2:1, and you want to keep that 2:1 ratio in the output video as well.
(*) Video stored in AVI files is usually assumed to have a PAR of 1:1, so the DAR is just the ratio of the video frame's width to its height. However, some software (particularly FFmpeg and MPlayer) is capable of embedding DAR and/or PAR information within the video data, in which case this may not hold.

Once you know the DAR of your input video, the question becomes how to keep the same DAR in the output video. This depends on the video format you're transcoding into, and can be divided into three main groups:

When you can specify the DAR directly

Example 1.
Suppose you have a 640x480 AVI file, with a DAR of 4:3, that you want to convert to an MPEG video stream. As it happens, MPEG allows you to specify the DAR directly in the video stream. The DAR has to be one of a few predefined values (1:1, 4:3, 16:9, or 2.21:1), but since the input video is in fact 4:3, we can take advantage of this. The only option you would need to give to transcode is

    --export_asr 2
(Note that the video will still display correctly with most players even if you don't specify --export_asr, but that's only because the assumed PAR of 1:1 happens to be correct. See the next example.)

Example 2.
Take the same situation from Example 1, but this time assume the output MPEG video is destined to be played back on a DVD player. DVD players only support a few video frame sizes, of which 640x480 is not one, so the video will have to be resized. However, since the DAR can be stored in the MPEG file, you only need to add a -Z option to change the frame size to one of the allowed sizes, and the DAR of 4:3 will remain unchanged:

    --export_asr 2 -Z 720x480
In this case, if you omit the --export_asr option, the video will look stretched horizontally when played back on a computer. This is because the MPEG video stream has no DAR stored with it, so the player assumes a PAR of 1:1, giving a resulting DAR of 3:2, or 1.5 -- larger (wider) than the original DAR of 4:3, or 1.33.

When you can set the DAR by changing the frame size

Example 3.
Suppose you have a PAL-format DVD containing a 704x576 MPEG video stream with a DAR of 16:9, and you want to convert that to an AVI file with XviD-encoded video so it can be watched on a computer. As mentioned above, aspect ratios cannot be stored in AVI files, and most player software will assume a PAR of 1:1, so the video frame size needs to be changed to be in the same ratio as the DAR.

In order to calculate the size to use for the output video, we make use of the following relationship between aspect ratios and frame size:

    DAR   width
    --- = ------
    PAR   height

or, alternatively:

    DAR * height = PAR * width

This allows us to determine two possible output frame sizes, by keeping either the width or the height constant and filling in the aspect ratios:

    width  = (DAR * height) / PAR = (16/9 * 576) / ( 1/1) = 1024 (1024x576)
    height = (PAR * width ) / DAR = ( 1/1 * 704) / (16/9) =  396 ( 704x396)

Either of these sizes can then be used with the -Z option to create an AVI file with the proper aspect ratio. (Naturally, any other size where the height is 9/16ths of the width will work, but these two sizes can be processed more quickly since either the width or the height remains unchanged.)

Example 4.
Take the same situation from Example 3, but this time suppose the MPEG video is 720x576 instead, with the same 16:9 DAR. The width and height equations from above give us:

    width  = (DAR * height) / PAR = (16/9 * 576) / ( 1/1) = 1024 (1024x576)
    height = (PAR * width ) / DAR = ( 1/1 * 720) / (16/9) =  405 ( 720x405)

The size 1024x576 is unchanged from Example 3, since the height of the original video is unchanged, and will work just as well. Note, however, that the second size, 720x405, has an odd value (405) for the height. Trying to use this size will cause an error, because the XviD video data format does not allow an odd width or height (as is the case for most current video codecs). In order to transcode the video using a width of 720, the height would need to be adjusted to an even number, either 404 or 406.

One way to adjust the height is simply to change the size given to the -Z option. While this accomplishes the desired change with no extra processing overhead, it also makes a slight change to the DAR, so that the resulting video will look a little bit stretched or squished vertically (though the change will be imperceptible to most people).

To avoid this change in the aspect ratio, you can instead use the -Y option to clip one pixel from, or add a single-pixel black border to, the top or bottom of the frame:

    -Z 720,405 -Y 1,0,0,0   # cuts off the top pixel
    -Z 720,405 -Y 0,0,-1,0  # adds a black pixel at the bottom

The resulting video's DAR is still slightly different from 16:9, but by adding or removing a line like this, you have effectively changed the DAR of the original video as well, so the AVI file will look exactly the same as the original MPEG video (with the exception of the missing or extra pixel).

Alternatively, you can use the -j option to directly change the size of the input video before you change its size. For example, by clipping 8 pixels from the left and right edges of the video:

    -j 0,8,0,8

you can reduce the input video's frame size to 704x576, giving you exactly the same situation as Example 3 (where a direct reduction to 704x396 could be made). Since this only requires zooming the frame vertically, it is faster and introduces less distortion than zooming the original 720x576 image to 704x396.

As another example, suppose you have a 672x272 video file on your computer that you want to transcode to an NTSC (720x480) DVD. If you don't know the DAR, but you assume the intended PAR is 1:1 (standard for computer monitors), you can calculate the DAR as:

    DAR = PAR * width / height = (1/1) * 672 / 272 = 42:17
or about 2.47:1. Now, we know that the width and height must fit within the DVD frame size, so we calculate using both the width and the height:
     width = 720    height = width * PAR / DAR = 720 * (1/1) / (42/17) = 292 (291.428)
    height = 480    width = height * DAR / PAR = 480 * (42/17) / (1/1) = 1186 (1185.882)
giving us possible frame sizes of either 720x292(*) or 1186x480. The latter is obviously too big for DVD video, so we use the former, and add black bars to the top and bottom with the -Y option to transcode:
    transcode -Z 720x292 -Y -94,0 (other options)

(*) 291.428 is rounded up to 292 because the most common video image format, YUV 4:2:0, encodes pixels in 2x2 groups, so width and height values must always be even.

Note that when the --export prof, --export asr, and -Z options are used with Transcode, all geometry calculations are done automagically for you, assuming that Transcode is able to probe the input files successfully. In that case the -Z option should be set to the default for the output format, and the fast flag should always be used.
For example, to create an NTSC DVD compatible mpg you would use -Z 720x480,fast. The appropriate geometry calculations will then be done by Transcode. For some reason this only works when the fast flag is used, even if the source geometry is incompatible with the fast flag!

Transcode Wiki | Recent Changes | Preferences
Password required to edit | View other revisions | View current revision
Edited February 3, 2007 4:50 am by Achurch (diff)