h1

Vista’s Video Nasties #4: H.264 recipe revealed

December 4, 2007

Vista busy cursor Here is the promised magic recipe, the complete solution, for automated H.264 encoding on Vista (or XP for that matter).

(Note that since I first published this post I have found an alternative, direct way to embed video into blogs where the video quality is as high as I wish. Details are here.)

Scenario

Input: The video to be encoded is an avi file in DV format, such as you might capture from a camcorder using Windows Movie Maker or other video editing application. The video may have been edited, so long as the edited footage has then been saved in DV format in an avi file. Audio content is assumed to be in PCM (uncompressed) format, typically with a 48,000 Hz sampling rate.

Output: The encoding process results in an mp4 file containing H.264 encoded video and AAC audio. The output file is suitable for uploading to on-line video hosting services such as youTube or vimeo.

Defaults: The default settings are video resolution of 448 x 336 and H.264 encoding quality of 26. These can be overridden by changing the relevant lines in the video.avs and video.bat files (see below).

Preliminary

It makes sense to create a folder on your PC where you will keep the relevant programs and working files. For the purposes of the post I will assume this is c:\videoenc

What you need to download and install

AviSynth

AviSynth 2.5 is a frameserver which we use here to perform the preliminary processing such as cropping and deinterlacing. Download the file Avisynth_257.exe from here. Run it to install AviSynth. Accept all defaults.

Smooth Deinterlacer

Get it from here and save the file smoothdeinterlacer.dll to c:\videoenc. Be sure to select the version designed to work with AviSynth 2.5x not the older version for AviSynth 2.0x.

x264

This is the H.264 encoder program itself. You can get it from here*. Click on any of the mirror links, e.g. mirror 01, next to x264.exe in the table of encoder download options. Save the x264.exe file to c:\videoenc. You don’t need to install anything. The file just needs to be in that folder.

[*Update 5 Dec 07: The link above appears to be down or unreliable. I have found another source for the x264.exe file here but have not yet had the opportunity to check its origin, version or to test it]

MP4Box

MP4Box takes the raw compressed video file and places it in an mp4 container file. The program is available from here. I downloaded version 0.4.4 compiled 3 June 2007. Again, you just need MP4Box.exe to be in the folder c:\videoenc.

MPlayer

You can get this from here. Download “MPlayer 1.0rc1 Windows” (not the GUI version) from any of the mirror site links. You need MPlayer to extract the uncompressed audio from the DV video file, so it can then be compressed as AAC audio. You only need the mplayer.exe file. Put it in c:\videoenc.

faac

The utility that compresses your extracted audio in AAC format. Available from here. Download the faac-1.26.1.zip file. Place the faac.exe file in c:\videoenc.

Creating the text files that automate the process

Within the c:\videoenc folder create a text file called video.txt. Open it and copy the following text into it:

LoadPlugin(“SmoothDeinterlacer.dll”)
DirectShowSource(“c:\videoenc\video.avi”)
SmoothDeinterlace(tff=false, doublerate=false)
LanczosResize(448,336)
converttoyv12()

Close it, saving the changes. Change the filename to video.avs.

Next create another file called video.txt. Open it and copy the following text into it:

@ECHO OFF
x264.exe –fps 25 –qp 26 –progress –output video.264 video.avs
MP4Box -flat -add video.264:fps=25 -v -new video.mp4
mplayer -vc null -vo null -ao pcm:fast video.avi
faac -b 128 –mpeg-vers 4 audiodump.wav
MP4box -add audiodump.aac video.mp4
PAUSE
CLS
EXIT

Close it, saving the changes. Change the filename to video.bat.

NOTE: The commands in video.bat as shown above assume a PAL DV source hence a frame rate of 25 fps. If your source video is in NTSC format you need to modify lines 2 and 3 to refer to a frame rate of 30 fps.

Encoding a DV avi file

Just save your raw or edited DV file to the c:\videoenc folder. It has to be called video.avi so save it under that name or rename it as applicable.

