encode.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Includes
  5. use IPC::Open3;
  6. use File::Basename;
  7. # Parameters
  8. my @video_params = ('--markers', '--large-file', '--optimize', '--encoder', 'x264', '--detelecine', '--decomb', '--loose-anamorphic', '--modulus', '16', '--encopts', 'b-adapt=2:rc-lookahead=50', '--audio-copy-mask', 'dtshd,dts,ac3,aac', '--audio-fallback', 'ffac3', '--mixdown', 'dpl2', '--ab', '192');
  9. my $FORMAT = 'mp4';
  10. my $QUALITY = 20;
  11. my $HD_QUALITY = 20;
  12. my $HD_WIDTH = 1350;
  13. my $MIN_VIDEO_WIDTH = 100;
  14. my $MAX_CROP_DIFF = .1;
  15. my $MAX_DURA_DIFF = 5;
  16. my @CODEC_ORDER = ('DTS-HD', 'DTS', 'PCM', 'AC3', 'AAC', 'OTHER');
  17. my $AUDIO_EXCLUDE_REGEX = '\b(?:Chinese|Espanol|Francais|Japanese|Korean|Portugues|Thai)\b';
  18. my $SUB_INCLUDE_REGEX = '\b(?:English|Unknown|Closed\s+Captions)\b';
  19. my %LANG_INCLUDE_ISO = ('639-1' => 1);
  20. my $HB_EXEC = $ENV{'HOME'} . '/bin/video/HandBrakeCLI';
  21. my $DEBUG = 0;
  22. my $FORCE_MKV = 0;
  23. # Runtime debug mode
  24. if (defined($ENV{'DEBUG'}) && $ENV{'DEBUG'}) {
  25. $DEBUG = 1;
  26. }
  27. # Allow overrides for audio languages
  28. if ($ENV{'AUDIO_EXECLUDE_REGEX'}) {
  29. $AUDIO_EXCLUDE_REGEX = $ENV{'AUDIO_EXCLUDE_REGEX'};
  30. }
  31. # Allow overrides for subtitle languages
  32. if ($ENV{'SUB_INCLUDE_REGEX'}) {
  33. $SUB_INCLUDE_REGEX = $ENV{'SUB_INCLUDE_REGEX'};
  34. }
  35. # Command-line parameters
  36. my ($in_file, $out_file, $title) = @ARGV;
  37. if (!defined($in_file) || length($in_file) < 1 || !-r $in_file) {
  38. die('Usage: ' . basename($0) . " in_file [out_file] [title]\n");
  39. }
  40. # Scan for title/track info
  41. my %titles = &scan($in_file);
  42. # Allow external subtitles for single-title files
  43. if (scalar(keys(%titles)) == 1) {
  44. my $srt_file = $in_file;
  45. $srt_file =~ s/\.\w{2,3}$/.srt/;
  46. if (-r $srt_file) {
  47. if ($DEBUG) {
  48. print STDERR 'Adding subtitles from: ' . $srt_file . "\n";
  49. }
  50. push(@video_params, '--srt-file', $srt_file);
  51. }
  52. }
  53. # Allow encoding of a specific title
  54. if ($title) {
  55. if ($title =~ /^\d+$/ && $title > 0) {
  56. if (!defined($titles{$title})) {
  57. die(basename($0) . ': Invalid title number: ' . $title . "\n");
  58. }
  59. my $selected_title = $titles{$title};
  60. %titles = ();
  61. $titles{$title} = $selected_title;
  62. } elsif ($title =~ /main/i) {
  63. my $max_title = 0;
  64. my $max_duration = 0;
  65. foreach my $title (keys(%titles)) {
  66. if (!$titles{$title}{'duration'}) {
  67. warn(basename($0) . ': Unknown duration for title: ' . $title . "\n");
  68. next;
  69. }
  70. my $new_max = 0;
  71. if (abs($titles{$title}{'duration'} - $max_duration) < $MAX_DURA_DIFF) {
  72. if ($titles{$title}{'aspect'} > $titles{$max_title}{'aspect'}) {
  73. $new_max = 1;
  74. } elsif ($titles{$title}{'size'}[0] > $titles{$max_title}{'size'}[0] || $titles{$title}{'size'}[1] > $titles{$max_title}{'size'}[1]) {
  75. $new_max = 1;
  76. }
  77. } elsif ($titles{$title}{'duration'} > $max_duration) {
  78. $new_max = 1;
  79. }
  80. if ($new_max) {
  81. $max_duration = $titles{$title}{'duration'};
  82. $max_title = $title;
  83. }
  84. }
  85. my $selected_title = $titles{$max_title};
  86. %titles = ();
  87. $titles{$max_title} = $selected_title;
  88. }
  89. }
  90. # Ensure we have an output file name
  91. # Force MKV output if the output file name is provided and ends in .MKV
  92. if (!defined($out_file) || length($out_file) < 1) {
  93. $out_file = $in_file;
  94. } else {
  95. my ($force_format) = $out_file =~ /\.(\w{2,3})$/;
  96. if (lc($force_format) eq 'mkv') {
  97. $FORMAT = 'mkv';
  98. }
  99. }
  100. # Keep the original format around so we can reset between tracks
  101. my $FORMAT_ORIG = $FORMAT;
  102. # Encode each title
  103. foreach my $title (keys(%titles)) {
  104. if ($DEBUG) {
  105. print STDERR 'Setting options for title: ' . $title . "\n";
  106. }
  107. # Reset
  108. $FORMAT = $FORMAT_ORIG;
  109. # Parse the title's audio and subtitle tracks
  110. my $scan = $titles{$title};
  111. my @audio = &audioOptions($scan);
  112. my @subs = &subOptions($scan);
  113. # Skip tracks that have no video
  114. if (scalar(@{ $scan->{'size'} }) < 2 || $scan->{'size'}[0] < $MIN_VIDEO_WIDTH) {
  115. print STDERR basename($0) . ': No video detected in: ' . $in_file . ':' . $title . ". Skipping title...\n";
  116. next;
  117. }
  118. # Skip tracks that have no audio
  119. if (scalar(@{ $scan->{'audio'} }) < 1 || scalar(@audio) < 1) {
  120. print STDERR basename($0) . ': No audio detected in: ' . $in_file . ':' . $title . ". Skipping title...\n";
  121. next;
  122. }
  123. # Reduce the quality if the image size is particularly large
  124. my $title_quality = $QUALITY;
  125. if ($scan->{'size'}[0] > $HD_WIDTH) {
  126. $title_quality = $HD_QUALITY;
  127. }
  128. # Override unlikely autocrop values
  129. if ( abs($scan->{'crop'}[0] - $scan->{'crop'}[1]) > $scan->{'size'}[0] * $MAX_CROP_DIFF
  130. || abs($scan->{'crop'}[2] - $scan->{'crop'}[3]) > $scan->{'size'}[0] * $MAX_CROP_DIFF)
  131. {
  132. print STDERR basename($0) . ': Overriding unlikely autocrop values: ' . join(':', @{ $scan->{'crop'} }) . "\n";
  133. my @crop = (0, 0, 0, 0);
  134. $scan->{'crop'} = \@crop;
  135. }
  136. # Force MKV output if the title contains PGS subtitles
  137. foreach my $track (values(%{ $scan->{'subtitle_parsed'} })) {
  138. if ($track->{'type'} eq 'PGS') {
  139. $FORMAT = 'mkv';
  140. last;
  141. }
  142. }
  143. # Force MKV output if the title contains DTS audio (technically MP4 is supported buy QuickTime hates it)
  144. foreach my $track (values(%{ $scan->{'audio_parsed'} })) {
  145. if ($track->{'codec'} =~ /DTS/i) {
  146. $FORMAT = 'mkv';
  147. last;
  148. }
  149. }
  150. # Select a file name extension that matches the format
  151. my $title_out_file = $out_file;
  152. $title_out_file =~ s/\.(?:\w{2,3}|dvdmedia)$//i;
  153. if ($FORMAT eq 'mkv') {
  154. $title_out_file .= '.mkv';
  155. } else {
  156. $title_out_file .= '.m4v';
  157. }
  158. # Force the title number into the output file name if there are multiple titles to be encoded
  159. if (scalar(keys(%titles)) > 1) {
  160. my $title_text = sprintf('%02d', $title);
  161. $title_out_file =~ s/(\.\w{2,3})$/\-${title_text}${1}/;
  162. }
  163. # Output file
  164. if (uc($title_out_file) eq uc($in_file)) {
  165. $title_out_file =~ s/(\.\w{2,3})$/-recode${1}/;
  166. }
  167. # Build the arugment list
  168. my @args = ($HB_EXEC);
  169. push(@args, '--title', $title);
  170. push(@args, '--input', $in_file);
  171. push(@args, '--output', $title_out_file);
  172. push(@args, '--format', $FORMAT);
  173. push(@args, '--quality', $title_quality);
  174. push(@args, '--crop', join(':', @{ $scan->{'crop'} }));
  175. push(@args, @video_params);
  176. push(@args, @audio);
  177. push(@args, @subs);
  178. if ($DEBUG) {
  179. print STDERR "\n" . join(' ', @args) . "\n\n";
  180. }
  181. # Sanity check
  182. if (-e $title_out_file) {
  183. print STDERR basename($0) . ': Output file exists: ' . $title_out_file . ". Skipping...\n";
  184. next;
  185. }
  186. # Run it
  187. my $child_out = '';
  188. my $child_in = '';
  189. my $pid = open3($child_in, $child_out, $child_out, @args);
  190. close($child_in);
  191. while (<$child_out>) {
  192. if ($DEBUG) {
  193. print STDERR $_;
  194. }
  195. }
  196. waitpid($pid, 0);
  197. close($child_out);
  198. # Provide the new file name if requested
  199. if ($ENV{'RECODE_OUTFILE'}) {
  200. print $title_out_file;
  201. }
  202. }
  203. # Cleanup
  204. exit(0);
  205. sub subOptions($) {
  206. my ($scan) = @_;
  207. my %tracks = ();
  208. foreach my $track (@{ $scan->{'subtitle'} }) {
  209. my ($language, $note, $iso, $text, $type) = $track->{'description'} =~ /^([^\(]+)(?:\s+\(([^\)]+)\))?\s+\(iso(\d+\-\d+)\:\s+\w\w\w\)\s+\((Text|Bitmap)\)\((CC|VOBSUB|PGS|SSA|TX3G|UTF\-\d+)\)/i;
  210. if (!defined($iso)) {
  211. print STDERR 'Could not parse subtitle description: ' . $track->{'description'} . "\n";
  212. # Temporarily exit on parsing errors -- at least until we're sure about this new parser
  213. #next;
  214. exit(1);
  215. }
  216. # Map text/bitmap into a boolean
  217. if ($text =~ /TEXT/i) {
  218. $text = 1;
  219. } else {
  220. $text = 0;
  221. }
  222. # Standardize the codes
  223. $iso = uc($iso);
  224. $type = uc($type);
  225. # Push all parsed data into an array
  226. my %data = ('language' => $language, 'note' => $note, 'iso' => $iso, 'text' => $text, 'type' => $type);
  227. $tracks{ $track->{'index'} } = \%data;
  228. # Print what we found
  229. if ($DEBUG) {
  230. print STDERR 'Found subtitle track: ';
  231. printHash(\%data);
  232. }
  233. }
  234. # Push the parsed data back up the chain
  235. $scan->{'subtitle_parsed'} = \%tracks;
  236. # Keep subtitles in our prefered langauges, and all text-based subtitles (they're small)
  237. my @keep = ();
  238. foreach my $index (keys(%tracks)) {
  239. if (isValidSubLanguage($tracks{$index}->{'language'}, $tracks{$index}->{'iso'})) {
  240. push(@keep, $index);
  241. } elsif ($tracks{$index}->{'text'}) {
  242. push(@keep, $index);
  243. } else {
  244. if ($DEBUG) {
  245. print STDERR 'Skipping subtitle ' . $index . ' due to language: ' . $tracks{$index}->{'language'} . "\n";
  246. }
  247. }
  248. }
  249. # Send back the argument string (if any)
  250. if (scalar(@keep) < 1) {
  251. return '';
  252. }
  253. return ('--subtitle', join(',', @keep));
  254. }
  255. sub audioOptions($) {
  256. my ($scan) = @_;
  257. # Type the audio tracks
  258. my %tracks = ();
  259. foreach my $track (@{ $scan->{'audio'} }) {
  260. my ($language, $codec, $note, $channels, $iso) = $track->{'description'} =~ /^([^\(]+)\s+\(([^\)]+)\)\s+(?:\(([^\)]*Commentary[^\)]*)\)\s+)?\((\d+\.\d+\s+ch|Dolby\s+Surround)\)(?:\s+\(iso(\d+\-\d+)\:\s+\w\w\w\))?/;
  261. if (!defined($channels)) {
  262. print STDERR 'Could not parse audio description: ' . $track->{'description'} . "\n";
  263. # Temporarily exit on parsing errors -- at least until we're sure about this new parser
  264. #next;
  265. exit(1);
  266. }
  267. # Decode the channels string to a number
  268. if ($channels =~ /(\d+\.\d+)\s+ch/i) {
  269. $channels = $1;
  270. } elsif ($channels =~ /Dolby\s+Surround/i) {
  271. $channels = 3.1;
  272. }
  273. # Standardize the codec
  274. foreach my $code (@CODEC_ORDER) {
  275. my $metacode = quotemeta($code);
  276. if ($codec =~ /${metacode}/i) {
  277. $codec = $code;
  278. last;
  279. } elsif ($code eq 'OTHER') {
  280. if ($codec =~ /MP3/i || $codec =~ /MPEG/i) {
  281. $codec = $code;
  282. } elsif ($codec eq 'truehd' || $codec eq 'dca') {
  283. print STDERR 'Found incompatible audio (' . $track->{'description'} . ') in track ' . $track->{'index'} . "\n";
  284. $codec = undef();
  285. } else {
  286. print STDERR 'Found unknown audio (' . $track->{'description'} . ') in track ' . $track->{'index'} . "\n";
  287. $codec = $code;
  288. }
  289. }
  290. }
  291. # Push all parsed data into the array
  292. my %data = ('language' => $language, 'codec' => $codec, 'channels' => $channels, 'iso' => $iso, 'note' => $note);
  293. $tracks{ $track->{'index'} } = \%data;
  294. # Print what we found
  295. if ($DEBUG) {
  296. print STDERR 'Found audio track: ';
  297. printHash(\%data);
  298. }
  299. }
  300. # Push the parsed data back up the chain
  301. $scan->{'audio_parsed'} = \%tracks;
  302. # Find the track with the most channels for each codec, and the highest number of channels among all types of tracks
  303. # Then choose the most desired codec among the set of codecs that contain the highest number of channels
  304. # This chooses the track with the most channels for the mixdown, and resolves ties using CODEC_ORDER
  305. my $mixdown = undef();
  306. my %bestByCodec = ();
  307. my $bestCodec = undef();
  308. my $mostChannels = 0;
  309. foreach my $codec (@CODEC_ORDER) {
  310. $bestByCodec{$codec} = findBestAudioTrack(\%tracks, $codec);
  311. if (defined($bestByCodec{$codec}) && (!defined($bestCodec) || $mostChannels < $tracks{ $bestByCodec{$codec} }->{'channels'})) {
  312. $bestCodec = $codec;
  313. $mostChannels = $tracks{ $bestByCodec{$codec} }->{'channels'};
  314. }
  315. }
  316. foreach my $codec (@CODEC_ORDER) {
  317. if (defined($bestByCodec{$codec}) && $tracks{ $bestByCodec{$codec} }->{'channels'} == $mostChannels) {
  318. $mixdown = $bestByCodec{$codec};
  319. last;
  320. }
  321. }
  322. # Sanity check
  323. if (!defined($mixdown) || $mixdown < 1) {
  324. print STDERR basename($0) . ": No usable audio tracks in title\n";
  325. return;
  326. }
  327. # Mixdown track first
  328. my @audio_tracks = ();
  329. if (defined($mixdown) && $mixdown > 0) {
  330. if ($DEBUG) {
  331. print STDERR 'Using track ' . $mixdown . " as default AAC audio\n";
  332. if ($tracks{$mixdown}->{'channels'} > 2) {
  333. print STDERR "\tMixing down with Dolby Pro Logic II encoding\n";
  334. }
  335. }
  336. my %track = ('index' => $mixdown, 'encoder' => 'ca_aac');
  337. push(@audio_tracks, \%track);
  338. }
  339. # Passthru DTS-MA, DTS, AC3, and AAC
  340. # Keep other audio tracks, but recode to AAC (using Handbrake's audio-copy-mask/audio-fallback feature)
  341. foreach my $index (keys(%tracks)) {
  342. if ($mixdown == $index && $tracks{$index}->{'codec'} eq 'OTHER' && $tracks{$index}->{'channels'} <= 2) {
  343. if ($DEBUG) {
  344. print STDERR 'Skipping recode of track ' . $index . ' since it is already used as the default track and contains only ' . $tracks{$index}->{'channels'} . " channels\n";
  345. }
  346. next;
  347. } elsif (!$tracks{$index}->{'codec'}) {
  348. if ($DEBUG) {
  349. print STDERR 'Skipping track ' . $index . " due to invalid codec\n";
  350. }
  351. next;
  352. } elsif (!isValidAudioLanguage($tracks{$index}->{'language'}, $tracks{$index}->{'iso'})) {
  353. if ($DEBUG) {
  354. print STDERR 'Skipping track ' . $index . ' due to language: ' . $tracks{$index}->{'language'} . "\n";
  355. }
  356. next;
  357. } else {
  358. my %track = ('index' => $index, 'encoder' => 'copy');
  359. push(@audio_tracks, \%track);
  360. }
  361. }
  362. # Consolidate from the hashes
  363. my @output_tracks = ();
  364. my @output_encoders = ();
  365. foreach my $track (@audio_tracks) {
  366. push(@output_tracks, $track->{'index'});
  367. push(@output_encoders, $track->{'encoder'});
  368. }
  369. # Send back the argument strings
  370. return ('--audio', join(',', @output_tracks), '--aencoder', join(',', @output_encoders));
  371. }
  372. sub scan($) {
  373. my ($in_file) = @_;
  374. # Fork to scan the file
  375. my $child_out = '';
  376. my $pid = open3('<&STDIN', $child_out, $child_out, $HB_EXEC, '--title', '0', '--input', $in_file);
  377. # Loop through the output
  378. my $scan;
  379. my %titles = ();
  380. my $inTitle = 0;
  381. my $zone = '';
  382. while (<$child_out>) {
  383. if (!$inTitle && m/scan thread found (\d+) valid title/i) {
  384. if ($DEBUG) {
  385. print STDERR 'Found ' . $1 . " titles in source\n";
  386. }
  387. } elsif (m/^\s+Stream \#0\.(\d+)\(\w+\)\:\s+(.*)/) {
  388. my $stream = $1;
  389. my $desc = $2;
  390. } elsif (m/^\s*\+\s+title\s+(\d+)\:/) {
  391. # Save the current title (if any)
  392. if ($inTitle) {
  393. $titles{$inTitle} = $scan;
  394. }
  395. # Grab the new title number
  396. $inTitle = $1;
  397. # Init the data collectors
  398. my @audio = ();
  399. my @crop = ();
  400. my @subtitle = ();
  401. my @size = ();
  402. my %tmp = (
  403. 'audio' => \@audio,
  404. 'crop' => \@crop,
  405. 'subtitle' => \@subtitle,
  406. 'size' => \@size,
  407. 'duration' => 0,
  408. 'aspect' => 0
  409. );
  410. $scan = \%tmp;
  411. if ($DEBUG) {
  412. print STDERR 'Found data for title ' . $1 . "\n";
  413. }
  414. } elsif ($inTitle) {
  415. if (/^\s*\+\s+size: (\d+)x(\d+)/) {
  416. push(@{ $scan->{'size'} }, $1, $2);
  417. if ($DEBUG) {
  418. print STDERR 'Size: ' . join('x', @{ $scan->{'size'} }) . "\n";
  419. }
  420. if (/\s+display aspect\: (\d*\.\d+)/) {
  421. $scan->{'aspect'} = $1;
  422. if ($DEBUG) {
  423. print STDERR 'Aspect: ' . $scan->{'aspect'} . "\n";
  424. }
  425. }
  426. } elsif (/^\s*\+\s+duration\:\s+(\d+)\:(\d+)\:(\d+)/) {
  427. $scan->{'duration'} = ($1 * 3600) + ($2 * 60) + $3;
  428. if ($DEBUG) {
  429. print STDERR 'Duration: ' . $scan->{'duration'} . "\n";
  430. }
  431. } elsif (/^\s*\+\s+autocrop\:\s+(\d+)\/(\d+)\/(\d+)\/(\d+)/) {
  432. push(@{ $scan->{'crop'} }, $1, $2, $3, $4);
  433. if ($DEBUG) {
  434. print STDERR 'Crop: ' . join('/', @{ $scan->{'crop'} }) . "\n";
  435. }
  436. } elsif ($zone ne 'audio' && /^\s*\+\s+audio\s+tracks\:\s*$/) {
  437. $zone = 'audio';
  438. } elsif ($zone eq 'audio' && /^\s*\+\s+(\d+)\,\s+(.*)/) {
  439. my %track = ('index' => $1, 'description' => $2);
  440. push(@{ $scan->{'audio'} }, \%track);
  441. if ($DEBUG) {
  442. print STDERR 'Audio Track #' . $track{'index'} . ': ' . $track{'description'} . "\n";
  443. }
  444. } elsif ($zone ne 'subtitle' && /^\s*\+\s+subtitle\s+tracks\:\s*$/) {
  445. $zone = 'subtitle';
  446. } elsif ($zone eq 'subtitle' && m/^\s*\+\s+(\d+)\,\s+(.*)$/) {
  447. my %track = ('index' => $1, 'description' => $2);
  448. push(@{ $scan->{'subtitle'} }, \%track);
  449. if ($DEBUG) {
  450. print STDERR 'Subtitle Track #' . $track{'index'} . ': ' . $track{'description'} . "\n";
  451. }
  452. }
  453. }
  454. }
  455. # Save the last title (if any)
  456. if ($inTitle) {
  457. $titles{$inTitle} = $scan;
  458. }
  459. # Cleanup the scan process
  460. waitpid($pid, 0);
  461. close($child_out);
  462. # Sanity check
  463. if (scalar(keys(%titles)) < 1) {
  464. die(basename($0) . ': Did not find any titles in file: ' . $in_file . "\n");
  465. }
  466. # Return
  467. return %titles;
  468. }
  469. sub findBestAudioTrack($$) {
  470. my ($tracks, $codec) = @_;
  471. my $best = undef();
  472. my $channels = 0;
  473. for my $index (keys(%{$tracks})) {
  474. my $track = $tracks->{$index};
  475. # Skip tracks not in the specified codec
  476. if (!$track->{'codec'} || $track->{'codec'} ne $codec) {
  477. next;
  478. }
  479. # Skip foreign language tracks
  480. if (!isValidAudioLanguage($track->{'language'}, $track->{'iso'})) {
  481. next;
  482. }
  483. # Find the track with the most channels (Ófirst wins)
  484. if (!defined($best) || $track->{'channels'} > $channels) {
  485. $best = $index;
  486. $channels = $track->{'channels'};
  487. }
  488. }
  489. return $best;
  490. }
  491. sub isValidSubLanguage($) {
  492. my ($lang, $iso) = @_;
  493. if (scalar(keys(%LANG_INCLUDE_ISO)) && $LANG_INCLUDE_ISO{$iso}) {
  494. return 1;
  495. } elsif ($SUB_INCLUDE_REGEX && $lang =~ /${SUB_INCLUDE_REGEX}/i) {
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. sub isValidAudioLanguage($) {
  501. my ($lang, $iso) = @_;
  502. if (scalar(keys(%LANG_INCLUDE_ISO)) && $LANG_INCLUDE_ISO{$iso}) {
  503. return 1;
  504. } elsif ($AUDIO_EXCLUDE_REGEX && $lang =~ /${AUDIO_EXCLUDE_REGEX}/i) {
  505. return 0;
  506. }
  507. return 1;
  508. }
  509. sub printHash($) {
  510. my ($data) = @_;
  511. my $first = 1;
  512. foreach my $key (keys(%{$data})) {
  513. if (defined($data->{$key})) {
  514. if ($first) {
  515. $first = 0;
  516. } else {
  517. print STDERR ', ';
  518. }
  519. print STDERR $key . ' => ' . $data->{$key};
  520. }
  521. }
  522. print STDERR "\n";
  523. }