-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnfo2mkv.pl
executable file
·432 lines (373 loc) · 13.5 KB
/
nfo2mkv.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/perl
use strict;
use warnings;
# Relevant documentations
# MKV-Tagging:
# https://www.matroska.org/technical/specs/tagging/index.html
# https://matroska.org/technical/specs/tagging/example-video.html
#
# MKV Thumbnails:
# https://www.matroska.org/technical/cover_art/index.html
#
# NFO:
# https://kodi.wiki/view/NFO_files/TV_shows
# https://kodi.wiki/view/NFO_files/Movies
# use module
use Data::Dumper;
use File::Temp;
use Getopt::Long;
use Time::Piece;
use XML::Simple;
use autodie qw(:all);
# Subclass XML::Simple to subclass tag-sorting
package MatroskaTagXML;
use base 'XML::Simple';
sub reorder {
my ($prefix, $keys) = @_;
my @old = @$keys;
my @ordered;
for my $key (@$prefix) {
if (grep { $_ eq $key} @old) {
push @ordered, $key;
@old = grep {$_ ne $key} @old;
}
}
push @ordered, @old;
return @ordered;
}
sub sorted_keys {
my ($self, $name, $hashref) = @_;
if ($name eq "Tag") {
return reorder(["Targets", "Simple"], [keys %$hashref]);
} elsif ($name eq "Simple") {
return reorder(["Name", "String", "Simple"], [keys %$hashref]);
}
return $self->SUPER::sorted_keys($name, $hashref);
}
package main;
sub usage {
print STDERR <<EOF;
$0 [--verbose] (--tvshow=tvshow.nfo --episode=Foo_SXXEYY.nfo | --movie=Movie.nfo) [--xml=mkvtags_out.xml] [--mkv=Foo_SXXEYY.mkv]
--help Show this helpmessage
--verbose Be verbose while processing data
--tvshow=FILE Input: The NFO-file for the overall tv-shwo
--episode=FILE Input: The NFO-file for the individual episode
--movie=FILE Input: The NFO-file for the movie file
--xml=FILE Output, optional: The Matroska tags in XML format
--mkv=FILE Output, optional: Apply the tags to this file using mkvpropedit from the MKVToolnix suite
Please note that --movie as opposed to the pair --tvshow and --episode are mutually exclusive.
--mkv expects mkvpropedit to be in your PATH
EOF
exit 1;
}
# Parse arguments
my $verbose = '';
sub parse_nfo {
my ($filename) = @_;
# read XML file
my $xml = new XML::Simple;
return $xml->XMLin($filename, SuppressEmpty => 1, KeepRoot => 0);
}
sub format_matroska_xml {
my ($data) = @_;
# Filter empty items in tags
my @tags;
for my $tag (@{$data->{Tag}}) {
my $target = $tag->{Targets};
my $simple = $tag->{Simple};
die "Data misses required element 'Targets'" unless defined $target;
die "Data misses required element 'Simole'" unless defined $simple;
# Only defined tags...
my @simpleattrs = grep { defined $_ } @$simple;
# Skip this level if we have no metadata whatsoever
next unless @simpleattrs;
# toemit...
push @tags, { Targets => $target, Simple => \@simpleattrs };
}
my $xml = new MatroskaTagXML;
return $xml->XMLout({Tag => \@tags},
# Fake a doctype (which we do not really check... but we should be compliant :p)
XMLDecl => '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<!DOCTYPE Tags SYSTEM "matroskatags.dtd">',
NoAttr => 1, # Emit everything as xml tags, Matroska knows no xml attributes
KeepRoot => 0,
RootName => 'Tags', # Rename the root element
);
}
sub spew {
my ($filename, $data) = @_;
open (my $fh, ">encoding(utf8)", $filename)
or die "Failed to open file '$filename' for writing";
print $fh $data or die "Writing data to file '$filename' failed";
close($fh);
return $filename;
}
sub make_string_tag {
my ($type, $value) = @_;
if ($value && "" eq ref $value) { # Plain scalar
return ({ Name => $type, String => $value});
} elsif ("ARRAY" eq ref $value && @$value) {
return (map { make_string_tag($type, $_) } @{$value});
} elsif(ref $value) { # Everything except undef
die "make_string_tag: Unsupported reference type @{[ref $value]}";
}
return;
}
sub make_actor_tags {
my ($actors) = @_;
my @actortags;
for my $actor (sort keys %$actors){
my $tag = make_string_tag("ACTOR", $actor);
my $role = $actors->{$actor}->{role};
if ($role) {
my $rtag = make_string_tag("CHARACTER", $role);
$tag->{Simple} = $rtag;
}
push @actortags, $tag;
}
return @actortags;
}
# To support:
# - Collection: (level 70)
# TITLE -> tvshow:<showtitle>|episode:<showtitle>
# SUMMARY -> tvshow:<plot>
# [GENRE] -> tvshow:<genre>
# LAW_RATING -> tvshow:<mpaa>
# - Season: (level 60)
# PART_NUMBER -> <season>
# (TOTAL_PARTS)
# PRODUCTION_STUDIO -> <studio>
# - Tag: (level 50)
# TITLE -> <title>
# ORIGINAL_TITLE -> <originaltitle>
# DATE_RELEASED -> <aired>|<premiered>
# SUMMARY -> <outline>|<plot>
# PART_NUMBER -> <episode>
# (TOTAL_PARTS)
# DIRECTOR -> <director>
# WRITTEN_BY -> <credits>
# [ACTOR{CHARACTER}] -> <actor><name>|<actor><role>
# PRODUCTION_STUDIO -> <studio>
# LAW_RATING -> <mpaa>
sub handle_episode {
my ($episodenfo, $shownfo) = @_;
print "Parsing episode NFO\n" if $verbose;
my $episodemeta = parse_nfo($episodenfo);
print "Parsing TV-show NFO if exists\n" if $verbose;
my $showmeta = $shownfo ? parse_nfo($shownfo) : {};
# Look, don't ask. I manages to shoot myself into my own foot using XML::Simple all by myself :P
# This is probably why XML::Simple usage is discouraged
# If the NFO has a single actor tag, and that in turn consists solely of a Name, e.g.
# <actor>
# <name>Nixus MiniMax</name>
# <role/>
# <thumb/>
# <profile/>
# </actor>
# it would otherwise be parsed as {actor => { name => "Nixus Minimax" }}. So we manually patch that instance...
my $patch_actors = sub {
my ($actors) = @_;
if ($actors->{name}) {
if (1 == keys %{$actors}) {
$actors = { $actors->{name} => {} };
} else {
my $name = $actors->{name};
delete $actors->{name};
$actors = { $name => $actors };
}
}
return $actors;
};
$episodemeta->{actor} = $patch_actors->($episodemeta->{actor} // {});
$showmeta->{actor} = $patch_actors->($showmeta->{actor} // {});
print "Parsed show metadata:\n" . Dumper($showmeta) . "\n" if $verbose;
print "Parsed episode metadata:\n" . Dumper($episodemeta) . "\n" if $verbose;
# Sometimes, roles are tagged for the show, but not for the episodes
for my $name (keys %{$episodemeta->{actor}}) {
my $data = $episodemeta->{actor}->{$name};
my $showdata = $showmeta->{actor}->{$name};
# So transfer them...
if (!$data->{role} && $showdata->{role}) {
$data->{role} = $showdata->{role};
}
}
my %tags = (Tag => [
{
Targets => {
# COLLECTION
TargetTypeValue => 70
},
Simple => [
make_string_tag("TITLE", $showmeta->{showtitle} || $episodemeta->{showtitle}),
make_string_tag("SUMMARY", $showmeta->{plot}),
make_string_tag("GENRE", $showmeta->{genre}),
make_actor_tags($showmeta->{actor}),
make_string_tag("LAW_RATING", $showmeta->{mpaa}),
]
},{
Targets => {
# SEASON
TargetTypeValue => 60
},
Simple => [
make_string_tag("PART_NUMBER", $episodemeta->{season}),
make_string_tag("PRODUCTION_STUDIO", $episodemeta->{studio}),
]
},{
Targets => {
# EPISODE
TargetTypeValue => 50
},
Simple => [
make_string_tag("PART_NUMBER", $episodemeta->{episode}),
make_string_tag("TITLE", $episodemeta->{title}),
make_string_tag("ORIGINAL_TITLE", $episodemeta->{originaltitle}),
make_string_tag("SUMMARY", $episodemeta->{plot} || $episodemeta->{outline}),
make_string_tag("SYNOPSIS", $episodemeta->{outline} || $episodemeta->{plot}),
make_string_tag("DATE_RELEASED", $episodemeta->{aired} || $episodemeta->{premiered}),
make_string_tag("DIRECTOR", $episodemeta->{director}),
make_string_tag("WRITTEN_BY", $episodemeta->{credits}),
make_actor_tags($episodemeta->{actor}),
make_string_tag("PRODUCTION_STUDIO", $episodemeta->{studio} || $showmeta->{studio}),
make_string_tag("LAW_RATING", $episodemeta->{mpaa} || $showmeta->{mpaa}),
make_string_tag("DATE_TAGGED", localtime->strftime('%Y-%m-%d')),
]
}
]);
print "Tag's perl datastructure:\n" . Dumper(\%tags) . "\n" if $verbose;
return \%tags;
}
# To support:
# - Collection: (level 70)
# TITLE -> <set><name> if present
# - Tag: (level 50)
# TITLE -> <title>
# ORIGINAL_TITLE -> <originaltitle>
# DATE_RELEASED -> <premiered>|<aired>
# SUMMARY -> <plot>|<outline>
# [GENRE] -> <genre>
# SYNOPSIS -> <outline>|<plot>
# PART_NUMBER -> ?
# (TOTAL_PARTS)
# DIRECTOR -> <director>
# WRITTEN_BY -> <credits>
# [ACTOR{CHARACTER}] -> <actor><name>|<actor><role>
# PRODUCTION_STUDIO -> <studio>
# LAW_RATING -> <mpaa>
sub handle_movie {
# Partno is used for split movie files (the good old days...)
my ($movienfo, $partno) = @_;
print "Parsing movie NFO\n" if $verbose;
my $moviemeta = parse_nfo($movienfo);
print "Parsed movie metadata:\n" . Dumper($moviemeta) . "\n" if $verbose;
my %tags = (Tag => [
{
Targets => {
# COLLECTION
TargetTypeValue => 70
},
Simple => [
make_string_tag("TITLE", ($moviemeta->{set} // {})->{name}),
]
},{
Targets => {
# Movie
TargetTypeValue => 50
},
Simple => [
make_string_tag("PART_NUMBER", $partno),
make_string_tag("TITLE", $moviemeta->{title}),
make_string_tag("ORIGINAL_TITLE", $moviemeta->{originaltitle}),
make_string_tag("SUMMARY", $moviemeta->{plot} || $moviemeta->{outline}),
make_string_tag("SYNOPSIS", $moviemeta->{outline} || $moviemeta->{plot}),
make_string_tag("DATE_RELEASED", $moviemeta->{premiered}) || $moviemeta->{aired},
make_string_tag("DIRECTOR", $moviemeta->{director}),
make_string_tag("WRITTEN_BY", $moviemeta->{credits}),
make_actor_tags($moviemeta->{actor}),
make_string_tag("GENRE", $moviemeta->{genre}),
make_string_tag("PRODUCTION_STUDIO", $moviemeta->{studio}),
make_string_tag("LAW_RATING", $moviemeta->{mpaa}),
make_string_tag("DATE_TAGGED", localtime->strftime('%Y-%m-%d')),
]
}
]);
print "Tag's perl datastructure:\n" . Dumper(\%tags) . "\n" if $verbose;
return \%tags;
}
sub apply_tags_to_file {
my ($mkvfile, $tagsfile) = @_;
my @cmd = ("mkvpropedit", $mkvfile, "--tags", "all:$tagsfile");
print "Running mkvpropedit: @cmd\n" if $verbose;
system (@cmd) == 0
or die "Mkvpropedit failed: $?";
}
sub main {
# Argument Parsing
my ($tvshownfo, $episodenfo, $movienfo, $mkvfile, $xmlfile);
my $help = '';
GetOptions( "--tvshow=s" => \$tvshownfo
, "--episode=s" => \$episodenfo
, "--movie=s" => \$movienfo
, "--mkv=s" => \$mkvfile
, "--xml=s" => \$xmlfile
, "--verbose!" => \$verbose
, "--help!" => \$help
) or usage();
usage() if $help;
die("--movie and --episode are mutually exclusive") if $movienfo and $episodenfo;
die("Both --tvshow and --episode are required for tagging TV-Shows") if ($tvshownfo || $episodenfo) and !($tvshownfo && $episodenfo);
for my $file ($tvshownfo, $episodenfo, $movienfo) {
die("NFO-File '$file' does not exist or is not readable by user") if ($file && !(-r $file));
}
die("MKV file '$mkvfile' does not exist or is not accessible(rw) by user") if ($mkvfile && !(-r $mkvfile and -w $mkvfile));
die("One of --episode or --movie is required") unless $movienfo or $episodenfo;
# Input Parsing
my $tags;
if ($episodenfo) {
$tags = handle_episode($episodenfo, $tvshownfo);
} elsif ($movienfo) {
my $partno;
# Detect: foo_partX.mkv and foo_Part_X_(YYYY).mkv, and extract X
if ($mkvfile && $mkvfile =~ /(part|teil|split|dvd|pt|disk|disc)[-_ ]*(?<no>\d+)([-_ ]\(\d{4}\))?.mkv$/) {
$partno = $+{no};
}
$tags = handle_movie($movienfo, $partno);
} else {
die("Internal error: no input nfo found");
}
# Transform tagXML to "string"
print "Formatting Tags as XML\n" if $verbose;
my $xmldata = format_matroska_xml($tags);
print "Formatted XML data:\n" . $xmldata . "\n" if $verbose;
# Output handling
unless ($xmlfile || $mkvfile) {
# No further action: Print to stdout
print $xmldata;
} else {
# Output the xml file, use temporary file if we only have to interact
# with mkvtoolnix
my $outfile = $xmlfile || File::Temp->new();
print "Writing to $outfile\n" if $verbose;
spew($outfile, $xmldata);
apply_tags_to_file($mkvfile, $outfile) if ($mkvfile);
}
}
main();
# TODO: - record uniqueid fields (tvdb, imdb, ...)
# - coverimage: --attachment-name "cover" --attachment-mime-type "image/jpeg" --add-attachment "%%~nf.jpg"
#
#
#https://www.matroska.org/technical/cover_art/index.html
#
# The pictures should only use the JPEG and PNG picture formats.
#
# There can be 2 different cover for a movie/album. A portrait one (like a DVD case) and a landscape one (like a banner ad for example, looking better on a wide screen).
#
# There can be 2 versions of the same cover, the normal one and the small one. The dimension of the normal one should be 600 on the smallest side (eg 960x600 for landscape and 600x800 for portrait, 600x600 for square). The dimension of the small one should be 120 (192x120 or 120x160).
#
# The way to differentiate between all these versions is by the filename. The default filename is cover.(png/jpg) for backward compatibility reasons. That is the "big" version of the file (600) in square or portrait mode. It should also be the first file in the attachments. The smaller resolution should be prefixed with "small_", ie small_cover.(jpg/png). The landscape variant should be suffixed with "_land", ie cover_land.jpg. The filenames are case sensitive and should all be lower case.
#
# In the end a file could contain these 4 basic cover art files:
# cover.jpg (portrait/square 600)
# small_cover.png (portrait/square 120)
# cover_land.png (landscape 600)
# small_cover_land.jpg (landscape 120)