Archive for August 11th, 2007

From DV/HDV/AVCHD to PS3/Xbox360/AppleTV


While there are a gazillion of video converters out there, I found none that is optimized to correctly deal both with anamorphic and interlaced video, or able to read intermediate lossless formats. There is always something that’s missing in the process of each of these utilities. It seems that all these converters are mostly consumed in transcoding pirated/DVD movies to another format rather than dealing with your home movies.

This is why I wrote the following tutorial, to show a way of how to export lossless video from your video editor (in this case Sony Vegas, although it’s similar with Adobe Premiere too) to an external converter and from there, to various formats (XViD, h.264, Flash, MP4, etc). The anamorphism and de-interlacing takes place on the video editor, so the converter only has to do that: converting. The main reason this tutorial exists is because the converter we use is more complex than most graphical converters, but it has the advantage of being able to read “lossless” codecs, which means that when you export from the video editor to the converter, you export to a special codec that loses no quality whatsoever. This workflow for videographers beats in quality any graphical converter out there.

The Method

1. Install Sony Vegas Movie Studio 8 Platinum Edition ($130 or 30 days free trial, suggested system requirements here). Grab your scenes from the tape and do your video editing on that program. Please note that h.264 encodings tend to de-saturate your mpeg2 HDV footage, so use the following filter that will affect the whole timeline by saturating the colors a bit: From the “Track FX” button select the “Color Corrector” filter and then select its “Studio RGB to Computer RGB” template. Meanwhile, make sure that your “Project settings” look like this for NTSC HDV (use a DV template if your camcorder is a plain DV widescreen one, but keep the field order, rendering quality and de-interlace method the same):

[ IF you shot with a Canon HV20 or HG10 in 24p mode, you will first have to remove pulldown before you start editing your scenes in Vegas. Then, select the 23.976 framerate in the “Project Settings” window. When you are creating a new project you can tell Vegas to create custom settings as a particular file on your disk, so you just tell it to derive the right settings from your pulldown’ed 24p videos. Then, make sure that it’s “progressive scan” for the “field order”, and “none” for the “Deinterlace method”. The Platinum edition of Vegas does support 24fps, even if it’s not officially advertised as being able to. It is in fact, the cheapest NLE in the market that supports 24p editing. ]

2. Install the Huffyuv 2.1.1 lossless codec from here. You do that by uncompressing the contents of the zipped file on your Desktop, right-clicking on the huffyuv.inf file and selecting “Install”. After this is done, you can delete these files. We will be using this codec because that’s the only lossless codec that produces ‘small’ filesizes while at the same time FFmpeg (our converter) supports it.

3. Once your have a final cut on Vegas, select “Render As” from its “File” menu. Select the .avi format from the “Save as type”, and then click the “Custom” button on the right side of the dialog. On the first tab select for “Video rendering quality” to be “Best”. On the “Video” tab select the field order to be “none, progressive scan”, select the right aspect ratio for your video, and on the “Video format” select “Huffyuv v2.1.1”. Once Huffyuv is selected, hit “Configure”, which is a button next to it. In the new window popping up, select for “RGB compression method” the “<-- Convert to YUY2” option. Hit “ok” in that window to go away. Back to the main “Custom Template” dialog, hit “ok”. Now, back on the main “Render As” dialog, for file name pick the name “Untitled.avi”, then immediately hit “save” and sit back while your video is being encoded to the Huffyuv lossless format. Be aware, the file size will be pretty huge, but at least you won’t be losing any quality.

4. In the meantime, install the PS3Video9 utility from here. Navigate to C:\Program Files\Red Kawa\Video Converter\Tools\FFmpeg\ and copy away the ffmpeg.exe and pthreadGC2.dll files found there. Feel free to uninstall PS3Video9 now (I recommend against using it as it can’t deal with interlacing correctly, it has a bad ad-ridden UI).

5. If the encoding has finished, move the Huffyuv-encoded “Untitled.avi” file on the same folder you have copied ffmpeg.exe & pthreadGC2.dll at.

