Cleaned up duration variable names
[videoscripts/.git] / make_mkv
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Take a list of video files and create a single mkv video file from them
4
5 # Pre-requisites:
6 # mkvtoolnix - http://www.bunkus.org/videotools/mkvtoolnix/
7 # ffmpeg
8
9 ####################################################################################################
10 # Includes
11 use File::Copy;
12 use File::Basename;
13 use Getopt::Std;
14 use File::stat;
15 use DateTime;
16 use DateTime::Duration;
17 use DateTime::Format::Duration;
18
19 ####################################################################################################
20 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
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";
27 ####################################################################################################
28
29
30 ####################################################################################################
31 # Command Line Options
32 getopts("st:o:i:h");
33
34 if(! -x $mkvmerge) { die "-E- Unable to find required program: mkvmerge\n"; }
35 if(! -x $ffmpeg) { die "-E- Unable to find required program: ffmpeg\n"; }
36 if(! defined $opt_t) { &usage(); die "-E- Missing required title: -t <title>\n"; }
37 if(! defined $opt_o) { &usage(); die "-E- Missing required argument output video names: -o <output.mkv>\n"; }
38 if(! defined $opt_i) { &usage(); die "-E- Missing required argument input video names: -i <input,input,...>\n"; }
39
40 sub usage {
41     print "usage: $0 -t <title> -o <output.mkv> -i <input,input,...>\n";
42     print "  -t <title>            Sets the general title for the output video file\n";
43     print "  -o <output.mkv>       Sets the name of the output mkv file\n";
44     print "  -i <input,input,...>  Sets the name of the input files to make into an mkv file\n";
45     print "  -s                    Simulate mode. Don't actually make the video, but tell us what you will do\n";
46     print "\n";
47     return 1;
48 }
49
50 ####################################################################################################
51 # Helper Subroutines
52 sub epoch_to_date {
53     my ($epoch) = @_;
54     my $mtime = DateTime->from_epoch(epoch => $epoch);
55     return sprintf("%4d",$mtime->year)."-".sprintf("%02d",$mtime->month)."-".sprintf("%02d",$mtime->day)." ".$mtime->hms;
56 }
57
58 ####################################################################################################
59 # MAIN
60
61 # Turn the list of input videos into a hash with a value equal to the modification time in epoch seconds
62 my %videos;
63 foreach $video (split(/,/, $opt_i)) {
64     if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; }
65     my $mtime_epoch = stat("$video")->mtime;
66     my $mdate = epoch_to_date($mtime_epoch);
67     $videos{$video} = $mtime_epoch;
68 }
69
70 # Create the chapters file for the mkv file.
71 # Make each video file it's own chapter in the MKV file.
72 # Name the chapter with the date and time the video clip was taken (modified date).
73 print "-> Creating $opt_o with title '$opt_t' from the following video files in last modified date order:\n";
74 open(CHAPTERS,">$chapter_file") || die "-E- Unable to create chapter file: $chapter_file\n";
75 my $chapter_num = 0;
76 my $chapter_timecode = DateTime::Duration->new(years => 1900, hours => 0, minutes => 0, seconds => 0);
77 my $timecode_format = DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N', normalize => 1);
78 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
79     $chapter_num++;
80     my $hour = 0;
81     my $min  = 5;
82     my $sec  = 0;
83     my $mtime_epoch = stat("$video")->mtime;
84     my $mdate = epoch_to_date($mtime_epoch);
85     my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`;
86     if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) {
87         $hour = $1;
88         $min = $2;
89         $sec = "$3.$4";
90     }
91     my $timecode = $timecode_format->format_duration($chapter_timecode);
92     print "$mdate $hour:$min:$sec  -> $video \n";
93     print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."=".$timecode."\n";
94     print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."NAME=".$mdate."\n";
95     my $dt = DateTime::Duration->new(years => 1900, hours => $hour, minutes => $min, seconds => $sec);
96     $chapter_timecode = $chapter_timecode + $dt;
97 }
98 close(CHAPTERS);
99 print "\n-> Creating the following chapter file for this video:\n";
100 $chapter_file_contents = `cat $chapter_file`;
101 print "$chapter_file_contents\n";
102
103 # Create the mkv video
104 print "-> Creating the MKV video file '$opt_o'\n";
105
106 my $cmd = "$mkvmerge --title \"$opt_t\" $output_file_options -o \"$opt_o\"";
107 my $video_count = 0;
108 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
109     if($video_count == 0) {
110         $cmd .= " $input_file_options \"$video\"";
111     } else {
112         $cmd .= " $input_file_options + \"$video\"";
113     }
114     $video_count++;
115 }
116 print "$cmd\n";
117 if(! defined $opt_s) { 
118     my $errno = system("$cmd");
119     if($errno > 0) { $errno = $errno - 255; }
120     if($errno > 1) { die "-E- mkvmerge encountered some errors with exit code $errno\n"; }
121 }
122
123 # Remove the temporary file used for the chapter generation
124 if(-e "$tmpfile") { unlink "$tmpfile"; }
125
126 ####################################################################################################
127