2 # Author: Alan J. Pippin
3 # Description: Take a list of video files and create a single mkv video file from them
6 # mkvtoolnix - http://www.bunkus.org/videotools/mkvtoolnix/
9 ###########################
16 use DateTime::Duration;
17 use DateTime::Format::Duration;
19 ###########################
20 # Configuration parameters
21 my $mkvmerge=`which mkvmerge`; chomp($mkvmerge);
22 my $ffmpeg=`which ffmpeg`; chomp($ffmpeg);
23 my $tmpfile = `tempfile`; chomp($tmpfile);
24 my $chapter_file = $tmpfile;
25 my $input_file_options = "-S";
26 my $output_file_options = "--chapters $chapter_file";
28 ###########################
32 if(! -x $mkvmerge) { die "-E- Unable to find required program: mkvmerge\n"; }
33 if(! -x $ffmpeg) { die "-E- Unable to find required program: ffmpeg\n"; }
34 if(! defined $opt_t) { &usage(); die "-E- Missing required title: -t <title>\n"; }
35 if(! defined $opt_o) { &usage(); die "-E- Missing required argument output video names: -o <output.mkv>\n"; }
36 if(! defined $opt_i) { &usage(); die "-E- Missing required argument input video names: -i <input,input,...>\n"; }
39 print "usage: $0 -t <title> -o <output.mkv> -i <input,input,...>\n";
40 print " -t <title> Sets the general title for the output video file\n";
41 print " -o <output.mkv> Sets the name of the output mkv file\n";
42 print " -i <input,input,...> Sets the name of the input files to make into an mkv file\n";
43 print " -s Simulate mode. Don't actually make the video, but tell us what you will do\n";
48 ###########################
52 my $mtime = DateTime->from_epoch(epoch => $epoch);
53 return sprintf("%4d",$mtime->year)."-".sprintf("%02d",$mtime->month)."-".sprintf("%02d",$mtime->day)." ".$mtime->hms;
56 ###########################
59 # Turn the list of input videos into a hash with a value equal to the modification time in epoch seconds
61 foreach $video (split(/,/, $opt_i)) {
62 if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; }
63 my $mtime_epoch = stat("$video")->mtime;
64 my $mdate = epoch_to_date($mtime_epoch);
65 $videos{$video} = $mtime_epoch;
68 # Create the chapters file for the mkv file.
69 # Make each video file it's own chapter in the MKV file.
70 # Name the chapter with the date and time the video clip was taken (modified date).
71 print "-> Creating $opt_o with title '$opt_t' from the following video files in last modified date order:\n";
72 open(CHAPTERS,">$chapter_file") || die "-E- Unable to create chapter file: $chapter_file\n";
77 my $new_duration = DateTime::Duration->new(years => 1900, hours => $prev_hour, minutes => $prev_min, seconds => $prev_sec);
78 my $duration_format = DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N', normalize => 1);
79 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
84 my $mtime_epoch = stat("$video")->mtime;
85 my $mdate = epoch_to_date($mtime_epoch);
86 my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`;
87 if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) {
92 my $start_time = $duration_format->format_duration($new_duration);
93 print "$mdate $hour:$min:$sec -> $video \n";
94 print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."=".$start_time."\n";
95 print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."NAME=".$mdate."\n";
96 my $dt = DateTime::Duration->new(years => 1900, hours => $hour, minutes => $min, seconds => $sec);
97 my $prev_dt = DateTime::Duration->new(years => 1900, hours => $prev_hour, minutes => $prev_min, seconds => $prev_sec);
98 $new_duration = $dt + $prev_dt;
99 $prev_hour = $hour; $prev_min = $min; $prev_sec = $sec;
102 print "\n-> Creating the following chapter file for this video:\n";
103 $chapter_file_contents = `cat $chapter_file`;
104 print "$chapter_file_contents\n";
106 # Create the mkv video
107 print "-> Creating the MKV video file '$opt_o'\n";
109 my $cmd = "$mkvmerge --title \"$opt_t\" $output_file_options -o \"$opt_o\"";
111 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
112 if($video_count == 0) {
113 $cmd .= " $input_file_options \"$video\"";
115 $cmd .= " $input_file_options + \"$video\"";
120 if(! defined $opt_s) {
121 my $errno = system("$cmd");
122 if($errno > 0) { $errno = $errno - 255; }
123 if($errno > 1) { die "-E- mkvmerge encountered some errors with exit code $errno\n"; }
126 # Remove the temporary file
127 if(-e "$tmpfile") { unlink "$tmpfile"; }
129 ###########################