Home Computers Handbrake CLI rip for AppleTV and video iPod
CLI rip for AppleTV and video iPod PDF Print E-mail
Written by Gordon Tillman   
Thursday, 04 December 2008 18:18
I wanted to automate the process of using the CLI version of HandBrake to rip one or more titles in a format that looked good both on the Apple TV and on video iPods. Here is a Python script front-end to the HandBrakeCLI program. I just call it rip. You will need to change the command setting to match where you have HandBrakeCLI installed. If you are not running Ubuntu you may also need to change input_src.
#!/usr/bin/env python
"""Front end to HandBrakeCLI, optimized for iPod and AppleTV
"""
import optparse
import os.path
import sys
import textwrap

# Option default values
command=os.path.expandvars("$HOME/bin/HandBrakeCLI")
input_src="/media/cdrom0"
title="1"
audio="1"
subtitle=None
view_only=False

titles=[]
audios=[]
subtitles=[]

rip_options="-e x264b30 -b 1500 -B 160 -R 48 -E faac -f mp4 -w 640 -x keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1 -m"

usage = """usage: %prog [options] 'first_title_name' ['second_title_name'...]

Rips a title or titles from a DVD in a format that looks good on both the
AppleTV and a Video iPod.

These are the parameters that are used for extraction.  They are eqivalent
to the iPod High-Rez presents in the GUI client:

-e x264b30
-b 1500
-B 160
-R 48
-E faac -
f mp4
-w 640
-x keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:
   vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1
-m

Notes:
1. Enclose each title_name in single quotes if it
   contains embedded spaces or other special
   characters.  It doesn't hurt just to **always**
   embed each title_name in single quotes!
   DO NOT include the file extension.  We will put one
   on for you; e.g., 'Booty Call', not 'Booty Call.m4v'
2. TITLE(S) - Titles are specified by an integer: 1, 2, 3, etc.
   If you want to rip a single title (say title 1), you would
   specify something like this:
   %prog -t 1 'Gone With the Wind'
   If you want to rip multiple titles (say 1-4), you would
   specify something like this:
   %prot -t 1,2,3,4 'First Movie' 'Second Movie' 'Third Movie' 'Forth Movie'
3. AUDIO_TRACK(S) - By default, uses audio track 1 for each title.
   You can override this by specifying the track or tracks to use.
   Here are a couple of examples:
   
   Rip one title with audio track 2:
   
   %prog -a 2 -t 1 'Blah'
   
   Rip two titles with audio track 2 for the first
   title and 1 for the second:
   
   %prog -a 2,1 -t 1,2 'Blah1' 'Blah2'
   
   If you specify fewer audio tracks than titles, the
   value of the last that you specify is used for all.
   For example, the following would user audio track 2
   for each title ripped.
   
   %prog -a 2 -t 1,2,3 'Number 1' 'Number 2' 'Number 3'
4. SUBTITLE(S) - By default, no subtitles are ripped.  If
   you want to rip subtitles, you can do so.  The same
   notes apply as in note 3.
    """ 
    

def do_cmd(cmd, comment=None):
    max_len = 72
    print "*" * max_len
    if comment:
        print comment
        print
    print "Command:"
    for line in textwrap.wrap(cmd, max_len):
        print line
    print "*" * max_len
    os.system(cmd)

    
if __name__ == '__main__':
    p = optparse.OptionParser(usage=usage)
    p.add_option("-c", default=command, metavar='COMMAND',
        help="path to the HandBrake command (default = %s)" % command)
    p.add_option("-i", default=input_src, metavar='INPUT',
        help="the input source (default = %s" % input_src)
    p.add_option("-t", default=title, metavar='TITLE(S)',
        help="(required) the title or titles to extract.  see note 2 above.")
    p.add_option("-a", default=audio, metavar='AUDIO_TRACK(S)',
        help="the audio track or tracks to use.  see notes 3 above.")
    p.add_option("-s", default=None, metavar='SUBTITLE(S)',
        help="the subtitle track or tracks to use.  see note 4 above.")
    p.add_option("-v", action="store_true", default=False,
        help="if specified, just dump the contents of the DVD and don't extract anything.")
    
    (options, names) = p.parse_args()
    
    titles = options.t.split(",")    
    audios = options.a.split(",")
    if options.s:
        subtitles = options.s.split(",")
    else:
        subtitles = [None]
    
    while len(audios) < len(titles):
        audios.append(audios[-1])
    while len(subtitles) < len(titles):
        subtitles.append(subtitles[-1])
    
    if options.v:
        cmd = "%s -t 0 -i %s" % (options.c, options.i)
        do_cmd(cmd, "Viewing DVD Information")
    else:        
        if len(names) < len(titles):
            print usage
            sys.exit(1)

        for i in range(len(titles)):
            t = titles[i]
            a = audios[i]
            s = subtitles[i]
            if s:
                st = "-s %s" % s
            else:
                st = ""
            n = "%s.m4v" % names[i]
            cmd = "%s %s -t %s -a %s %s -i %s -o '%s'" % (options.c, rip_options, t, a, st, options.i, n)
            #cmd = "%s -f mp4 -m -d -t %s -a %s %s -e x264b30 -E faac -w 640 -b 1500 -i %s -o '%s'" % \
            #(options.c, t, a, st, options.i, n)
        
            do_cmd(cmd, "Ripping title %d of %d" % (i+1, len(titles)))
        os.system("ls -l *.m4v")      
Last Updated on Thursday, 04 December 2008 19:46