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 23dvd2concat - create a concat script for a DVD title 24 25=head1 SYNOPSIS 26 27tools/dvd2concat I<path/to/dvd/structure> > I<file.concat> 28 29=head1 DESCRIPTION 30 31This script uses B<lsdvd> to produce concat script for a DVD title. 32The resulting script can be used to play the DVD using B<ffplay>, to 33transcode it using B<ffmpeg> or any other similar use. 34 35I<path/to/dvd/structure> is the path to the DVD structure hierarchy; it 36normally contains a directory named B<VIDEO_TS>. It must not be encrypted 37with CSS. 38 39I<file.concat> is the output file. It can be used as an input to ffmpeg. 40It will require the B<-safe 0> and 41B<-protocol_whitelist file,subfile,concat> options. 42 43=cut 44 45use strict; 46use warnings; 47use Getopt::Long ":config" => "require_order"; 48use Pod::Usage; 49 50my $title; 51 52GetOptions ( 53 "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) }, 54 "manpage|m" => sub { pod2usage({ -verbose => 2, -exitval => 0 }) }, 55 "title|t=i" => \$title, 56) and @ARGV == 1 or pod2usage({ -verbose => 1, -exitval => 1 }); 57my ($path) = @ARGV; 58 59my $lsdvd_message = 60"Make sure your lsdvd version has the two following patches applied:\n" . 61"http://sourceforge.net/p/lsdvd/feature-requests/1/\n" . 62"https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=603826\n"; 63 64my $lsdvd = do { 65 open my $l, "-|", "lsdvd", "-Op", "-x", $path 66 or die "You need to install lsdvd for this script to work.\n$lsdvd_message"; 67 local $/; 68 <$l>; 69}; 70my %lsdvd = eval $lsdvd; 71die $@ if $@; 72 73if (!defined $title) { 74 $title = $lsdvd{longest_track}; 75 warn "Using longest title $title\n"; 76} 77my $track = $lsdvd{track}[$title - 1] 78 or die "Title $title does not exist (1-", scalar(@{$lsdvd{track}}), ")\n"; 79my $vts_base = sprintf "%s/VIDEO_TS/VTS_%02d_", $path, $track->{vts}; 80my @frag; 81for my $i (1 .. 9) { 82 my $file = sprintf "%s%d.VOB", $vts_base, $i; 83 my $size = -s $file or last; 84 push @frag, { file => $file, size => $size >> 11 }; 85} 86 87my $concat = "ffconcat version 1.0\n"; 88$concat .= "\nstream\nexact_stream_id 0x1E0\n"; 89for my $audio (@{$track->{audio}}) { 90 $concat .= "\nstream\nexact_stream_id " . $audio->{streamid} . "\n"; 91 $concat .= "stream_meta language " . $audio->{langcode} . "\n" if $audio->{langcode}; 92} 93for my $subp (@{$track->{subp}}) { 94 $concat .= "\nstream\nexact_stream_id " . $subp->{streamid} . "\n"; 95 $concat .= "stream_codec dvd_subtitle\n"; 96 $concat .= "stream_meta language " . $subp->{langcode} . "\n" if $subp->{langcode}; 97 my $extradata = ""; 98 if ($track->{width} && $track->{height}) { 99 $extradata .= "size: " . $track->{width} . "x" . $track->{height} . "\n"; 100 } 101 if (my $pal = $track->{palette}) { 102 my @pal; 103 for my $c (@$pal) { 104 # Adapted from mplayer/sub/vobsub.c 105 my $y = ((hex($c) >> 16) & 0xFF); 106 my $u = ((hex($c) >> 8) & 0xFF) - 128; 107 my $v = ((hex($c) >> 0) & 0xFF) - 128; 108 my ($r, $g, $b) = map { int($_ < 0 ? 0 : $_ > 255 ? 255 : $_) } 109 $y + 1.4022 * $u, 110 $y - 0.3456 * $u - 0.7145 * $v, 111 $y + 1.7710 * $v; 112 push @pal, sprintf "%06x", ($r << 16) | ($g << 8) | $b; 113 } 114 $extradata .= "palette: " . join(", ", @pal) . "\n"; 115 } 116 if ($extradata ne "") { 117 $concat .= "stream_extradata " . unpack("H*", $extradata); 118 } 119} 120my $chap_time = 0; 121for my $chap (@{$track->{chapter}}) { 122 $concat .= sprintf "\nchapter %d %.3f %.3f\n", 123 $chap->{ix}, $chap_time, $chap_time + $chap->{length}; 124 $chap_time += $chap->{length}; 125} 126for my $cell (@{$track->{cell}}) { 127 my $off = $cell->{first_sector}; 128 die "Your lsdvd version does not print cell sectors.\n$lsdvd_message" 129 unless defined $off; 130 my $size = $cell->{last_sector} + 1 - $cell->{first_sector}; 131 132 my $frag = 0; 133 while ($frag < @frag) { 134 last if $off < $frag[$frag]->{size}; 135 $off -= $frag[$frag++]->{size}; 136 } 137 die "Cell beyond VOB data\n" unless $frag < @frag; 138 my $cur_off = $off; 139 my $cur_size = $size; 140 my @files; 141 while ($cur_size > $frag[$frag]->{size} - $cur_off) { 142 push @files, $frag[$frag]->{file}; 143 $cur_size -= $frag[$frag]->{size} - $cur_off; 144 $cur_off = 0; 145 die "Cell end beyond VOB data\n" unless ++$frag < @frag; 146 } 147 push @files, $frag[$frag]->{file}; 148 my $file = @files == 1 ? $files[0] : "concat:" . join("|", @files); 149 my $start = $off << 11; 150 my $end = ($off + $size) << 11; 151 152 my $dur = int(1000 * $cell->{length}); 153 $concat .= "\nfile 'subfile:$file'\n"; 154 $concat .= "option start $start\n"; 155 $concat .= "option end $end\n"; 156 $concat .= sprintf "duration %02d:%02d:%02d.%03d\n", 157 int($dur / 3600000), int($dur / 60000) % 60, int($dur / 1000) % 60, 158 $dur % 1000; 159} 160 161print $concat; 162