X-Git-Url: http://git.pippins.net/embedvideo/.git/static/gitweb.js?a=blobdiff_plain;f=make_mkv;h=0ad7534b8fef4408f46960faa1691ec6e30bac20;hb=cb660ed1efe9806045d753d02e5c0b656919d639;hp=c7690041400776d700aa323143d5d695280d9337;hpb=eb8eb0bd100a288298370362135991ad4710cc59;p=videoscripts%2F.git diff --git a/make_mkv b/make_mkv index c769004..0ad7534 100755 --- a/make_mkv +++ b/make_mkv @@ -17,6 +17,11 @@ use Data::Dumper; use DateTime; use DateTime::Duration; use DateTime::Format::Duration; +use DateTime::Format::Strptime qw( ); + +# Set our ffmpeg creation_time format +$ffmpeg_time_format_utc = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak'); +$ffmpeg_time_format_local = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'local', on_error => 'croak'); #################################################################################################### # Configuration parameters @@ -66,7 +71,19 @@ sub epoch_to_date { my %videos; foreach $video (split(/,/, $opt_i)) { if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; } - my $mtime_epoch = stat("$video")->mtime; + my $mtime_epoch = 0; + my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`; + my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands); + if($creation_time) { + my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time); + if ($brands && $brands =~ /$local_tz_brands/) { + $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time); + } + $date_taken->set_time_zone('local'); + $mtime_epoch = $date_taken->epoch; + } else { + $mtime_epoch = stat("$video")->mtime; + } my $mdate = epoch_to_date($mtime_epoch); $videos{$video} = $mtime_epoch; } @@ -191,6 +208,10 @@ foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) { unlink "$video_mp4"; die "-E- handbrake encountered some errors with exit code $errno\n"; } + # Restore the metadata from the original video to the intermediate video to preserve the creation_time of the original video metadata in it + `$ffmpeg -i \"$video\" -i \"$video_mp4\" -map 1 -map_metadata 0 -c copy -movflags use_metadata_tags \"$video_mp4.fixed.mp4\" >> \"$tmpfile\" 2>&1`; + $errno=system("mv \"$video_mp4.fixed.mp4\" \"$video_mp4\" 2>/dev/null"); + if($errno) { print "-E- Error moving fixed metadata video back to dstfile: $video_mp4.fixed.mp4 -> $video_mp4\n"; } } } else { if($no_recompress) { @@ -218,6 +239,12 @@ foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) { if($video_stream_info =~ / (\d+x\d+)[,| ]/) { $dimensions = "$1"; } else { print "-W- ffprobe was unable to find dimensions for video: $video\n"; } + # Video Codec + my $video_stream_info = `$ffprobe "$video" 2>&1 | grep -e "Stream.*Video"`; chomp($video_stream_info); + my $video_codec = "unknown"; + if($video_stream_info =~ / Video: (\S+) /) { $video_codec = "$1"; } + else { print "-W- ffprobe was unable to find video codec for video: $video\n"; } + # Color space my $color_space = "unknown"; if($video_stream_info =~ /, (\S+)\(.*?\)/) { $color_space = "$1"; } @@ -235,7 +262,7 @@ foreach my $video (sort{$videos{$a} <=> $videos{$b}} keys %videos) { else { print "-W- ffprobe was unable to find audio codec for video: $video\n"; } # Now create our parameters string - my $parameters = "$dimensions.$color_space.$audio_handler.$audio_codec"; + my $parameters = "$dimensions.$video_codec.$color_space.$audio_handler.$audio_codec"; print " Adding video $video to be merged into output video file: $opt_o.$parameters.mkv\n" if($opt_v); push @{$merge_videos{"$parameters"}}, $video; @@ -267,7 +294,19 @@ foreach my $key (keys %merge_videos) { my $hour = 0; my $min = 5; my $sec = 0; - my $mtime_epoch = stat("$video")->mtime; + my $mtime_epoch = 0; + my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`; + my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands); + if($creation_time) { + my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time); + if ($brands && $brands =~ /$local_tz_brands/) { + $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time); + } + $date_taken->set_time_zone('local'); + $mtime_epoch = $date_taken->epoch; + } else { + $mtime_epoch = stat("$video")->mtime; + } my $mdate = epoch_to_date($mtime_epoch); my $duration = `$ffmpeg -i "$video" 2>&1 | grep Duration`; if($duration =~ /Duration: (\d+):(\d+):(\d+)\.(\d+)/) { @@ -303,7 +342,7 @@ foreach my $key (keys %merge_videos) { } print "\n$merge_cmd\n"; if(! defined $opt_s) { - if(-f "$opt_o.$key.mkv") { die "-E- Output video file $opt_o already exists. This is unexpected for key $key!"; } + #if(-f "$opt_o.$key.mkv") { die "-E- Output video file $opt_o already exists. This is unexpected for key $key!"; } my $errno = system("$merge_cmd"); $errno = $errno >> 8; if($errno > 1) {