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/
10 ####################################################################################################
18 use DateTime::Duration;
19 use DateTime::Format::Duration;
20 use DateTime::Format::Strptime qw( );
22 # Set our ffmpeg creation_time format
23 $ffmpeg_time_format = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
25 ####################################################################################################
26 # Configuration parameters
27 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
28 # Default configuration values
29 require "organize_videos.conf";
30 # Override defaults with local customizations
31 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
33 ####################################################################################################
34 # Command Line Options
35 getopts("svqzt:o:i:h");
37 if(! defined $opt_t) { &usage(); die "-E- Missing required title: -t <title>\n"; }
38 if(! defined $opt_o) { &usage(); die "-E- Missing required argument output video names: -o <output.mkv>\n"; }
39 if(! defined $opt_i) { &usage(); die "-E- Missing required argument input video names: -i <input,input,...>\n"; }
40 if(! -x $ffmpeg) { die "-E- Missing required executable for ffmpeg: $ffmpeg\n"; }
41 if(! -x $avconv) { die "-E- Missing required executable for avconv: $avconv\n"; }
44 print "usage: $0 -t <title> -o <output.mkv> -i <input,input,...>\n";
45 print " -t <title> Sets the general title for the output video file\n";
46 print " -o <output> Sets the basename of the output mkv file\n";
47 print " -i <input,input,...> Sets the names of the input files to combine into a new mkv file\n";
48 print " -q Requantize input videos to decrease output video size (requires HandBrakeCLI)\n";
49 print " -z Recompress input videos to decrease output video size (requires HandBrakeCLI)\n";
50 print " -s Simulate mode. Don't actually make the video, but tell us what you will do\n";
51 print " -v Increase verbosity for debug\n";
56 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
57 ####################################################################################################
61 my $mtime = DateTime->from_epoch(epoch => $epoch);
62 $mtime->set_time_zone($timezone);
63 return sprintf("%4d",$mtime->year)."-".sprintf("%02d",$mtime->month)."-".sprintf("%02d",$mtime->day)." ".$mtime->hms;
66 ####################################################################################################
69 # Turn the list of input videos into a hash with a value equal to the modification time in epoch seconds
71 foreach $video (split(/,/, $opt_i)) {
72 if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; }
74 my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
76 my $date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
77 $date_taken->set_time_zone('local');
78 $mtime_epoch = $date_taken->epoch;
80 $mtime_epoch = stat("$video")->mtime;
82 my $mdate = epoch_to_date($mtime_epoch);
83 $videos{$video} = $mtime_epoch;
86 ####################################################################################################
87 # 1) Create the mkv video
88 print "-> Creating MKV video files with basename '$opt_o'\n";
90 # Make our input file command line options for all the videos
93 foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) {
94 # Make a note of the video extension
95 my $video_ext = $video;
96 $video_ext =~ s/.*\.(\S+)$/$1/;
98 # Detect if the input file is interlaced or not.
99 # There is a bug in mkvmerge 5.0.1 and earlier where if the video content is 1080i, it doesn't mux it properly, and it stutters.
100 # The quick solution to this issue is to run the interlaced video file through ffmpeg and tell it to copy the video/audio streams to a mkv container.
101 # We will then merge this temporarily created mkv container into the final mkv container instead of the original interlaced video.
102 # http://ubuntuforums.org/showthread.php?t=1627194
103 # http://forum.doom9.org/showthread.php?t=155732&page=31
105 # This is the best way to detect if content is interlaced or progressive:
106 # http://www.aktau.be/2013/09/22/detecting-interlaced-video-with-ffmpeg/
108 my $detect_cmd = "$ffmpeg -filter:v idet -frames:v 100 -an -f rawvideo -y /dev/null -i \"$video\" 2>&1 | grep Parsed_idet";
109 if($opt_v) { print " $detect_cmd\n"; }
110 my $detect_output = `$detect_cmd`;
111 if($detect_output !~ /Progressive:0/) { $progressive = 1; }
113 my $video_mkv = $video;
114 print " Detected interlaced video content: $video\n";
115 # We don't need to do this anymore since it is not an issue with the new mkvmerge
116 if($video_ext !~ /mkv/i) {
117 $video_mkv =~ s/\.[^.]*$//; $video_mkv .= ".ffmpeg.mkv";
118 if(-f $video_mkv) { next; }
119 print " Re-muxing the interlaced video content as an mkv file: $video_mkv\n";
120 my $make_mkv_cmd = "$avconv -y -i \"$video\" -scodec copy -acodec copy -vcodec copy -f matroska \"$video_mkv\" >> \"$tmpfile\" 2>&1";
122 if($opt_v) { print " $make_mkv_cmd\n"; }
123 if(! defined $opt_s) {
124 my $errno = system("$make_mkv_cmd");
125 $errno = $errno >> 8;
128 print "-W- avconv encountered some errors with exit code $errno\n";
133 # Push the name of our intermediate video file onto a list of files to delete on exit
134 push(@video_tmp_files, "$video_mkv");
135 # Copy the modification date from the src video to the dst video
136 system("touch \"$video_mkv\" -r \"$video\"");
137 # Update the name of our video file to equal the name of our new intermediate video file name
142 print " Detected progressive video content: $video\n";
145 # Re-quantize or re-compress the input video to reduce the resulting output filesize
146 # This also gives us a chance to deinterlace the video as well
147 # Only re-quantize for .MTS videos
148 # We can re-compress any input video
149 my $rotated = "none";
150 if(((defined $opt_q) && ($video_ext =~ /mts/i)) || (defined $opt_z)) {
152 my $handbrake_options = "";
154 # Set our output video filename
155 my $video_mp4 = $video; $video_mp4 =~ s/\.[^.]*$//; $video_mp4 .= ".hb.mp4";
157 # Don't recompress certain file extensions
158 # Unless the video is not progressive or is rotated, then we do need to recompress it to deinterlace or unrotate it
159 my $no_recompress = 0;
160 if($no_recompress_file_ext && $progressive && ($video =~ $no_recompress_file_ext)) { $no_recompress = 1; }
162 # Set our rotate option accordingly to rotate videos taken in portrait mode to landscape
163 my $rotate = `$ffprobe "$video" 2>&1 | grep -e rotate | awk '{print \$3}'`; chomp($rotate);
165 print " Detected rotated input video ($rotate). Rotating video to: $video_mp4\n";
166 if($rotate eq "90") { $handbrake_options .= " --rotate=4"; $rotated = "90"; }
167 if($rotate eq "180") { $handbrake_options .= " --rotate=3"; $rotated = "none"; }
168 $no_recompress = 0; # We have to recompress this video to unrotate it
171 # Set our requantize factor accordingly
172 if(defined $opt_q and ! -f $video_mp4) {
173 print " Re-quantizing input video content to: $video_mp4\n";
174 $handbrake_options = $handbrake_requantize_options;
175 if(!$progressive) { $handbrake_options = "-q $interlaced_requantize_quality"; }
176 else { $handbrake_options = "-q $progressive_requantize_quality"; }
179 # Set our recompress options accordingly
180 if(defined $opt_z and ! -f $video_mp4 and ! $no_recompress) {
181 print " Re-compressing input video content to: $video_mp4\n";
182 $handbrake_options = $handbrake_recompress_options;
183 # We want our audio to be passed-through by default, so detect how the audio of the input is encoded, and tell handbrake to make the output match
184 if($opt_v) { print " $ffmpeg -i \"$video\" 2>&1 | grep \"Audio\" | sed -r -e 's/.*?Audio: (\\S+).*?/\\1/' | tail -n 1\n"; }
185 $AUDIO_CODEC=`$ffmpeg -i "$video" 2>&1 | grep "Audio" | sed -r -e 's/.*?Audio: (\\S+).*?/\\1/' | tail -n 1`; chomp($AUDIO_CODEC);
186 $AUDIO_CODEC =~ s/,$//g;
187 if($AUDIO_CODEC eq "") { print "-W- Unable to extract audio track encoding from input video file: $video\n"; $handbrake_options .= " -E copy"; }
188 else { $handbrake_options .= " -E copy:$AUDIO_CODEC"; }
191 # Set our de-interlace option accordingly
192 my $deinterlace_option = "";
193 if(!$progressive) { $deinterlace_option = "-d"; }
195 # Use HandBrake to requantize/recompress/deinterlace the input video
196 if(! $no_recompress and ! -f $video_mp4) {
197 my $handbrake_cmd = "$handbrake $deinterlace_option $handbrake_options -i \"$video\" -o \"$video_mp4\" >> \"$tmpfile\" 2>&1";
198 if($opt_v) { print " $handbrake_cmd\n"; }
199 if(! defined $opt_s and ! -f $video_mp4) {
200 my $errno = system("$handbrake_cmd");
201 $errno = $errno >> 8;
204 die "-E- handbrake encountered some errors with exit code $errno\n";
206 # Restore the metadata from the original video to the intermediate video to preserve the creation_time of the original video metadata in it
207 `$ffmpeg -i \"$video\" -i \"$video_mp4\" -map 1 -map_metadata 0 -c copy -movflags use_metadata_tags \"$video_mp4.fixed.mp4\" >> \"$tmpfile\" 2>&1`;
208 $errno=system("mv \"$video_mp4.fixed.mp4\" \"$video_mp4\" 2>/dev/null");
209 if($errno) { print "-E- Error moving fixed metadata video back to dstfile: $video_mp4.fixed.mp4 -> $video_mp4\n"; }
213 print " Copying input video content to: $video_mp4\n";
214 system("cp \"$video\" \"$video_mp4\"") if(!$opt_s);
218 # Push the name of our intermediate video file onto a list of files to delete on exit
219 push(@video_tmp_files, "$video_mp4");
220 # Copy the modification date from the src video to the dst video
221 system("touch \"$video_mp4\" -r \"$video\"");
222 # Update the name of our video file to equal the name of our new intermediate video file name
227 ####################################################################################################
228 # 2) Group the encoded videos together by their parameters
229 # We can merge videos that have the same parameters together in a destination video file.
232 my $video_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Video"`; chomp($video_stream_info);
233 my $dimensions = "unknown";
234 if($video_stream_info =~ / (\d+x\d+)[,| ]/) { $dimensions = "$1"; }
235 else { print "-W- ffprobe was unable to find dimensions for video: $video\n"; }
238 my $video_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Video"`; chomp($video_stream_info);
239 my $video_codec = "unknown";
240 if($video_stream_info =~ / Video: (\S+) /) { $video_codec = "$1"; }
241 else { print "-W- ffprobe was unable to find video codec for video: $video\n"; }
244 my $color_space = "unknown";
245 if($video_stream_info =~ /, (\S+)\(.*?\)/) { $color_space = "$1"; }
246 else { print "-W- ffprobe was unable to find color space for video: $video\n"; }
249 my $audio_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Audio"`; chomp($video_stream_info);
250 my $audio_handler = "unknown";
251 if($audio_stream_info =~ / Hz, ([^,]+), /) { $audio_handler = "$1"; }
252 else { print "-W- ffprobe was unable to find audio handler for video: $video\n"; }
255 my $audio_codec = "unknown";
256 if($audio_stream_info =~ /Audio: (\S+) /) { $audio_codec = "$1"; }
257 else { print "-W- ffprobe was unable to find audio codec for video: $video\n"; }
259 # Now create our parameters string
260 my $parameters = "$dimensions.$video_codec.$color_space.$audio_handler.$audio_codec";
262 print " Adding video $video to be merged into output video file: $opt_o.$parameters.mkv\n" if($opt_v);
263 push @{$merge_videos{"$parameters"}}, $video;
267 print "\n-> Merge videos hash:\n";
268 print Dumper(\%merge_videos);
272 ####################################################################################################
273 # 3) Create a chapter file for each destination video file
275 # tmp chapter file used by handbrake when creating mkv, but remove the 0 byte file it creates, we'll create it if we need it
276 my $chapter_file = `tempfile`; chomp($chapter_file); unlink "$chapter_file";
278 foreach my $key (keys %merge_videos) {
279 # Create the chapters file for each output mkv file.
280 # Make each video file it's own chapter in the MKV file.
281 # Name the chapter with the date and time the video clip was taken (modified date).
282 print "-> Creating $opt_o.$key.mkv with title '$opt_t' from the following video files in last modified date order:\n";
283 open(CHAPTERS,">$chapter_file.$key") || die "-E- Unable to create chapter file: $chapter_file.$key\n";
285 my $chapter_timecode = DateTime::Duration->new(years => 2000, hours => 0, minutes => 0, seconds => 0);
286 my $timecode_format = DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N', normalize => 1);
287 foreach my $video (@{$merge_videos{$key}}) {
293 my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
295 my $date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
296 $date_taken->set_time_zone('local');
297 $mtime_epoch = $date_taken->epoch;
299 $mtime_epoch = stat("$video")->mtime;
301 my $mdate = epoch_to_date($mtime_epoch);
302 my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`;
303 if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) {
308 my $timecode = $timecode_format->format_duration($chapter_timecode);
309 print "$mdate $hour:$min:$sec -> $video \n";
310 print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."=".$timecode."\n";
311 print CHAPTERS "CHAPTER".sprintf("%02d",$chapter_num)."NAME=".$mdate."\n";
312 my $dt = DateTime::Duration->new(years => 2000, hours => $hour, minutes => $min, seconds => $sec);
313 $chapter_timecode = $chapter_timecode + $dt;
316 print "\n-> Creating the following chapter file $chapter_file.$key for this video:\n";
317 $chapter_file_contents = `cat $chapter_file.$key`;
318 print "$chapter_file_contents\n";
321 ####################################################################################################
322 # 4) Run mkvmerge to merge all of the videos together of the same parameters
323 foreach my $key (keys %merge_videos) {
324 my $merge_cmd = "$mkvmerge --title \"$opt_t\" $output_file_options --chapters $chapter_file.$key -o \"$opt_o.$key.mkv\"";
326 foreach my $video (@{$merge_videos{$key}}) {
328 $merge_cmd .= " $input_file_options \"$video\"";
330 $merge_cmd .= " $input_file_options + \"$video\"";
334 print "\n$merge_cmd\n";
335 if(! defined $opt_s) {
336 #if(-f "$opt_o.$key.mkv") { die "-E- Output video file $opt_o already exists. This is unexpected for key $key!"; }
337 my $errno = system("$merge_cmd");
338 $errno = $errno >> 8;
340 unlink "$opt_o.$key.mkv";
341 die "-E- mkvmerge encountered some errors with exit code $errno\n";
346 ####################################################################################################
347 # 5) Remove all temporary files
349 # Remove the temporary file used for ffmpeg and handbrake output
350 if(-e "$tmpfile") { unlink "$tmpfile"; }
352 # Remove the temporary file used for the chapter generation
353 foreach my $key (keys %merge_videos) {
354 if(-e "$chapter_file.$key") { unlink "$chapter_file.$key"; }
357 # Remove any temporary video files created during the merge process
358 foreach my $video (@video_tmp_files) {
359 if(-e "$video") { unlink "$video"; }
363 ####################################################################################################