[Home]Calculating Frame Size And Aspect Ratio

(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 (the DVD player will play it back with a PAR of 8:9 to compensate for the frame size):

    --export_asr 2 -Z 720x480
However, 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:

    height = 576,  width = (DAR * height) / PAR = (16/9 * 576) / ( 1/1) = 1024 (1024x576)
     width = 704, 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:

    height = 576,  width = (DAR * height) / PAR = (16/9 * 576) / ( 1/1) = 1024 (1024x576)
     width = 720, 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. This will, of course, result in a slight change to the DAR, making it 16:8.98 or 16:9.02 respectively; if this is a problem, you'll have to choose a different horizontal size, such as 704x396 as in the previous example. (This will be slower, since transcode will have to zoom both horizontally and vertically.)

When you can't keep the same DAR

Example 5.
Return to example 2, where an AVI video file was transcoded to MPEG. Now suppose that the AVI file is 672x272 with a PAR of 1:1, giving a DAR of 42:17. DVD players do not support this aspect ratio, and you cannot simply keep the same video frame size because DVD players require a fixed size (720x480, 704x480, 352x480, or 352x240 for NTSC). Worse, DVD video does not allow a PAR of 1:1. So what can you do?

Solving this requires two steps: zooming the input video to change the PAR (while keeping the DAR constant), then adding borders to change the DAR (while keeping the new PAR constant). First, let's assume that the output DVD will have a DAR of 16:9, since that is closer to the input video's 42:17 (about 2.5:1) than a DAR of 4:3. We'll also choose the largest possible DVD frame size, 720x480. From these we can see that the PAR of the DVD video stream will be:

          height * DAR   480 * (16/9)
    PAR = ------------ = ------------ = 32/27
              width          720

Next we need to determine the size to zoom the input video to. As in examples 3 and 4, we hold either the width or the height constant to come up with two possible sizes. Note that here, we use the DAR of our input video, not the DAR of the DVD, because we don't want to stretch the video.

    height = 480,  width = (DAR * height) / PAR = (42/17 * 480) / (32/27) = 1000 (1000x480)
     width = 720, height = (PAR * width ) / DAR = (32/27 * 720) / (42/17) =  346 ( 720x346)

(As in example 4, these are rounded to even numbers due to the requirements of the video codec.) Since 1000x480 is too large for DVD, we instead choose 720x346 as the zoom size.

Finally, we need to add a border to the video so that it matches the desired output frame size of 720x480. This is done with the -Y option, which is processed after -Z (as opposed to the -j option used in Example 4, which is handled before -Z and is used for clipping or adding borders to the input frame). The difference between the zoomed frame and the output frame size is 480 - 346 = 134 pixels, so we should add half of that, or 67 pixels, to the top and bottom of the frame. But again, these values must be even, so we have to use 68 and 66 instead. (This will shift the video slightly above or below the center position of the display.)

Thus our transcode options are (remembering that "3" is the aspect ratio code for 16:9):

    -Z 720x346 -Y -68,0,-66,0 --export_asr 3

Example 6.
Sometimes you explicitly want to (or have to) change the DAR. DVD players, for example, do this when showing a widescreen (16:9) DVD on a standard-definition (4:3) television. Naturally, you don't want to stretch the video; instead, you can add borders to or cut the edges off of the input video so that the output frame is of the right size.

Suppose you have a 16:9 DVD containing a 720x576 MPEG2 stream as in Example 4, and you want to display it on a device that has a 640x480 resolution with a PAR of 1:1 (and therefore a DAR of 4:3). As in Example 5, the calculation consists of two steps: zooming the frame to match the new PAR, then clipping or adding borders to match the new DAR.

Let's assume that we want to fit the entire video frame onto the display without clipping (so-called "letterboxing"). Using our width/height formulas from above, we find:

    height = 480,  width = (DAR * height) / PAR = (16/9 * 480) / ( 1/1) = 854 (854x480)
     width = 640, height = (PAR * width ) / DAR = ( 1/1 * 640) / (16/9) = 360 (640x360)

The first of these will not fit on the display device, so our desired zoom size is the second, 640x360. Now we can add a border to the top and bottom, of (480 - 360) / 2 = 60 pixels each, giving us our final 640x480, 4:3 video frame while retaining the original 16:9 input video in the center of the frame:

    -Z 640x360 -Y -60,0,-60,0

Transcode Wiki | Recent Changes | Preferences
Password required to edit | View other revisions
Last edited February 4, 2007 4:52 am by Achurch (diff)