Created
October 17, 2020 12:45
-
-
Save ggl/d436c3315e36a9d205cf29976db860ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Capture::Tiny; | |
use File::Which; | |
use Getopt::Std; | |
use Path::Tiny; | |
my $path = $ARGV[0] or HELP_MESSAGE(); | |
my %opt; | |
Getopt::Std::getopt('b:', \%opt); | |
my $metaflac = File::Which::which('metaflac') or die "Cannot find metaflac\n"; | |
my $opusenc = File::Which::which('opusenc') or die "Cannot find opusenc\n"; | |
my @files; | |
if (-d $path) { | |
@files = Path::Tiny->new($path)->children(qr/\.flac/); | |
} | |
elsif (-f $path) { | |
@files = Path::Tiny->new($path); | |
}; | |
foreach my $file (@files) { | |
encode($file); | |
}; | |
sub encode { | |
my $file_in = shift; | |
if (-f $file_in) { | |
my ($stdout) = Capture::Tiny::capture { | |
system($metaflac, '--export-tags=-', $file_in); | |
}; | |
my %tags; | |
foreach my $ln (split("\n", $stdout // '')) { | |
my ($key, $val) = split("=", $ln, 2); | |
next if $key !~ /^(title|artist|album|genre|date)$/i; | |
$tags{'--'.lc($key)} = $val; | |
}; | |
my $file_out = $file_in; | |
$file_out =~ s/\.flac/\.opus/; | |
system($opusenc, '--bitrate', $opt{'b'} // 160, %tags, $file_in, $file_out); | |
}; | |
} | |
sub HELP_MESSAGE { | |
print qq/$0 [-b <bitrate>] <path|files>\n/; | |
exit 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment