Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[videoscripts/.git] / mkv_extract_chapter
index b70a7501db76507225bf6e638ff1ebd970be1be1..273484b82c7ed0cb299e39d8f3ff5303e5fe36bd 100755 (executable)
@@ -39,7 +39,7 @@ use Getopt::Std;
 ####################################################################################################
 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
 my $ffmpeg=`which ffmpeg`; chomp($ffmpeg);
-my $tmpfile = `tempfile`; chomp($tmpfile);
+my $tmpfile = `mktemp`; chomp($tmpfile);
 ####################################################################################################
 
 ####################################################################################################
@@ -73,7 +73,7 @@ sub usage {
 # SUBROUTINES
 
 sub detect_ext {
-    my ($ffmpeg_info, $interlaced) = @_;
+    my ($ffmpeg_info, $progressive) = @_;
 
     my $h264 = 0;
     my $h264_high = 0;
@@ -94,9 +94,9 @@ sub detect_ext {
     # Quicktime/MOV
     if($h264 && $pcm_s16le) { return "mov"; }
     # MTS
-    if($h264_high && $ac3) { if($interlaced) { return "mkv"; } else { return "mp4"; } }
+    if($h264_high && $ac3) { if(!$progressive) { return "mkv"; } else { return "mp4"; } }
     # 3GP/MP4
-    if($h264 && $aac) { if($interlaced) { return "mkv"; } else { return "mp4"; } }
+    if($h264 && $aac) { if(!$progressive) { return "mkv"; } else { return "mp4"; } }
 
     return "UNKNOWN";
 }
@@ -118,8 +118,8 @@ my $all_chapters = 0;
 if($opt_c =~ /all/) { $all_chapters = 1; }
 print "-> Extracting the following chapters from $opt_i: @chapters\n";
 
-my $interlaced = system('ffmpeg -i "$opt_i" 2>&1 | grep -q "frame rate differs"');
-if($interlaced != 0) {
+my $progressive = system('ffmpeg -i "$opt_i" 2>&1 | grep -q "frame rate differs"');
+if(!$progressive) {
     print "   Detected interlaced video content\n";
 } else {
     print "   Detected progressive video content\n";
@@ -129,7 +129,7 @@ if($interlaced != 0) {
 # For each chapter specified on the command line, use ffmpeg to extract a video clip from that chapter
 my @ffmpeg_info = `$ffmpeg -i $opt_i 2>&1`;
 foreach $line (@ffmpeg_info) {
-    if($line =~ /Chapter #\d+\.(\d+): start (\S+), end (\S+)/) {
+    if($line =~ /Chapter #\d+\:(\d+): start (\S+), end (\S+)/) {
        $chapter = $1;
        $start = $2;
         if($start > 0) { $start += 1; } # Add some margin to prevent taking a piece of the previous clip
@@ -139,7 +139,7 @@ foreach $line (@ffmpeg_info) {
        if($all_chapters || &export_chapter(\@chapters,$chapter)) {
            $ext = "UNKNOWN"; 
            if(defined $opt_e) { $ext = $opt_e; }
-           else { $ext = &detect_ext(\@ffmpeg_info, $interlaced); } 
+           else { $ext = &detect_ext(\@ffmpeg_info, $progressive); } 
            if($ext =~ /UNKNOWN/) { die "-E- Unable to determine the file type/extension to use for the output videos. Specify it with the -e <ext> option.\n"; }
            $dstfile = $opt_o . ".c" . sprintf("%03d",$chapter) . "." . $ext;
            print "-> Exporting $chapter to $dstfile:\n";
@@ -153,7 +153,7 @@ foreach $line (@ffmpeg_info) {
                $errno = $errno >> 8;
                if($errno > 0) { die "-E- ffmpeg encountered some errors with exit code $errno\n"; }
            }
-           if($interlaced && $ext =~ /mkv/i) {
+           if(!$progressive && $ext =~ /mkv/i) {
                $ext = "mp4";
                $srcfile = $dstfile;
                $dstfile = $dstfile = $opt_o . ".c" . sprintf("%03d",$chapter) . "." . $ext;