-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmkv_audio_to_opus.pl
74 lines (57 loc) · 1.67 KB
/
mkv_audio_to_opus.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl
# Convert MKV audio files to OPUS files, in a given directory (and its subdirectories).
# Requires `ffmpeg` and `exiftool`.
use 5.036;
use File::Find qw(find);
use File::Temp qw(mktemp);
use File::Copy qw(move);
use File::Basename qw(dirname basename);
use File::Spec::Functions qw(catfile);
use Getopt::Long qw(GetOptions);
my $bitrate = 96;
sub usage ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [files | directories]
options:
-b --bitrate=i : output bitrate in kbps (default: $bitrate)
-h --help : display this message and exit
EOT
exit($exit_code);
}
GetOptions('b|bitrate=i' => \$bitrate,
'h|help' => sub { usage(0) },)
or die("Error in command line arguments");
sub is_mkv_audio ($file) {
my $res = `exiftool \Q$file\E`;
$? == 0 or return;
defined($res) or return;
$res =~ m{^MIME\s+Type\s*:\s*audio/x-matroska}mi;
}
sub convert ($file) {
my $tmpfile = mktemp("tempXXXXXXXXXXX") . '.opus';
say ":: Temporary file: $tmpfile";
system("ffmpeg", '-loglevel', 'warning', "-i", $file, "-b:a", $bitrate . "K", $tmpfile);
$? == 0 or do {
unlink($tmpfile);
return;
};
my $dir = dirname($file);
my $basename = basename($file) =~ s{\.\w+\z}{.opus}r;
my $new_file = catfile($dir, $basename);
unlink($file) or return;
say ":: Moving: $tmpfile -> $new_file";
move($tmpfile, $new_file);
}
my @dirs = @ARGV;
@dirs || usage(1);
find(
{
wanted => sub {
if (-f $_ and is_mkv_audio($_)) {
say ":: Converting: $_";
convert($_);
}
},
},
@dirs
);