Skip to content

Instantly share code, notes, and snippets.

@oylenshpeegul
Created June 10, 2021 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oylenshpeegul/16d2a590e0ccfba91c3740fe2489465d to your computer and use it in GitHub Desktop.
Save oylenshpeegul/16d2a590e0ccfba91c3740fe2489465d to your computer and use it in GitHub Desktop.
Convert .opus and .m4a files to .mp3
#!/usr/bin/env perl
use v5.28;
use warnings;
use Path::Tiny;
use String::ShellQuote;
my $dir = path(shift // '..');
# Convert the .opus files by piping to opusdec first.
# This creates a WAV, which sox will convert to MP3.
#
for my $path ($dir->children(qr/\.opus\z/)) {
say $path;
my $infile = shell_quote $path;
# say $infile;
my $outfile = $path->basename =~ s/\.opus$/.mp3/r;
# say $outfile;
my @cmd = ('sox', "|opusdec --force-wav $infile -", $outfile);
system(@cmd) == 0 or die "system '@cmd' failed: $?";
}
# Convert the .m4a files with ffmpeg and LAME.
#
for my $path ($dir->children(qr/\.m4a\z/)) {
say $path;
my $infile = shell_quote $path;
# say $infile;
my $outfile = shell_quote($path->basename =~ s/\.m4a$/.mp3/r);
# say $outfile;
my $cmd = "ffmpeg -v 5 -y -i $infile -acodec libmp3lame -ac 2 -ab 192k $outfile";
system($cmd) == 0
or die "system '$cmd' failed: $?";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment