Video with Linux

Mixing Video Formats in Cinelerra

Suppose source files with both 4:3 and 16:9 aspect ratios and different interlacing orders are to be used together in the same Cinelerra project. One way of proceding is to deinterlace everything, crop, rescale and produce a final project containing only deinterlaced progressive frames. However, to retain the full motion and quality of the original sources the final project should be interlaced as well. See About Interlaced Video for more information. To make it easier to use these different video files in the same project, we convert each one to have the same aspect ratio and interlacing order before editing.

For definiteness let's assume the final video will have a 16:9 aspect ratio with bottom field first interlacing order. The case where the final video will have have 4:3 aspect ratio is simpler and not treated here. We consider three types of conversions.

   16:9 top field first     ->  16:9 bottom field first
    4:3 top field first     ->  16:9 bottom field first
    4:3 bottom field first  ->  16:9 bottom field first
The first of these conversions can be done with a simple shift of the entire video frame down by one scan line. The other two of these conversions involve cropping and rescaling the video in the vertical direction. To avoid temporal aliasing when rescaling in the vertical direction one must first separate each interlaced frame into two fields. We do this using a frame-rate doubling motion compensating deinterlacer. This preserves the best resolution. Then we rescale and reinterlace.

Needed Software

This workflow relies only on open source software. Note that the -interlace option is not present in standard versions of ffmpeg and you will need my patchs discussed in DVD complaint mpeg2 on Linux to use it.

Only Change the Interlacing Order

Assume the input src.avi is 720x480 top field first widescreen video. The following commands convert it to 720x480 bottom field first widescreen video.
ffmpeg -i src.avi -f wav -y src.wav
ffmpeg -i src.avi -f yuv4mpegpipe -pix_fmt yuv411p -y /dev/stdout |
    yuvcorrect -T PROGRESSIVE 2>/dev/null |
    y4mscaler -v 0 -I active=720x479+0+0 \
         -O active=720x479+0+1 -O size=720x480 |
    yuvcorrect -T INTERLACED_BOTTOM_FIRST 2>/dev/null |
    ffmpeg -f yuv4mpegpipe -i /dev/stdin -i src.wav \
        -interlace 1 -ildct -ilme -top 0 \
        -vcodec dvvideo -acodec pcm_s16le \
        -y src-new.avi
This relatively cheap way of switching the interlace order will leave a black line at the top of the image. This line will not be noticable as it will disappear in the overscan of most televisions. If it bothers you, there is another way to switch the interlace order that swaps fields between adjacent frames. That method does not leave a blank line at the top, but is more complicated in that it also requires shifting the audio by half a frame. It will not be discussed here.

Convert 4:3 to Widescreen

Assume the input src.avi is 720x480 top or bottom field first 4:3 aspect ratio video. The following commands convert it to 720x480 bottom field first widescreen video. If the input interlacing order is top field first set
$ iorder=INTERLACED_TOP_FIRST
otherwise, if the input video is bottom field first set
$ iorder=INTERLACED_BOTTOM_FIRST
Now enter the following commands
$ ffmpeg -i src.avi -f wav -y src.wav
$ ffmpeg -i src.avi -f yuv4mpegpipe -pix_fmt yuv411p -y /dev/stdout |
    yuvcorrect -T $iorder 2>/dev/null |
    yuvdeinterlace -d |
    yuvfps -s 30000:1001 -r 30000:1001 |
    yuvcorrect -T PROGRESSIVE 2>/dev/null |
    y4mscaler -v 0 -I sar=NTSC -O sar=NTSC_WIDE \
        -O size=720x480 -I active=720x360+0+60 |
    yuvfps -s 60000:1001 -r 60000:1001 |
    y4minterlace -ib |
    ffmpeg -f yuv4mpegpipe -i /dev/stdin -i src.wav \
        -interlace 1 -ildct -ilme -top 0 \
        -vcodec dvvideo -acodec pcm_s16le \
        -y src-new.avi
We use the same sequence of commands for converting top field first and bottom field first source files. After deinterlacing the image is rescaled so that the original scan lines are no longer strictly supported by one field or the other. Therefore it makes no quality difference whether the original source was top field first or bottom field first.

Conclusions

Converting all the source files to have the same aspect ratio and interlace order makes it is possible to easily edit them in Cinelerra. The conversion is slow, but preserves as much of the motion and quality of the original source as possible.
Last Updated: Fri Jul 17 23:05:45 PDT 2009