Double click the video.bat file in the same folder.

That’s it. The whole encoding process will now run from beginning to end under control of the commands in that file. You can go and make a cup of coffee.

Meantime a DOS box will open and display lots of stuff. Eventually, the display stops changing and the bottom line reads “Press any key to continue …” When you do that the DOS box disappears.

You can then find your encoded file, containing H.264 encoded video and AAC encoded audio, in c:\videoenc. It is called video.mp4. You can now upload it directly to youTube, vimeo, veoh, whatever.

Changing settings

The default resolution is 448 x 336 pixels. If you want something different just change line 4 of the video.avs file. You can just open the file with Notepad to make the changes. You must though ensure that both the horizontal and vertical resolutions are multiples of 16.

The default H.264 encoding quality is 26, on a scale from 1 to 51, using single-pass encoding. You can modify the quality setting by changing the “qp” parameter in line 2 of video.bat. Lower values for qp improve quality but increase the bitrate and filesize, vice versa for higher values.

In principle it should be possible to obtain better quality for the same filesize by using 2-pass or 3-pass encoding. So far I haven’t seen much improvement in my experiments with multi-pass encoding but it’s early days. I’ll report on my discoveries in a future exciting episode of Vista’s Video Nasties.

Credits

I found this very helpful when I was getting started with a command line solution for use of x264. It seems to be part of an encoding guide that looked highly promising but was abandoned years ago. Shame.

The rest of it was down to trial, error and slog.

AddThis Social Bookmark Button

Advertisements

12 comments

  1. Well I was having most of the problems listed above:

    no output file and deblock = ps but i got those fixed. Now I am getting

    Rawyuv input requires a resolution.


  2. Jimmy – thanks for clearing that up. Many thanks.


  3. Hi

    The difference is that x264.exe –fps 25 –qp 26 –progress –output video.264 video.avs should be with double — infront of for example fps. On the webpage it only shows a single one. So that is the diffence.

    Best regards
    Jimmy


  4. Compare in Word can’t find any differences either. Sure you posted your corrected version or might you have reposted the original by mistake?


  5. Glad you solved your problem Jimmy but I’m struggling to see what’s different in your version of the batch file. Must be going blind in my old age.


  6. Hi again

    Found the solution. the video.bat should look like this

    @ECHO OFF
    x264.exe –fps 25 –qp 26 –progress –output video.264 video.avs
    MP4Box -flat -add video.264:fps=25 -v -new video.mp4
    mplayer -vc null -vo null -ao pcm:fast video.avi
    faac -b 128 –mpeg-vers 4 audiodump.wav
    MP4box -add audiodump.aac video.mp4
    PAUSE
    CLS
    EXIT


  7. Hi

    Thanks for this great instruction, but I have a problem.

    When calling x264.exe –fps 25 –qp 26 –progress –output video.264 video.avs

    I get “x264 [error]: invalid argument: deblock = ps”

    Jimmy


  8. Wow, thanks for this. This works faultlessly for me under vista. I like you, cannnot fatham, why we still have to use DOS to get the job done. At least we can rely on the good ole black box eh 😉

    Thank you


  9. Glad you got it working.


  10. I got it working finally. Turns out its not a good idea to cut and paste from sites. Your avs file (”) (0x94)should have been (“) (0x22) and the x264 parameters are (–) not (-)and the video.avi is in C:\videoenc\ not C:\enc but I’m having some fun now trying to figure out x264 parameters to give decent quality at the smaller file sizes. thx.


  11. Afraid I have no explanation other than maybe a file of the same name already exists and is read only so can’t be overwritten? Really can’t think what else might be causing problems. Maybe start again with a completely new folder? Best of luck.


  12. Thanks for the fantastically detailed instructions. However I can’t get the first command to run. (x264) it crashes saying:”[error]: No output file.” I looked at the x264 help and change -output to -o with no difference then I elminated the avisynth script and just used my video.avi file to try to simplify and got the same result no output file result.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: