#!/usr/bin/perl # this script is intended to fix a specific problem I found with my # mythtv files that were captured by firewire. It may be of value # in other cases, but don't use this if you don't understand what it does. # # this script seeks through the beginning of the file specified, until it # finds four sequences where the TS sync byte is 188 bytes apart. This is # probably Transport Stream, but the possibility exists that it is a # coincidence. # # the original file will be saved with a .tsfixer extension. If the new # file doesn't work, replace it with the orignal. # # Usage: ./tsfixer.pl /path/to/file/file.nuv $inputfile = $ARGV[0]; open TSfile, "$inputfile" or die "Can't open $inputfile: $!\n"; binmode TSfile; $i = 0; $not_ts = 1; $do_it = 0; while (($i < 100000) && ($not_ts == 1)) { $start_pos = tell(TSfile); read(TSfile, $buf1, 1); $buf_u = unpack('C*', $buf1); if ($buf_u == 71){ seek(TSfile, $start_pos +188, 0); read(TSfile, $buf2, 1); $buf_u2 = unpack('C*', $buf2); seek(TSfile, $start_pos +376, 0); read(TSfile, $buf3, 1); $buf_u3 = unpack('C*', $buf3); seek(TSfile, $start_pos +564, 0); read(TSfile, $buf4, 1); $buf_u4 = unpack('C*', $buf4); if(($buf_u2 == 71) && ($buf_u3 == 71) && ($buf_u4 == 71) && ($start_pos != 1)){ $not_ts = 0; printf ("found TS at $i\n"); if ($start_pos == 0){ printf ("this file doesn't need fixing.\n"); }else{ $do_it = 1; } } }else { $i++; } } close TSfile; if($do_it == 1){ $outputfile = $inputfile; $moved_inputfile = "$inputfile" . ".tsfixer"; rename $inputfile, $moved_inputfile or die "Can't rename $inputfile: $!\n"; open TSfile, "$moved_inputfile" or die "Can't open $moved_inputfile: $!\n"; binmode TSfile; seek(TSfile, $start_pos, 0); open TSoutfile, "> $outputfile" or die "Can't open $outputfile: $!\n"; binmode TSoutfile; while (){ print TSoutfile; } } close TSfile; close TSoutfile;