6. You now have several options on how to encode your final file, but I have ready for you some templates, just download them and unzip them on the same folder that ffmpeg.exe is at (zip file’s last update: Dec 11 2007). As long as your .avi file is called “Untitled.avi” too, all you have to do is double-click on the desired .bat file. Use 1080p-30.bat for PS3/XBox360 and for TVs that are actually 1080p, use 720p-30.bat for PS3/XBox360 and for 1080i/720p TVs, and use 480p-30 for EDTVs, normal TVs, the AppleTV or if your source video is DV instead of HDV/AVCHD. Of course, if your footage is 24 fps, use the .bat files that their filename has the word “24” in them instead of “30”, or choose “25” if you are in a PAL country. Other formats are also available within these templates. Encoding will take some time depending on the speed of your computer, so be patient. Make sure that no other programs are running at the time, and that you are not swapping (aka “paging”, being out of RAM).

6. Now copy the resulted file on the multimedia device you want to, and enjoy it!

Important Notes

1. My .bat files are creating optimized h.264 files in two passes for optimal quality, although you can certainly edit some of these .bat files to change some aspects of the encoding. For example, you can speed-up encoding by doing only one pass. Remove the “-pass 1” attribute from the first ffmpeg paragraph and then remove completely the second paragraph, save the .bat file and then encode. Also, you can tweak the bit-rate by tweaking the numbers of the -b and -bt attributes. You can always edit the input video filename (in my .bat files it’s called “Untitled.avi) and the output video filename etc. Finally, the -title “XXXX” attribute sets the title on the video, so replace it with your own title (e.g. “A day at the zoo”). If you are on a PAL system, change all the instances of “-r 24000/1001” (or whatever value is after the -r attribute) to “-r 25000/1001” instead.

2. You can choose to de-interlace inside Vegas as I showed above (recommended), or you can de-interlace later, through ffmpeg. If you decide to de-interlace through ffmpeg, you must add the “-deinterlace” attribute *exactly* after the two “ffmpeg.exe” words on the .bat file of your choice. It must be the FIRST argument that ffmpeg gets, otherwise its de-interlacing creates a bad ghosting effect where there is fast motion. Yeah, I know it’s weird, but that’s ffmpeg we are talking about, the king of weirdness.

3. As you noticed, I recommended you encode at 480p for the AppleTV instead of 720p. This is not only because the AppleTV supports files only up to 720/24p instead of 30p, but also because the Quicktime engine (that AppleTV uses) has real trouble decoding videos encoded with the x264 encoder. It seems that Apple has optimized their Quicktime to only playback smoothly files that were encoded with Apple’s encoder. Problem is, Apple does not provide the right tools to do what we need here (lack of de-interlacing on Quicktime Pro, FCP is expensive), but also quality of the Apple h.264 encoder sucks compared to x264 and it’s ultra-slow to encode. So, I recommend staying with my method even at the expense of AppleTV. Hopefully, AppleTV 2.0 will be better/faster in that respect. My FFmpeg templates use CAVLC instead of CABAC so this should help the AppleTV to render the files easier.

4. In the most recent versions of the file I also added a few more templates, one for the (480×270 video sized) iPhone, one for the PSP (just copy the also 480×270 file on a folder named just “VIDEO” of the root of the PSP’s memory stick — create the folder if it doesn’t exist) and one for the QVGA iPod/iPhone+Symbian (for Symbian devices that run either UIQ 3.0+ or S60 3.1+). Unfortunately, there is no way to have the same h.264 file playing on both the Apple iPhone and the Sony PSP (even if the output resolution/bitrate is the same) because they are both really picky and not very compatible as to what they are expecting. This is why there are different templates for each. Also, there is an MP4 template that will create videos for QVGA cellphones. The “PMPs-and-NokiaInternetTablets” template is for these Asian XViD PMPs that are usually sold for cheap online, and Nokia’s 770 and N800 Internet Tablets. There are also two HD templates for XViD. Finally, the “FlashVideo” template is for adding Flash video on your own web site (*not* for uploading to YouTube or other video sharing sites). For more in-depth Flash video tutorial and additional Flash templates, check here.

5. For the PS3 in particular, you can copy the encoded h.264 .mp4 files on a folder called “VIDEO” (upper case) and then burn that to a regular DVD media (as normal files, not as DVD-video). The PS3 will automatically recognize it as a video-oriented media and it will playback the HD .mp4 videos in there directly from the DVD disk. If you will burn on a CD media instead, the bitrate should never exceed 8 Mbps, or the playback will stutter.