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

Sunday, March 28, 2010

Generating Profile data files for multi-process programs

Generating Profile data files for multi-process programs

Gprof uses profiling data files (i.e gmon.out) to generate profiling information. gmon.out files are generated by compiling and linking a program with the -pg option. The program is then executed and on a normal shutdown (either through a return 0 from main() or by invoking the exit() system call) the gmon.out file is generated in the process' current working directory.

In a multi-process program, the gmon.out file generated could either be corrupted or could have incomplete profiling information. This could happen because each of the process' end up writing (or overwriting) to the same gmon.out file.

One solution to this problem would be to create a separate directory for each of the executables and then run the multi-process program. gmon.out files for each process can then be found in their respective directories (unless the program invokes chdir() system call).

Author: mwnn <mwnnlin AT gmail DOT com>

Date: 2010-03-28 10:20:38 IST

HTML generated by org-mode 6.21b in emacs 23

Tuesday, March 16, 2010

Working with rar packages on GNU/Linux

Working with *.rar packages on GNU/Linux

  1. To extract files from a rar package use the e option:
    $ unrar e <rar package>
    
  2. To extract files from a partially downloaded rar package use the -kb option:
    $ unrar e -kb <rar package>
    

Author: mwnn <mwnnlin AT gmail DOT com>

HTML generated by org-mode 6.21b in emacs 23

Reboot Huawei SmartAX MT882 router from CLI

Reboot Huawei SmartAX MT882 router from CLI

#! /usr/bin/env python

import sys;
import telnetlib;

def establish_conn(host):
    tn = telnetlib.Telnet();
    tn.open(host);
    return tn;

def login(tn, user, passwd):
    tn.read_until("Login: ");
    tn.write(user + "\r\n");
    tn.read_until("Password: ");
    tn.write(passwd + "\r\n");
    return None;

def exec_cmd(tn, cmd, prompt):
    tn.read_until(prompt);
    tn.write(cmd + "\r\n");
    return None;

def reboot_router(tn, username, passwd):
    login(tn, username, passwd);
    exec_cmd(tn, "console enable\n", "--> ");
    exec_cmd(tn, "restart", "Quantum> ");
    return None;

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print "Usage: python", sys.argv[0], "<Router IP> <username> <password>";
        sys.exit(0);
    tn = establish_conn(sys.argv[1]);
    reboot_router(tn, sys.argv[2], sys.argv[3]);
    print "Router rebooted!";

Author: mwnn <mwnnlin AT gmail DOT com>

HTML generated by org-mode 6.21b in emacs 23