dvd2concat 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env perl
  2. # Copyright (c) 2014 Nicolas George
  3. #
  4. # This file is part of FFmpeg.
  5. #
  6. # FFmpeg is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public License
  8. # as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # FFmpeg is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. =head1 NAME
  20. dvd2concat - create a concat script for a DVD title
  21. =head1 SYNOPSIS
  22. tools/dvd2concat I<path/to/dvd/structure> > I<file.concat>
  23. =head1 DESCRIPTION
  24. This script uses B<lsdvd> to produce concat script for a DVD title.
  25. The resulting script can be used to play the DVD using B<ffplay>, to
  26. transcode it using B<ffmpeg> or any other similar use.
  27. I<path/to/dvd/structure> is the path to the DVD structure hierarchy; it
  28. normally contains a directory named B<VIDEO_TS>. It must not be encrypted
  29. with CSS.
  30. I<file.concat> is the output file. It can be used as an input to ffmpeg.
  31. It will require the B<-safe 0> and
  32. B<-protocol_whitelist file,subfile,concat> options.
  33. =cut
  34. use strict;
  35. use warnings;
  36. use Getopt::Long ":config" => "require_order";
  37. use Pod::Usage;
  38. my $title;
  39. GetOptions (
  40. "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
  41. "manpage|m" => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
  42. "title|t=i" => \$title,
  43. ) and @ARGV == 1 or pod2usage({ -verbose => 1, -exitval => 1 });
  44. my ($path) = @ARGV;
  45. my $lsdvd_message =
  46. "Make sure your lsdvd version has the two following patches applied:\n" .
  47. "http://sourceforge.net/p/lsdvd/feature-requests/1/\n" .
  48. "https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=603826\n";
  49. my $lsdvd = do {
  50. open my $l, "-|", "lsdvd", "-Op", "-x", $path
  51. or die "You need to install lsdvd for this script to work.\n$lsdvd_message";
  52. local $/;
  53. <$l>;
  54. };
  55. my %lsdvd = eval $lsdvd;
  56. die $@ if $@;
  57. if (!defined $title) {
  58. $title = $lsdvd{longest_track};
  59. warn "Using longest title $title\n";
  60. }
  61. my $track = $lsdvd{track}[$title - 1]
  62. or die "Title $title does not exist (1-", scalar(@{$lsdvd{track}}), ")\n";
  63. my $vts_base = sprintf "%s/VIDEO_TS/VTS_%02d_", $path, $track->{vts};
  64. my @frag;
  65. for my $i (1 .. 9) {
  66. my $file = sprintf "%s%d.VOB", $vts_base, $i;
  67. my $size = -s $file or last;
  68. push @frag, { file => $file, size => $size >> 11 };
  69. }
  70. my $concat = "ffconcat version 1.0\n";
  71. $concat .= "\nstream\nexact_stream_id 0x1E0\n";
  72. for my $audio (@{$track->{audio}}) {
  73. $concat .= "\nstream\nexact_stream_id " . $audio->{streamid} . "\n";
  74. $concat .= "stream_meta language " . $audio->{langcode} . "\n" if $audio->{langcode};
  75. }
  76. for my $subp (@{$track->{subp}}) {
  77. $concat .= "\nstream\nexact_stream_id " . $subp->{streamid} . "\n";
  78. $concat .= "stream_codec dvd_subtitle\n";
  79. $concat .= "stream_meta language " . $subp->{langcode} . "\n" if $subp->{langcode};
  80. my $extradata = "";
  81. if ($track->{width} && $track->{height}) {
  82. $extradata .= "size: " . $track->{width} . "x" . $track->{height} . "\n";
  83. }
  84. if (my $pal = $track->{palette}) {
  85. my @pal;
  86. for my $c (@$pal) {
  87. # Adapted from mplayer/sub/vobsub.c
  88. my $y = ((hex($c) >> 16) & 0xFF);
  89. my $u = ((hex($c) >> 8) & 0xFF) - 128;
  90. my $v = ((hex($c) >> 0) & 0xFF) - 128;
  91. my ($r, $g, $b) = map { int($_ < 0 ? 0 : $_ > 255 ? 255 : $_) }
  92. $y + 1.4022 * $u,
  93. $y - 0.3456 * $u - 0.7145 * $v,
  94. $y + 1.7710 * $v;
  95. push @pal, sprintf "%06x", ($r << 16) | ($g << 8) | $b;
  96. }
  97. $extradata .= "palette: " . join(", ", @pal) . "\n";
  98. }
  99. if ($extradata ne "") {
  100. $concat .= "stream_extradata " . unpack("H*", $extradata);
  101. }
  102. }
  103. my $chap_time = 0;
  104. for my $chap (@{$track->{chapter}}) {
  105. $concat .= sprintf "\nchapter %d %.3f %.3f\n",
  106. $chap->{ix}, $chap_time, $chap_time + $chap->{length};
  107. $chap_time += $chap->{length};
  108. }
  109. for my $cell (@{$track->{cell}}) {
  110. my $off = $cell->{first_sector};
  111. die "Your lsdvd version does not print cell sectors.\n$lsdvd_message"
  112. unless defined $off;
  113. my $size = $cell->{last_sector} + 1 - $cell->{first_sector};
  114. my $frag = 0;
  115. while ($frag < @frag) {
  116. last if $off < $frag[$frag]->{size};
  117. $off -= $frag[$frag++]->{size};
  118. }
  119. die "Cell beyond VOB data\n" unless $frag < @frag;
  120. my $cur_off = $off;
  121. my $cur_size = $size;
  122. my @files;
  123. while ($cur_size > $frag[$frag]->{size} - $cur_off) {
  124. push @files, $frag[$frag]->{file};
  125. $cur_size -= $frag[$frag]->{size} - $cur_off;
  126. $cur_off = 0;
  127. die "Cell end beyond VOB data\n" unless ++$frag < @frag;
  128. }
  129. push @files, $frag[$frag]->{file};
  130. my $file = @files == 1 ? $files[0] : "concat:" . join("|", @files);
  131. my $start = $off << 11;
  132. my $end = ($off + $size) << 11;
  133. my $dur = int(1000 * $cell->{length});
  134. $concat .= "\nfile 'subfile:$file'\n";
  135. $concat .= "option start $start\n";
  136. $concat .= "option end $end\n";
  137. $concat .= sprintf "duration %02d:%02d:%02d.%03d\n",
  138. int($dur / 3600000), int($dur / 60000) % 60, int($dur / 1000) % 60,
  139. $dur % 1000;
  140. }
  141. print $concat;