Video Encoding Script

“enc” is the project page for this simple (yet powerful) video encoding script. The requirements are just Python and ffmpeg so it should also run on Windows and Mac. It has only been tested on Debian Linux and FreeBSD.

With the current settings it uses libx264 to encode H.264 video, libfaac to encode AAC audio and outputs into a .mp4 container format.

If you want to compile ffmpeg, x264 and faac from source, I have written a short how to compile fffmpeg guide. It is a good idea to always use the latest versions of ffmpeg and x264.

Download

Version 1.3.2 (2012-03-06)

Examples of use

Multiple DVDs to half CDs with de-interlacing and 128kbps stereo audio:

enc -t halfcd -a 128 -o '-deinterlace' *.VOB

Blu-ray to data DVD (1080p – Will play on VLC in a PC that has a DVD drive or a Blu-ray player but not a standalone DVD player). DVDs have a file size cap of 4GiB per file. 384kbps 5.1 surround sound audio.

enc -t dvd -a 384 -c 6 example.m2ts

Blu-ray to iPod Classic or Nano (from a 16:9 source, with automatic padding)

enc -s 1000m --ipod=16x9 example.m2ts

Blu-ray to iPhone or iPod Touch (untested, -s may not be required or may give bad aspect ratio)

enc -s 1000m -o '-vpre libx264-ipod640 -s 480x320' example.m2ts

Command line arguments

“-h”, “–help” – print command line help
“-s”, “–size” – specify a target size e.g. 350m or 4g
“-t”, “–target” – specify target size by media name e.g. quartercd (175m), halfcd (350m), cd (700m), dvd (4g)
“-d”, “–deinterlace” – enable deinterlacing
“-a”, “–ab” – specify audio bitrate in kbps (default 128, 192 for >350m, 384 for 5.1)
“-c”, “–ac” – specify number of audio channels (default 2, use 6 for 5.1)
“-o”, “–opts” – specify extra options to be passed directly through to ffmpeg
“–sim” – print ffmpeg command lines without executing them.

Change log

Version 1.3.2 – 2012-03-06

+ “-h”, “–help” added to show usage
+ shebang changed to use /usr/bin/env
+ ffmpeg video bitrate argument -b changed to -b:v
- x264 presets fast first pass and high/slow removed, as they are no longer available in ffmpeg

Version 1.3 – 2010-03-19

+ “-d”, “–deinterlace” option added for easy deinterlacing
+ “-c”, “–ac” option added for setting the number of audio channels (2 for stereo, 6 for 5.1 surround)
+ Automatic audio bitrate, if not explicitly specified. Outputs 350MiB or less are 128kbps. Outputs over 350MiB are 192kbps if stereo, 384kbps if surround.
+ Header added to script

Version 1.2 – 2009-08-11

+ “-t”, “–target” option added to target Quarter CD, Half CD, CD and data DVD (175MiB, 350MiB, 700MiB and 4GiB respectively).
+ Size argument now accepts “k”, “m” and “g” suffixes. The argument “-s 350″ now means 350 bytes while “-s 350m” means 350 MiB.
+ “-o” option can now be used multiple times. Options will be concatenated.
+ “–ipod” argument adds 320×240 iPod support. This argument specifies the source aspect ratio and converts it to 4:3, if necessary, with top and bottom padding.
+ Positive bit rate checking added.

Version 1.1 – 2009-07-17

+ All scripts moved into a single python script.
+ Command line arguments added to avoid the need to edit the script
+ “-o”, “–opts” option added for added flexibility
+ “–sim” added for simulation only. This prints out the ffmpeg commands rather than executing them.

Version 1.0 – 2009-06-16

+ Python script to calculate bit rates based on target file sizes and constant audio bit rates.
+ Bash script to loop through files and run 2-pass ffmpeg on them.

Known bugs

* ffmpeg’s -t argument is ignored by the script so using something like -o ‘-t 60′ will generate bad bit rates. You can still use this but it will be as if you encoded the whole thing targeting a file size and then cut some seconds out of that. The current workaround is to cut what you want with the “copy” codecs and the “-t” and “-ss” arguments beforehand and then encode the resulting file with this script.
* Output file names are not very pretty. example.VOB will produce an output file called example.VOB.mp4 rather than example.mp4.

7 Responses to “Video Encoding Script”

  1. Matt says:

    Erm, I don’t know if this is a bug but it seems to act weird when it does the 2 passes. Here’s an example of the commands it sends out..

    ffmpeg -i 1.VOB -pass 1 -an -vcodec libx264 -vpre fastfirstpass -b 378744 -bt 378744 -threads 0 -y -f mp4 pass1.tmp
    ffmpeg -i 1.VOB -pass 2 -acodec libfaac -ac 2 -ab 128k -vcodec libx264 -vpre hq -b 378744 -bt 378744 -threads 0 -y 1.VOB.mp4

    is it supposed to input from A to pass1.tmp and then from A to B? Shouldn’t it be A to tmp then tmp to B.

    Another thing. It doesn’t get rid of pass1.tmp when it finishes.

  2. George Helyar says:

    No, the tmp file is not used by the second pass at all and can even be deleted safely before the second pass.

    Only the log file (and other similar files) are used by the second pass, as well as the original input media of course.

    The script does not delete anything. You have to clean it up yourself.

    If you still have the log and need to make changes such as choosing a different audio stream, you may be able to skip the first pass and run only the second pass again.

  3. a,alex says:

    hi, is there a chance you can add flvtool meta injection for mp4 clips and automate the entire process with cron ?

  4. George Helyar says:

    That is outside of the scope of this script.

    This script simplifies video encoding using ffmpeg, x264, faac and an mp4 container. Specifically, it simplifies it by drastically reducing the command line arguments you need to enter, batching multiple files to encode and automatically calculating bitrates to target a given file size e.g. to fit on a CD and play in a Blu-ray player or computer.

  5. Sean says:

    What an excellent script! You have saved me a lot of time and effort in working out how to get this done and for that I thank you!

  6. BUGHUNTER says:

    Thanks for your efforts, however please make it easier to understand, what your script really does by puuting together at least some basic information into a README.TXT or any other documentation file. Please consider people finding this page via goog and asking themselves:
    - what exactly does this script
    - what is the difference to directly calling ffmpeg?
    - are there any parameters that can be defined?
    - how to define input directories with files to convert?
    - which default ffmpeg params does it use?

    yes, we can just read the source, but would make sense to see these info before downloading and analyzing code…

    also pleaase consider putting it up on github for better collaboration!

  7. George Helyar says:

    Hi Bughunter, thanks for the feedback. There’s a help command and some examples. You don’t need to look at the source.

    It’s quite a simple script, it mostly just simplifies batch encoding and targeting specific file sizes as described in the original post it came from. There is also a description on this page.

    It doesn’t require anything as complex as input directories. It just takes a list of files, which you can list manually or your shell will pass automatically if you use wildcards such as *.m2ts.

    It just uses basic bitrate and codec parameters. If you want to pass any extra options not specified through the script you can just use -o or you can use –sim to output the ffmpeg command lines without executing them, for further tuning or running them at a later date.

Leave a Reply