-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2opus
executable file
·57 lines (44 loc) · 1.46 KB
/
convert2opus
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
#!/usr/bin/perl
#
# Usage: convert2opus [OPTIONS] FILE [FILE...]
# OPTIONS are passed to opusenc
# FILE can be anything with metadata
# so long as a .wav of the same name also exists (e.g. foo.mp3 and foo.wav).
use strict;
use warnings;
my @options = ();
for my $file (@ARGV) {
if (-s $file) {
my @encode = ("opusenc", @options);
open (INFO, qq/mediainfo "$file" |/) or die $!;
while (<INFO>) {
next unless /^(\S[^:]*\S)\s+:\s+(\S.*)\s*$/;
my ($key, $value) = ($1, $2);
if ($key eq "Album") {
push (@encode, "--album", $value);
} elsif ($key eq "Track name") {
push (@encode, "--title", $value);
} elsif ($key eq "Track name/Position") {
push (@encode, "--comment", "$key=$value");
} elsif ($key eq "Performer") {
push (@encode, "--artist", $value);
} elsif ($key eq "Genre") {
push (@encode, "--genre", $value);
} elsif ($key eq "Recorded date") {
push (@encode, "--date", $value);
}
}
close INFO;
my $wav = $file;
$wav =~ s/\.[^.]+$//;
for my $test ("", qw/wav aiff flac raw dts ac3 ogg/) {
if (-s "$wav.$test") { $file = "$wav.$test"; last; }
}
my $outfile = $file;
$outfile =~ s/\.(?:wav|aiff|flac|ogg|raw)$//i;
print "\n" . "-"x60 . "\nWriting: $outfile.opus\n";
system(@encode, $file, "$outfile.opus");
} else {
push (@options, $file);
}
}