Skip to content

Instantly share code, notes, and snippets.

@makenosound
Created January 23, 2016 01:46
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 makenosound/688c1a84ab5fc75f163c to your computer and use it in GitHub Desktop.
Save makenosound/688c1a84ab5fc75f163c to your computer and use it in GitHub Desktop.
Use ffpmeg to convert h.265 to h.265
#!/usr/bin/perl
use strict;
use warnings;
open DIR, "ls -1 |";
while (<DIR>)
{
chomp;
next if ( -d "$_"); # skip directories
next unless ( -r "$_"); # if it's not readable skip it!
my $file = $_;
open PROBE, "ffprobe -show_streams -of csv '$file' 2>/dev/null|" or die ("Unable to launch ffmpeg for $file! ($!)");
my ($v, $a, $s, @c) = (0,0,0);
while (<PROBE>)
{
my @streaminfo = split(/,/, $_);
push(@c, $streaminfo[2]) if ($streaminfo[5] eq "video");
$a++ if ($streaminfo[5] eq "audio");
$s++ if ($streaminfo[5] eq "subtitle");
}
close PROBE;
$v = scalar @c;
if (scalar @c eq 1 and $c[0] eq "ansi")
{
warn("Text file detected, skipping...\n");
next;
}
warn("$file: Video Streams: $v, Audio Streams: $a, Subtitle Streams: $s, Video Codec(s): " . join (", ", @c) . "\n");
if (scalar @c > 1)
{
warn("$file has more than one video stream, bailing!\n");
next;
}
if ($c[0] eq "hevc")
{
warn("HEVC detected for $file ...converting to AVC...\n");
system("mkdir -p h265");
my @params = ("-hide_banner", "-threads 2");
# Map streams
push(@params, "-map 0") if ($a > 1 or $s > 1 or $v > 1);
# Map audio stream 2 instead of defaults
# push(@params, "-map 0:0 -map 0:2 -map 0:2 -map 0:4 -map 0:4");
push(@params, "-c:a copy") if ($a);
push(@params, "-c:s copy") if ($s);
push(@params, "-c:v libx264 -pix_fmt yuv420p") if ($v);
if (system("mv '$file' 'h265/$file'"))
{
warn("Error moving $file -> h265/$file\n");
next;
}
warn("Running command: ffmpeg -xerror -i 'h265/$file' " . join(" ", @params) . " '$file'");
if (system("ffmpeg -xerror -i 'h265/$file' " . join(" ", @params) . " '$file' 2>/dev/null"))
{
warn("FFMPEG ERROR. Cannot convert $file restoring original...\n");
system("mv 'h265/$file' '$file'");
next;
}
} else {
warn("$file doesn't appear to need converting... Skipping...\n");
}
}
close DIR;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment