#!/usr/bin/perl -w # # Read the .keep file and print an ffmpeg command to stdout that will # split the master .mpg about 30 seconds before each keepable segment # Also have it write a .csv file with the info about exactly where the # splits happened, so later I can seek to the precise location. # use strict; # Get PATH set to include this script's directory and other useful bits my $newpath=`dirname $0`; chomp($newpath); $newpath=`$newpath/echo-path`; chomp($newpath); $ENV{'PATH'}=$newpath; # Arg 1 should be the .mpg file to be split, and arg 2 the .keep file # with the times of segments. if (scalar(@ARGV) != 2) { die "usage: mpgfile keepfile\n"; } my $keepfile = $ARGV[1]; my $infile = $ARGV[0]; my $basefile=`basename $infile .mpg`; chomp($basefile); # Slightly complicated here. If I have really short segments or commercial # breaks I might get confused unless I check to see that all the segment # times are indeed increasing and none of the segment split times fall # inside any of the previous segments I want to keep. I want all the # material I wish to keep to be inside one and only one segment. my $kh; open($kh, '<', $keepfile) || die "Cannot read $keepfile\n"; my @segs; my @times; my $lastime; while (<$kh>) { chomp; my @seg = split(' ',$_); my $t = $seg[0] - 30.0; if ($t < 0.0) { $t = 0.0; } my $tv = $t; if (defined($lastime) && ($t < $lastime)) { $tv = -1; } else { my $s; foreach $s (@segs) { if (($tv >= $s->[1]) && ($tv < $s->[2])) { $tv = -1; last; } } } push(@segs,[$tv,$seg[0],$seg[1]]); if ($tv != -1) { $lastime = $tv; push(@times,$tv); } } print "ffmpeg -i \"" . $infile . "\" -codec copy -map 0 -f segment -segment_list \"" . "$basefile.csv" . "\" -segment_times " . join(',',@times) . " \"" . $basefile . "-part%03d.mpg\"\n";