12Feb/102
Convert all AVI’s from a directory to MP4 using ffmpeg and Perl
I recently bought a new iPod (nano 5G) and I couldn't find a script that converts multiple AVI's from a directory to MP4 quickly so I wrote one myself. It uses ffmpeg to convert so you need to have it installed. 170MB AVI should be like 80MB as MP4.
#!/usr/bin/perl
# avi2mp4.pl
# Sun Feb 12 22:41:46 2010
# Copyright 2010 jolttz
# jolttz{ät}gmail{dot}com
#
use strict;
my $dirname = $ARGV[$0];
my ($width, $height, $size);
my $bitrate = 409600;
opendir(DIR, $dirname) or die "Can't open directory $dirname: $!";
while (defined(my $file = readdir(DIR))) {
if ($file =~ m/(.+)\.avi/i) {
# Get the aspect ratio
open(P, "ffmpeg -i '$dirname/$file' 2>&1 |") or die "$!\n";
while(
) {
if (/Video:.+ (\d+)x(\d+)/) {
$width = $1;
$height = $2;
}
}
close(P);
die "Failed finding the aspect ratio.\n" unless defined $width;
my $aspect = $width/$height;
if (abs($aspect - 16/9) < 0.02) {
$size = "320x180";
} elsif (abs($aspect - 4/3) < 0.02) {
$size = "320x240";
} else {
die "Weird aspect ratio: ${width}x${height} = $aspect\n";
}
print $size;
print "Converting '$dirname/$file'...\n";
exec("ffmpeg", "-i", $dirname. "/" .$file, "-b", $bitrate, "-s", $size, "-vcodec", "mpeg4", "-ab", 128, "-acodec", "libfaac", "-r", 25, $dirname. "/" .$1. ".mp4");
}
}
closedir(DIR);
Syntax:
b22stard@b22s-desktop ~ $ perl avi2mp4.pl /dir/to/the/avis/
Enjoy