Last active
November 20, 2016 09:33
-
-
Save m00shm00sh/dfcd23663936cf3dffa6 to your computer and use it in GitHub Desktop.
Transcoder *->m4a jig
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 threads; | |
use sigtrap qw(die INT QUIT); | |
use File::Basename; | |
use File::Path qw/make_path/; | |
use Data::Dumper; | |
use POSIX; | |
use Thread::Queue; | |
local @_; | |
while (<>){ | |
chomp; | |
push @_, $_ if $_; | |
} | |
our $N = scalar @_; | |
exit unless $N > 0; | |
sub lg { | |
my $n = shift; | |
return log($n)/log(10); | |
} | |
sub compose { | |
my $f = shift; | |
my $g = shift; | |
die unless ref $f eq 'CODE'; | |
die unless ref $g eq 'CODE'; | |
return sub { | |
return $f->($g->(@_)); | |
} | |
}; | |
our ($nthr, $pn, $tn, @workers, $wq); | |
$nthr = $N > 8 ? 8 : $N; | |
my $clg = compose(\&POSIX::ceil, \&lg); | |
$pn = $clg->(do "/proc/sys/kernel/pid_max"); | |
$tn = $clg->($nthr); | |
our $do_jpg = $ENV{DO_JPG} // 1; | |
our $skip_m4a = $ENV{SKIP_M4A} // undef; | |
@workers = (); | |
$wq = Thread::Queue->new(); | |
for (my $i = 0; $i < $nthr; ++$i) { | |
push @workers, async { | |
my $th = sprintf("%0".$pn."d_%0".$tn."d", $$, $i); | |
while (local $_ = $wq->dequeue) { | |
my ($S, $D, $SF, $DF); | |
$S=$_; | |
$D="../SS/".$S; | |
make_path(dirname $D); | |
if ($D =~ /\.m4a$/ and $skip_m4a) { | |
system('cp', $S, $D) == 0 or die "cp.m4a \"$S\" \"$D\""; | |
next; | |
} | |
$SF="/tmp/AEAE_$th"; | |
$DF="/tmp/AEAF_$th.m4a"; | |
if ($D =~ /\.(?:jpe?g|png)$/i) { | |
next unless $do_jpg; | |
system('cp', $S, $D) == 0 or die "cp.jpg \"$S\" \"$D\""; | |
next; | |
} | |
$D =~ s/\.[^.]+$/.m4a/; | |
system('cp', $S, $SF) == 0 or die "cp"; | |
system(qw/ffmpeg -i/, $SF, qw/-vn -c:a libfdk_aac -vbr 3 -y/, $DF) == 0 or die "ffmpeg (\"$S\") (\"$D\")"; | |
system('cp', $DF, $D) == 0 or die "cp"; | |
unlink $SF; | |
unlink $DF; | |
} | |
}; | |
} | |
for (@_) { | |
$wq->enqueue($_); | |
} | |
$wq->end(); | |
$_->join() for (@workers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment