encode.pl 17 KB

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