blob: 8effee86b9f3e181cfe828ebc36e29754b4e01f3
1 | #!/usr/bin/env perl |
2 | |
3 | # Copyright (c) 2014 Nicolas George |
4 | # |
5 | # This file is part of FFmpeg. |
6 | # |
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. |
11 | # |
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. |
16 | # |
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 |
20 | |
21 | =head1 NAME |
22 | |
23 | dvd2concat - create a concat script for a DVD title |
24 | |
25 | =head1 SYNOPSIS |
26 | |
27 | tools/dvd2concat I<path/to/dvd/structure> > I<file.concat> |
28 | |
29 | =head1 DESCRIPTION |
30 | |
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. |
34 | |
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 |
37 | with CSS. |
38 | |
39 | I<file.concat> is the output file. It can be used as an input to ffmpeg. |
40 | It will require the B<-safe 0> option. |
41 | |
42 | =cut |
43 | |
44 | use strict; |
45 | use warnings; |
46 | use Getopt::Long ":config" => "require_order"; |
47 | use Pod::Usage; |
48 | |
49 | my $title; |
50 | |
51 | GetOptions ( |
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 }); |
56 | my ($path) = @ARGV; |
57 | |
58 | my $lsdvd_message = |
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"; |
62 | |
63 | my $lsdvd = do { |
64 | open my $l, "-|", "lsdvd", "-Op", "-x", $path |
65 | or die "You need to install lsdvd for this script to work.\n$lsdvd_message"; |
66 | local $/; |
67 | <$l>; |
68 | }; |
69 | my %lsdvd = eval $lsdvd; |
70 | die $@ if $@; |
71 | |
72 | if (!defined $title) { |
73 | $title = $lsdvd{longest_track}; |
74 | warn "Using longest title $title\n"; |
75 | } |
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}; |
79 | my @frag; |
80 | for my $i (1 .. 9) { |
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 }; |
84 | } |
85 | |
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"; |
90 | } |
91 | for my $subp (@{$track->{subp}}) { |
92 | $concat .= "\nstream\nexact_stream_id " . $subp->{streamid} . "\n"; |
93 | } |
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" |
97 | unless defined $off; |
98 | my $size = $cell->{last_sector} + 1 - $cell->{first_sector}; |
99 | |
100 | my $frag = 0; |
101 | while ($frag < @frag) { |
102 | last if $off < $frag[$frag]->{size}; |
103 | $off -= $frag[$frag++]->{size}; |
104 | } |
105 | die "Cell beyond VOB data\n" unless $frag < @frag; |
106 | my $cur_off = $off; |
107 | my $cur_size = $size; |
108 | my @files; |
109 | while ($cur_size > $frag[$frag]->{size} - $cur_off) { |
110 | push @files, $frag[$frag]->{file}; |
111 | $cur_size -= $frag[$frag]->{size} - $cur_off; |
112 | $cur_off = 0; |
113 | die "Cell end beyond VOB data\n" unless ++$frag < @frag; |
114 | } |
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"; |
120 | |
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, |
124 | $dur % 1000; |
125 | } |
126 | |
127 | print $concat; |
128 |