Return to Snippet

Revision: 22616
at January 17, 2010 06:20 by OtonVM


Initial Code
__author__ = "OtonVM"
__date__ = "$Jan 15, 2010 2:18:00 PM$"

import sys
import os
import subprocess
import os.path

if sys.platform == 'win32':
    source = r"C:\Users\Oton\Desktop\HandBrake"
    if os.path.exists(r"C:\Users\Oton\Desktop\HB Out") == False:
        os.mkdir(r"C:\Users\Oton\Desktop\HB Out")
    destination = r"C:\Users\Oton\Desktop\HB Out"
    encoder = sys.path[0] + r'\tools\handbrakecli\HandBrakeCLI.exe'
    audioFormat = "faac"

if sys.platform == 'darwin':
    source = r"/Users/Oton/Desktop/Encode"
    if os.path.exists(r"/Users/Oton/Desktop/HB Out") == False:
        os.mkdir(r"/Users/Oton/Desktop/HB Out")
    destination = r"/Users/Oton/Desktop/HB Out"
    encoder = sys.path[0] + r'/tools/handbrakecli/HandBrakeCLI'
    audioFormat = "ca_aac"

def encode(input, output):
    for root, dirs, files in os.walk(input):
        for file in files:
            (base, ext) = os.path.splitext(file)
            if ext.lower() == ".avi" or ext.lower() == ".mkv" or ext.lower() == ".mp4" or ext.lower() == ".m4v" or ext.lower() == ".mov":
                newfile = os.path.join(output, base + ".m4v")
                infile = os.path.join(root, file)
                if os.path.exists(newfile):
                    print('Skipping file: ' + infile)
                else:
                    encode = encoder + ' --encoder x264 --quality 30.0 --audio 1 --aencoder ' + audioFormat + ' --arate 44.1 --mixdown dpl2 --ab 96 --drc 2.0 --format mp4 --ipod-atom --maxHeight 240 --keep-display-aspect --modulus 16 --vfr --x264opts level=30:bframes=0:mbtree=1:weightp=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:8x8dct=0:me=umh:no-fast-pskip=1:no-dct-decimate=1:filter=-1,-1:qpmin=16:merange=128:subme=9:mixed-refs=0:trellis=0 --verbose 2' + ' -i "' + infile + '" -o "' + newfile + '"'
                    subprocess.call(encode)
                    os.remove(infile)

if __name__ == "__main__":
    encode(source, destination)

Initial URL


Initial Description
A simple python script for taking all files in the base dir and encoding them (and than removing them) into another folder. Handbrake, quality based, ipod nano compatible video, mac or windows.

Initial Title
Batch iPod Video Encoder

Initial Tags
python, video

Initial Language
Python