Video Cutter: Video Cutting using Mencoder

26 06 2011

I have written a perl script, then can split/cut parts from a video file.

#!/usr/bin/perl -w
#===============================================================================
#
#         FILE:  video_cutter.pl
#
#        USAGE:  ./video_cutter.pl [options] <input_video_file>
#
#  DESCRIPTION:  
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
#      VERSION:  1.0
#      CREATED:  06/26/2011 03:57:55 PM IST
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

use Getopt::Std;

sub usage()
{
   print STDERR "USAGE: $0 [options] <input_video_file>\n";
   print STDERR "          -c <conf_file>    default  /base_dir/input_video_file/video_cutter.conf\n";
   print STDERR "                            Start_time(hh:mm:ss),End_time(hh:mm:ss)\n\n";
   print STDERR "          -o <out_dir>      default  /base_dir/input_video_file/\n";
}

sub hms_to_seconds()
{
   my $end_sec = 0;
   my ($h, $m, $s) = split(/:/, $_[0]);
   $s = defined($s) ? $s : 0;
   $end_sec += $s;
   $m = defined($m) ? $m : 0;
   $end_sec += (60 * $m);
   $h = defined($h) ? $h : 0;
   $end_sec += (3600 * $h);
   return($end_sec);
}

my %opts;
getopt('o:c:', \%opts);

foreach my $opt (sort keys %opts)
{
   if (!defined($opts{$opt}))
   {
       print STDERR "$0: Requires value for option '$opt'\n";
       &usage();
       exit(-1);
   }
}

if (@ARGV != 1)
{
   &usage();
   exit(-1);
}

my $input_video_file = "$ARGV[0]";
my $out_dir = `dirname $input_video_file`;
chomp($out_dir);
my $conf_file = "$out_dir/video_cutter.conf";

if (defined($opts{"c"}))
{
   $conf_file = $opts{"c"};
}
if (defined($opts{"o"}))
{
   $out_dir = $opts{"o"};
}

unless (-f "$input_video_file")
{
   print STDERR "$0: Input video file '$input_video_file' is not present\n";
   exit(-1);
}
unless (-f "$conf_file")
{
   print STDERR "$0: split conf file '$conf_file' is not present\n";
   exit(-1);
}
unless (-d "$out_dir")
{
   print STDERR "$0: out dir '$out_dir' is not present\n";
   exit(-1);
}
unless (-w "$out_dir")
{
   print STDERR "$0: out dir '$out_dir' is not writable\n";
   exit(-1);
}

my $mencoder = "/usr/local/bin/mencoder";
unless (-x $mencoder)
{
   $mencoder = "/usr/bin/mencoder";
}
unless (-x $mencoder)
{
   print STDERR "$0: please install mencoder\n";
   print STDERR "sudo apt-get install mencoder\n";
   exit(-1);
}

