#!/usr/bin/perl -CADS # for all MKV files containing at least one FLAC audio track, # replace all audio tracks with an MP3 version of the first FLAC # track found. # # requires flac, mkvtoolnix, and lame use strict; mkdir("out"); foreach my $infile (@ARGV) { open(In,"-|","mkvinfo",$infile) or die "$0: $infile: $!\n"; my ($track,$lang,$name); my ($hasflac,$intracks); while () { if (/^.\+ Segment tracks/) { $intracks = 1; }elsif (/^.\+/) { $intracks = 0; last if $hasflac == 1; } next unless $intracks; if (/^. \+ A track/) { last if $hasflac == 1; ($hasflac,$track,$lang,$name) = (); }elsif (/^. \+ Track number: (\d+)/) { $track = $1; }elsif (/^. \+ Codec ID: A_FLAC/) { $hasflac = 1; }elsif (/^. \+ Language: (\S+)/) { $lang = $1; }elsif (/^. \+ Name: (.*)$/) { $name = $1; chomp($name); } } close(In); if ($hasflac != 1) { print "$infile: no FLAC audio track, skipping\n"; next; } print join(" ",$infile,$track,$lang,$name),"\n"; system("mkvextract","tracks",$infile,"$track:mp3audio.flac"); system("flac -c -d mp3audio.flac | lame --alt-preset standard - mp3audio.mp3"); my $outfile = $infile; $outfile =~ s|.*/||; system("mkvmerge","-o","out/$outfile","-A",$infile, "--language","0:$lang","--track-name","0:$name","mp3audio.mp3"); } unlink("mp3audio.mp3","mp3audio.flac"); exit 0;