Transcoding DV to Widescreen MP4 in Linux
When I export video from Cinelerra in Linux after editing, I always render as Raw DV. It takes a lot of harddrive space, but I've never gotten any other format to work in Cinelerra, especially when my finished video needs to be widescreen. Luckily, Linux has some great tools for transcoding video. One of the most popular is ffmpeg.
I'll usually render the video to DV and then run the following commands to get it into an MP4 that is widescreen:
ffmpeg -f dv -i RenderedVideo.dv -pass 1 -vcodec h264 -g 300 -mbd 2
-cmp 2 -subcmp 2 -deinterlace -aspect 16:9 -s 640x360 -padtop 4
-padbottom 4 -b 768k -acodec aac -y Final_vodcast_render.mp4
ffmpeg -f dv -i RenderedVideo.dv -pass 2 -vcodec h264 -g 300 -mbd 2
-cmp 2 -subcmp 2 -deinterlace -aspect 16:9 -s 640x360 -padtop 4
-padbottom 4 -b 768k -acodec aac -y Final_vodcast_render.mp4
MP4Box -add Final_vodcast_render.mp4 Final_vodcast.mp4
MP4Box -par 1=1:1 Final_vodcast.mp4
rm Final_vodcast_render.mp4
A little explanation may be in order.
The first line is a first pass of the video encoding. It's really there just to gather data in order to optimize the real encoding. You could throw away the final video file created by this, but I always choose to keep it around.
For a real understanding of all the options, take a look at this page that lists all the options and tries to explain them. Important ones are:
- -vcodec: The video codec we want to use. Internet standard at the moment is H.264
- -deinterlace: My camera records for standard TVs (640i). If your's does too, you'll need to deinterlace the signal for internet video
- -aspect: Need to set it to widescreen which, for my camera, is 16:9
- -s: This is the final size of the video, 640 pixels by 360 pixels
- -padtop,-padbottom: Video needs to be processed in 16x16 pixel blocks. Since 360 is not divisible by 16, I need to make sure the height is divisible by 16. Adding a 4 pixel line of black padding pixels to the top and bottom makes the height 368, which is divisible by 16
- -b: The bitrate of the video. 768k is a little big for internet video, but it keeps the quality up there for Apple TV and what not.
- -acodec: The Audio codec we want to use, AAC
- -y: Overwrite files, needed on the second run
The other options are really just things I got from other people on what to set to get this to run on an Apple TV. I felt that was important enough for my podcast to keep them in there. Blip.tv actually does a good job of converting this to an iPod friendly format, so I won't go into the nightmare that is transcoding video in Linux for the iPod. I actually don't think I even got a good iPod version in all the times I tried it.
The last three lines basically fix the MP4 that ffmpeg created. It would mess up the pixel aspect ratio (PAR) and I just reset it here to 1:1 square pixels.