open(CFH, "$conf_file") or die("$0: Cannot open '$conf_file'\n");
my $line;
my $nline = 0;
my $nsplit = 0;
my $split_name = `basename $input_video_file`;
chomp($split_name);
$split_name =~ s/\.[^.]*$//;
my $split_ext = $input_video_file;
$split_ext =~ s/.*\.([^.]*)$/$1/;
my $success = 0;
while ($line = <CFH>)
{
   chomp($line);
   $nline++;
   next if ($line =~ m/^#/);
   my ($start_time, $end_time) = split(/,/, $line);
   next if (!defined($end_time));
   my $start_sec = &hms_to_seconds($start_time);
   my $end_sec = &hms_to_seconds($end_time);
   if ($start_sec >= $end_sec)
   {
       print STDERR "$0: $start_sec >= $end_sec\n";
       print STDERR "    $start_time >= $end_time ... skipping for line no $nline ...\n";
       next;
   }
   $end_sec -= $start_sec;
   my $cmd = sprintf("%s -ss %d -endpos %d -ovc copy -oac copy -o %s/%s_%03d.%s %s",
           $mencoder, $start_sec, $end_sec, $out_dir, $split_name, $nsplit,
           $split_ext, $input_video_file);
   print "\n\n";
   print "-" x 80 . "\n";;
   print "$cmd\n";
   system("$cmd");
   if ($? != 0)
   {
       print STDERR "$0: failed to create $nsplit split for line no $nline\n";
       print STDERR "\t$cmd\n";
   }
   else
   {
       print STDOUT "Created $nsplit split for line $nline\n";
       $success++;
   }
   print "-" x 80 . "\n";;
   $nsplit++;
}

close(CFH);

print "\n";
print "=" x 80 . "\n";;
printf("Total lines = %d,   Success = %d/%d,  Failure = %d/%d\n",
       $nline, $success, $nsplit,
       $nsplit - $success, $nsplit);
print "=" x 80 . "\n";;

exit(0);

Please place the above perl script (video_cutter.pl) in any directory present in $PATH.

Sample Run:

Usage of the above script.

$ video_cutter.pl 
USAGE: /home/mitesh/Programming/Perl/WCS/video_cutter.pl [options] 
         -c     default  /base_dir/input_video_file/video_cutter.conf
                           Start_time(hh:mm:ss),End_time(hh:mm:ss)

         -o       default  /base_dir/input_video_file/

Content of a sample config file, specifying the split timings.
This will create 3 splits, of 0s-90s, 140s-210s, and 240s-End.

$ cat test_video.conf 
00:00:00,00:01:30
00:02:20,00:03:30
00:04:00,59:59:00

Now, it will create the three split files in ~/Video/test/ directory.

$ video_cutter.pl -c test_video.conf -o ~/Video/test test_video.vob 

--------------------------------------------------------------------------------
/usr/local/bin/mencoder -ss 0 -endpos 90 -ovc copy -oac copy -o /home/mitesh/Video/test/test_video_000.vob test_video.vob
MEncoder 1.0rc4-4.4.5 (C) 2000-2010 MPlayer Team
...
...
...
Created 2 split for line 3
--------------------------------------------------------------------------------

================================================================================
Total lines = 3,   Success = 3/3,  Failure = 0/3
================================================================================
$ ls -l ~/Video/test/*.vob
-rw-r--r-- 1 mitesh mitesh   7519672 Jun 27 00:31 /home/mitesh/Video/test/test_video_000.vob
-rw-r--r-- 1 mitesh mitesh   5844598 Jun 27 00:31 /home/mitesh/Video/test/test_video_001.vob
-rw-r--r-- 1 mitesh mitesh 570855360 Jun 27 00:30 /home/mitesh/Video/test/test_video_002.vob




CPU Frequency Scaling

24 05 2011

I have written a small shell script to increase or decrease CPU frequency. By default, it shows current CPU Frequency Scaling Governor. This can be changed only by root user. So this script needs to be run as root user or sudo as root, while changing the CPU Frequency Scaling governor.

#!/bin/bash

available_governors=$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_governors \
            | head -1 | sed -e 's/ \([a-zA-Z0-9]\)/|\1/g' -e 's/ $//')
if [ $# -ne 1 ]
then

   echo "USAGE: $0 [$available_governors]"
fi

echo "Command line to change CPU Scaling."
echo "                    - By Mitesh Singh Jat"
echo ""

## CPU Governor path
#/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
function current_cpu_governor ()
{
   echo -n "Current CPU Scaling Governor is: "
   cpu_scaling_governor="NOT SET"
   for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
   do
       cpu_scaling_governor=$(cat $governor)
   done
   echo "$cpu_scaling_governor"
}

current_cpu_governor;

## Exit, if no governor is provided
new_governor=""
if [ $# -eq 0 ]
then
   exit 0
else
   new_governor="$1"
fi

## Run as root always
user_id=`whoami`
if [[ "$user_id" != "root" ]]
then
   echo "$0: please run this script as root user."
   exit
fi 

if [ -z $(echo $available_governors | sed -e 's/^/|/' -e 's/$/|/' | grep "|$new_governor|") ]
then
   echo "Sorry, this mode '$new_governor' is not supported."
   exit 1
else
   echo "Setting CPU into '$new_governor' Mode..."
fi
## Now set cpu governor to the given mode
for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
do
   echo "$new_governor" > $governor
done
current_cpu_governor;

exit 0

Sample Run

Getting current CPU Frequency Scaling Governor
$ cd /path/where/cpu_scaling.sh/is/copied/
$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand

Increasing CPU frequency (Please run as root).
$ ./cpu_scaling.sh performance
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand
./cpu_scaling.sh: please run this script as root user.
$ sudo ./cpu_scaling.sh performance
[sudo] password for mitesh:
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand
Setting CPU into Performance Mode...
Current CPU Scaling Governor is: performance
$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: performance

Decreasing CPU frequency
$ sudo ./cpu_scaling.sh ondemand
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: performance
Setting CPU into OnDemand Mode...
Current CPU Scaling Governor is: ondemand




Playing only Audio from a Video File

11 03 2011

If you want to play only audio from a video file (say xyz.avi),
please provide -vo null in mplayer, given as:

$ mplayer -vo null xyz.avi





Speeding up Linux File System (ext3) Performance

9 03 2011

We can increase ext3 performance, hence increasing Hard Disk performance by ~40%, and decrease power consumption by Hard Disk, if we provide “noatime,nodiratime” options during partition mounting. Actually what happens is: whenever a file/directory is accessed, its atime “access time” is updated with epoch. These two options prevent these not-so-useful disk accesses. Hence
performance improvement.

The sample mount point (/home) entry in /etc/fstab may look like:

$ cat /etc/fstab | grep home
/dev/sda2 /home           ext3    defaults,noatime,nodiratime        0       0




Windows XP in Grub 2

24 02 2011

When I installed Debian 6.0 (Squeeze) on my laptop, I found that Debian Installer had not added Windows XP entry in the GRUB 2. You might also face same issue, then you can add following lines in /boot/grub/grub.cfg

## (1) Windows XP in /dev/sda1
menuentry "Windows XP" {
    set root=(hd0,1)
    chainloader +1
}

Here are few tips how to set root in the grub.cfg

# DEVICE NAME CONVERSIONS
#
#  Linux           Grub
# -------------------------
#  /dev/fd0        (fd0)
#  /dev/sda        (hd0)
#  /dev/sdb2       (hd1,2)
#  /dev/sda3       (hd0,3)
#




Cleaning of Temporary Install Files in Ubuntu/Debian

2 02 2010

Cleaning of Temporary Install Files in Ubuntu/Debian can be done by giving
following commands:

$ sudo apt-get autoclean; sudo apt-get autoremove





How to change boot order in GRUB2?

15 12 2009

With Ubuntu 9.10 (Karmic Koala), GRUB2 is the default boot loader.
How to change boot order in GRUB2?

1.


$ cat /etc/grub/grub.cfg

see the order of the wanted kernel. Starts from 0.

2.


$ vi /etc/default/grub

change GRUB_DEFAULT=0 value to wanted kernel

3. run


$ update-grub
 

to update

4. reboot and check with


$ uname -r 

to see if correct kernel selected.





Find Biggest Files/Directories

7 12 2009

If you want to find files which are consuming significant amount of disk space,
please use the following commands.

Since ls does not give correct size on disk, better to
use du command.

$ du /path/to/dir | sort -nr

I hope above
command will give you the proper result you wanted.

Note: The above command may take large time, depending on
number of files in /path/to/dir . You can use depth(say 2) in
that dir.

$ du --max-depth=2 /path/to/dir | sort -nr





Newer Yahoo! Messenger Protocol on Pidgin on Debian Lenny 5.0

17 11 2009

Yahoo! has changed authentication method of Yahoo! messenger protocol, which is not supported in Pidgin (Version = 2.5.7) supports newer Yahoo! Messenger Protocol. But Debian 5.0 Lenny repository has older pidgin (2.4.3). We can install newer pidgin, in the following steps:

## download latest pidgin from pidgin.im (at this time 2.6.3 is latest)
$ wget http://sourceforge.net/projects/pidgin/files/Pidgin/pidgin-2.6.3.tar.bz2

## extract files using tar
$ tar -zxvf pidgin-2.6.3.tar.bz2

$ cd pidgin-2.6.3

## Configure, Compile, and make
$ ./configure --disable-screensaver --disable-vv --disable-avahi --disable-tcl --disable-tk --prefix=/usr 
$ make
$ sudo make install

Now you can use newer pidgin with newer Yahoo! Messenger. :)
Enjoy Yahoo!





Find Files Created Between 2 Times

17 11 2009

In order to find files created between two times (start hour and end hour). The required hours are hours from current time. For example,

Current time = 2 PM = 14:00

If you want file created between 9 AM and 12 PM today, the start hour and end hour are:

Start Hour = (14 – 9) = 5
End Hour = (14 – 12) = 2

Hence required command is:

$  ./find_files_between_times.pl /path/to/dir 5 2

The Perl script which does this is given below:-

#!/usr/bin/perl -w
#===============================================================================
#
#         FILE:  find_files_between_times.pl
#
#        USAGE:  ./find_files_between_times.pl <dir> <start_hour> <end_hour>
#
#  DESCRIPTION:  
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
#      COMPANY:  Yahoo Inc, India
#      VERSION:  1.0
#      CREATED:  11/13/2009 10:00:02 PM IST
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

if (@ARGV != 3)
{
    print STDERR "Usage: $0 <dir> <start_hour> <end_hour>\n";
    exit(-1);
}

my $dir = $ARGV[0];
## Calculate current_hour - given_hour
#my $start_time = `/bin/date "+\%H"`;
my $start_time = $ARGV[1];
#chomp($start_time);
my $end_time = $ARGV[2];
$end_time = time - ($end_time * 60 * 60);
#$end_time = $start_time - $end_time;
#$start_time = $start_time - $ARGV[0];

if ($start_time > $end_time)
{
    ($start_time, $end_time) = ($end_time, $start_time);
}

my $cmd = "find $dir -ctime -$start_time";
print "Running $cmd\n";
my @files = `$cmd`;
foreach my $file (@files) # (`find $dir -ctime $start_time`)
{
    chomp($file);
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                           $atime,$mtime,$ctime,$blksize,$blocks) 
        = stat($file);

    if ($ctime > $end_time)
    {
        next;
    }
    print "$file\n";
}

exit(0);








Follow

Get every new post delivered to your Inbox.