Skip to content

Instantly share code, notes, and snippets.

@dmikushin
Created January 25, 2020 08:11
Show Gist options
  • Save dmikushin/c08334683c64a0c4721822e23609d7a6 to your computer and use it in GitHub Desktop.
Save dmikushin/c08334683c64a0c4721822e23609d7a6 to your computer and use it in GitHub Desktop.
Batch-decode a list of *.MKV videos into *.WEBM (VP9) with deinterlace filter, using multiple parallel instances of FFmpeg
#!/usr/bin/perl -w
#
# Batch-decode a list of *.MKV videos into *.WEBM (VP9) with deinterlace filter,
# using multiple parallel instances of FFmpeg.
#
use threads;
# XXX The number of parallel threads to use.
my($nthreads) = 8;
sub thread_func
{
my($file) = shift;
my($tid) = threads->tid();
my($basename) = $file;
$basename =~ s/\.mkv$//g;
my($output) = "$basename.webm";
print "Decoding $file pass 1\n";
system("ffmpeg -y -i \"$file\" -c:v libvpx-vp9 -pass 1 -passlogfile $tid -b:v 1000K -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1 -an -f webm /dev/null");
print "Decoding $file pass 2\n";
system("ffmpeg -i \"$file\" -vf yadif -c:v libvpx-vp9 -pass 2 -passlogfile $tid -b:v 1000K -threads 8 -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -c:a libopus -b:a 96k -f webm \"$output\"");
}
my(@files) = split("\n", `find . -name "*.mkv"`);
# Keep array of threads if you want to gracefully exit the
# app by waiting for them all to finish
my(@threads) = ();
foreach $file (@files)
{
my($basename) = $file;
$basename =~ s/\.mkv$//g;
my($output) = "$basename.webm";
if (-e "$output")
{
print "Skipping \"$file\" which is already done\n";
next;
}
push(@threads, threads->create(\&thread_func, $file));
if (scalar(@threads) >= $nthreads)
{
my($blocked) = 1;
while ($blocked)
{
foreach $thread (@threads)
{
if (not $thread->is_running())
{
$blocked = 0;
$thread->join();
@threads = grep { $_ ne $thread } @threads;
last;
}
}
sleep(10);
}
}
}
foreach (@threads)
{
$_->join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment