Last active
January 6, 2018 18:50
-
-
Save amolloy/b7a5f32d5dfa4730d39ba4e6f1a92ce1 to your computer and use it in GitHub Desktop.
Use mp4 TV Show metadata to rename files in a Plex-friendly format
This file contains 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/perl -w | |
use strict; | |
use local::lib; | |
use File::Basename; | |
use File::Util qw(escape_filename); | |
use File::Path qw(make_path); | |
use File::Spec; | |
use Getopt::Long; | |
use List::MoreUtils qw(natatime); | |
use Data::Dumper; | |
use JSON; | |
my $src; | |
my $dst; | |
GetOptions("src=s" => \$src, | |
"dst=s" => \$dst) or die; | |
die "Must supply source." unless length($src); | |
die "Must supply destination." unless length($dst); | |
die "Source does not exist." unless -d $src;; | |
die "Destination does not exist." unless -d $dst; | |
my @files = <$src/*>; | |
foreach my $file (@files) | |
{ | |
my ($path, $filename, $ext) = fileparse($file, '\..*'); | |
my $metadata = `/usr/local/bin/exiftool -api largefilesupport=1 -tvseason -tvshow -tvepisode -title -j "$file"`; | |
next unless $metadata; | |
my $decoded = decode_json($metadata); | |
my $showInfo = $$decoded[0]; | |
my $show = escape_filename($$showInfo{"TVShow"}); | |
my $episode = sprintf("%02s", $$showInfo{"TVEpisode"}); | |
my $title = escape_filename($$showInfo{"Title"}); | |
my $destination = ""; | |
my $seasonFilenamePart = ""; | |
if (length($$showInfo{"TVSeason"})) | |
{ | |
my $season = sprintf("%02s", $$showInfo{"TVSeason"}); | |
$seasonFilenamePart = 's' . $season; | |
$destination = File::Spec->catfile($dst, $show, "Season " . $season); | |
} | |
else | |
{ | |
$destination = File::Spec->catfile($dst, $show); | |
} | |
if (!-d $destination) | |
{ | |
make_path $destination or die "Couldn't create destination directory for $file"; | |
} | |
my $destName = $show . ' - ' . $seasonFilenamePart . 'e' . $episode. ' - ' . $title . $ext; | |
my $fullpath = File::Spec->catfile($destination, $destName); | |
rename $file, $fullpath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment