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
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.)
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 720x480In 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
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
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.)
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.
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 -j option to change the size of the input video before the zoom is performed. For example, by clipping 8 pixels from the left and right edges of the video:
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.
When you can't keep the same DAR
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