dvd2concat 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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> option.
  32. =cut
  33. use strict;
  34. use warnings;
  35. use Getopt::Long ":config" => "require_order";
  36. use Pod::Usage;
  37. my $title;
  38. GetOptions (
  39. "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
  40. "manpage|m" => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
  41. "title|t=i" => \$title,
  42. ) and @ARGV == 1 or pod2usage({ -verbose => 1, -exitval => 1 });
  43. my ($path) = @ARGV;
  44. my $lsdvd_message =
  45. "Make sure your lsdvd version has the two following patches applied:\n" .
  46. "http://sourceforge.net/p/lsdvd/feature-requests/1/\n" .
  47. "https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=603826\n";
  48. my $lsdvd = do {
  49. open my $l, "-|", "lsdvd", "-Op", "-x", $path
  50. or die "You need to install lsdvd for this script to work.\n$lsdvd_message";
  51. local $/;
  52. <$l>;
  53. };
  54. my %lsdvd = eval $lsdvd;
  55. die $@ if $@;
  56. if (!defined $title) {
  57. $title = $lsdvd{longest_track};
  58. warn "Using longest title $title\n";
  59. }
  60. my $track = $lsdvd{track}[$title - 1]
  61. or die "Title $title does not exist (1-", scalar(@{$lsdvd{track}}), ")\n";
  62. my $vts_base = sprintf "%s/VIDEO_TS/VTS_%02d_", $path, $track->{vts};
  63. my @frag;
  64. for my $i (1 .. 9) {
  65. my $file = sprintf "%s%d.VOB", $vts_base, $i;
  66. my $size = -s $file or last;
  67. push @frag, { file => $file, size => $size >> 11 };
  68. }
  69. my $concat = "ffconcat version 1.0\n";
  70. $concat .= "\nstream\nexact_stream_id 0x1E0\n";
  71. for my $audio (@{$track->{audio}}) {
  72. $concat .= "\nstream\nexact_stream_id " . $audio->{streamid} . "\n";
  73. }
  74. for my $subp (@{$track->{subp}}) {
  75. $concat .= "\nstream\nexact_stream_id " . $subp->{streamid} . "\n";
  76. }
  77. for my $cell (@{$track->{cell}}) {
  78. my $off = $cell->{first_sector};
  79. die "Your lsdvd version does not print cell sectors.\n$lsdvd_message"
  80. unless defined $off;
  81. my $size = $cell->{last_sector} + 1 - $cell->{first_sector};
  82. my $frag = 0;
  83. while ($frag < @frag) {
  84. last if $off < $frag[$frag]->{size};
  85. $off -= $frag[$frag++]->{size};
  86. }
  87. die "Cell beyond VOB data\n" unless $frag < @frag;
  88. my $cur_off = $off;
  89. my $cur_size = $size;
  90. my @files;
  91. while ($cur_size > $frag[$frag]->{size} - $cur_off) {
  92. push @files, $frag[$frag]->{file};
  93. $cur_size -= $frag[$frag]->{size} - $cur_off;
  94. $cur_off = 0;
  95. die "Cell end beyond VOB data\n" unless ++$frag < @frag;
  96. }
  97. push @files, $frag[$frag]->{file};
  98. my $file = @files == 1 ? $files[0] : "concat:" . join("|", @files);
  99. my $start = $off << 11;
  100. my $end = ($off + $size) << 11;
  101. $file = "subfile,,start,${start},end,${end},,:$file";
  102. my $dur = int(1000 * $cell->{length});
  103. $concat .= sprintf "\nfile '%s'\nduration %02d:%02d:%02d.%03d\n", $file,
  104. int($dur / 3600000), int($dur / 60000) % 60, int($dur / 1000) % 60,
  105. $dur % 1000;
  106. }
  107. print $concat;