dvd2concat 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_meta language " . $subp->{langcode} . "\n" if $subp->{langcode};
  79. }
  80. for my $cell (@{$track->{cell}}) {
  81. my $off = $cell->{first_sector};
  82. die "Your lsdvd version does not print cell sectors.\n$lsdvd_message"
  83. unless defined $off;
  84. my $size = $cell->{last_sector} + 1 - $cell->{first_sector};
  85. my $frag = 0;
  86. while ($frag < @frag) {
  87. last if $off < $frag[$frag]->{size};
  88. $off -= $frag[$frag++]->{size};
  89. }
  90. die "Cell beyond VOB data\n" unless $frag < @frag;
  91. my $cur_off = $off;
  92. my $cur_size = $size;
  93. my @files;
  94. while ($cur_size > $frag[$frag]->{size} - $cur_off) {
  95. push @files, $frag[$frag]->{file};
  96. $cur_size -= $frag[$frag]->{size} - $cur_off;
  97. $cur_off = 0;
  98. die "Cell end beyond VOB data\n" unless ++$frag < @frag;
  99. }
  100. push @files, $frag[$frag]->{file};
  101. my $file = @files == 1 ? $files[0] : "concat:" . join("|", @files);
  102. my $start = $off << 11;
  103. my $end = ($off + $size) << 11;
  104. my $dur = int(1000 * $cell->{length});
  105. $concat .= "\nfile 'subfile:$file'\n";
  106. $concat .= "option start $start\n";
  107. $concat .= "option end $end\n";
  108. $concat .= sprintf "duration %02d:%02d:%02d.%03d\n",
  109. int($dur / 3600000), int($dur / 60000) % 60, int($dur / 1000) % 60,
  110. $dur % 1000;
  111. }
  112. print $concat;