Sunday, April 18, 2010

Batch convert all video files to iPod playable format (requires MEncoder)

Batch convert all video files to iPod playable format (requires MEncoder)

#! /usr/bin/env python

import sys;
import os;
import os.path;
import string;

video_file_extensions = ["avi", "mpg", "mkv", "wmv", "dat", "vob", "flv", "mp4"];

def is_video_file(file):
    if file[-3:].lower() in video_file_extensions:
        return True;
    else:
        return False;

def convert_file(in_file, out_file):
    cmd = "mencoder  -really-quiet";
    cmd = cmd + " \"" + in_file + "\"";
    cmd = cmd + " -o " + "\"" + out_file + "\"";
    cmd = cmd + " -vf dsize=480:320:2,scale=-8:-8,harddup";
    cmd = cmd + " -oac faac -faacopts mpeg=4:object=2:raw:br=128";
    cmd = cmd + " -of lavf -lavfopts format=mp4 -ovc x264 -sws 9";
    cmd = cmd + " -x264encopts nocabac:level_idc=30:bframes=0:bitrate=900:";
    cmd = cmd + "threads=auto:turbo=1:global_header:threads=auto:subq=5:";
    cmd = cmd + "frameref=6:partitions=all:trellis=1:chroma_me:me=umh";
    print cmd;
    os.system(cmd);
    return None;

usage = "Usage:", sys.argv[0], "<file | directory> <destination directory>";

def batch_convert(file_dir_list, dest_dir):
    for ent in file_dir_list:
        if os.path.exists(ent) == True:
            if os.path.isdir(ent) == True:
                print "Entering", ent;
                dir_lst = os.listdir(ent);
                for i in range(len(dir_lst)):
                    dir_lst[i] = ent + "/" + dir_lst[i];
                batch_convert(dir_lst, dest_dir);
                print "Leaving", ent;
            elif is_video_file(ent) == True:
                slash_idx = string.rfind(ent, "/");
                out_file = "";
                if slash_idx != -1:
                    out_file = ent[slash_idx + 1:];
                    out_file = dest_dir + "/" + out_file[0:-3] + "mp4";
                else:
                    out_file = dest_dir + "/" + ent[0:-3] + "mp4";
                print "=" * 80;
                print "in_file = ", ent;
                print "out_file = ", out_file;
                convert_file(ent, out_file);
                print "=" * 80;
        else:
            print "Error:", ent, "does not exist.";
    return None;


if __name__ == "__main__":
    if ((len(sys.argv) < 3)
        or (sys.argv >= 3 and os.path.exists(sys.argv[len(sys.argv) - 1]) == True
            and (os.path.isdir(sys.argv[len(sys.argv)-1]) == False))):
        print usage;
    else:
        if os.path.exists(sys.argv[len(sys.argv) - 1]) == False:
            print "Creating directory:", sys.argv[len(sys.argv) - 1];
            os.mkdir(sys.argv[len(sys.argv) - 1]);

        batch_convert(sys.argv[1:len(sys.argv) - 1],
                      sys.argv[len(sys.argv) - 1]);

HTML generated by org-mode 6.33x in emacs 23

2 comments:

  1. Hi MWNN Have you fixed the issue what i have reported: If file contains spaces your batch file wouldn't work....

    ReplyDelete
  2. Kanth: Please let me know if the modified program works for you.

    ReplyDelete