Wednesday, August 19, 2009

Parsing a binary file in Perl

This is an example of how to parse a binary file in perl. This reads an mpeg-2 file and chops the first 8 Mbyte chunk out of the file, closing at the next clean sequence header boundary.

#! perl
use Getopt::Std;

use strict;

sub chopMpegFile
{
my ($fileName) = @_;

open FILE, "$fileName" or die "Could not open file: $!\n";
open OUT, ">out.mpg" or die "Could not open file: $!\n";

binmode(FILE);
binmode(OUT);

my $buffer = '';
my $count = 0;
my $done = 0;

while ((! $done) && ( sysread(FILE, $buffer, 4) ))
{
my $value = unpack 'N', $buffer;
if (($count++ > 2 * 1024 * 1024) && ($value == 0x000001b3))
{
print "Closing file.\n";
$done = 1;
}
else
{
syswrite(OUT, $buffer, 4);
}
if (($count % (1024*100)) == 0)
{
print "Count $count\n";
}
}
close(FILE);
close(OUT);
}

my @args = splice(@ARGV, 0);
foreach my $arg (@args)
{
print "$arg\n";
&chopMpegFile($arg);
}