3 # Copyright (c) 2014 Nicolas George
5 # This file is part of FFmpeg.
7 # FFmpeg is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public License
9 # as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # FFmpeg is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 dvd2concat - create a concat script for a DVD title
27 tools/dvd2concat I<path/to/dvd/structure> > I<file.concat>
31 This script uses B<lsdvd> to produce concat script for a DVD title.
32 The resulting script can be used to play the DVD using B<ffplay>, to
33 transcode it using B<ffmpeg> or any other similar use.
35 I<path/to/dvd/structure> is the path to the DVD structure hierarchy; it
36 normally contains a directory named B<VIDEO_TS>. It must not be encrypted
39 I<file.concat> is the output file. It can be used a input to ffmpeg.
40 It will require the B<-safe 0> option.
46 use Getopt::Long ":config" => "require_order";
52 "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
53 "manpage|m" => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
54 "title|t=i" => \$title,
55 ) and @ARGV == 1 or pod2usage({ -verbose => 1, -exitval => 1 });
59 "Make sure your lsdvd version has the two following patches applied:\n" .
60 "http://sourceforge.net/p/lsdvd/feature-requests/1/\n" .
61 "https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=603826\n";
64 open my $l, "-|", "lsdvd", "-Op", "-x", $path
65 or die "You need to install lsdvd for this script to work.\n$lsdvd_message";
69 my %lsdvd = eval $lsdvd;
72 if (!defined $title) {
73 $title = $lsdvd{longest_track};
74 warn "Using longest title $title\n";
76 my $track = $lsdvd{track}[$title - 1]
77 or die "Title $title does not exist (1-", scalar(@{$lsdvd{track}}), ")\n";
78 my $vts_base = sprintf "%s/VIDEO_TS/VTS_%02d_", $path, $track->{vts};
81 my $file = sprintf "%s%d.VOB", $vts_base, $i;
82 my $size = -s $file or last;
83 push @frag, { file => $file, size => $size >> 11 };
86 my $concat = "ffconcat version 1.0\n";
87 $concat .= "\nstream\nexact_stream_id 0x1E0\n";
88 for my $audio (@{$track->{audio}}) {
89 $concat .= "\nstream\nexact_stream_id " . $audio->{streamid} . "\n";
91 for my $subp (@{$track->{subp}}) {
92 $concat .= "\nstream\nexact_stream_id " . $subp->{streamid} . "\n";
94 for my $cell (@{$track->{cell}}) {
95 my $off = $cell->{first_sector};
96 die "Your lsdvd version does not print cell sectors.\n$lsdvd_message"
98 my $size = $cell->{last_sector} + 1 - $cell->{first_sector};
101 while ($frag < @frag) {
102 last if $off < $frag[$frag]->{size};
103 $off -= $frag[$frag++]->{size};
105 die "Cell beyond VOB data\n" unless $frag < @frag;
107 my $cur_size = $size;
109 while ($cur_size > $frag[$frag]->{size} - $cur_off) {
110 push @files, $frag[$frag]->{file};
111 $cur_size -= $frag[$frag]->{size} - $cur_off;
113 die "Cell end beyond VOB data\n" unless ++$frag < @frag;
115 push @files, $frag[$frag]->{file};
116 my $file = @files == 1 ? $files[0] : "concat:" . join("|", @files);
117 my $start = $off << 11;
118 my $end = ($off + $size) << 11;
119 $file = "subfile,,start,${start},end,${end},,:$file";
121 my $dur = int(1000 * $cell->{length});
122 $concat .= sprintf "\nfile '%s'\nduration %02d:%02d:%02d.%03d\n", $file,
123 int($dur / 3600000), int($dur / 60000) % 60, int($dur / 1000) % 60,