encode.pl 17 KB

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