-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2dvd.pl
173 lines (133 loc) · 4.16 KB
/
v2dvd.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl
# ...........................................................................
# Copyright (C) 2011 Gilberto "Velenux" Ficara <g.ficara@oltrelinux.com>
#
# Released under the GNU GPL license v3.
#
use strict;
use warnings;
use Carp;
use File::Temp qw/ tempfile tempdir /; # standard distribution
# binaries (change as needed)
my $bin_ffmpeg = '/usr/bin/ffmpeg';
my $bin_dvdauthor = '/usr/bin/dvdauthor';
my $bin_genisoimage = '/usr/bin/genisoimage';
# ----------- HERE BE DRAGONS ------------
# some globals
my $dir = tempdir( CLEANUP => 1 );
my $header_dvdauthor = "<dvdauthor dest=\"$dir/dvd\"><vmgm /><titleset><titles>";
my $content_dvdauthor = '';
my $footer_dvdauthor = '</titles></titleset></dvdauthor>';
# check for binaries
if (! -x $bin_ffmpeg or ! -x $bin_dvdauthor or ! -x $bin_genisoimage) {
&print_help();
}
# subs
#
# print help message
#
sub print_help {
print "Usage:
$0 <file1> [<file2> <file3> ...]
This program requires:
- FFMpeg (binary: ffmpeg)
- DVD Author (binary: dvdauthor)
- GenIsoImage (binary: genisoimage)
";
}
#
# extract metadata from video file
#
sub extract_meta {
my $f = shift;
print "Extracting metadata from $f ...\n";
# parse FFMpeg output for metadata
open META, "$bin_ffmpeg -i \"$f\" 2>&1 |"
or carp "Problem extracting metadata from $f, $?, $!";
# length (in seconds), width (pixel), height, framerate
my ($len, $w, $h, $fr);
while (<META>) {
# Duration: 00:08:30.91, start: 0.000000, bitrate: 2488 kb/s
# Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720, 2330 kb/s, 30 fps, 29.97 tbr, 1k tbn, 59.94 tbc
# Duration: 00:03:47.42, start: 0.000000, bitrate: 207 kb/s
# Stream #0.0: Video: h264 (Main), yuv420p, 320x240 [PAR 1:1 DAR 4:3], 157 kb/s, 29.97 tbr, 1k tbn, 59.94 tbc
#
if (/^\s*Duration: (\d+):(\d+):(\d+)\.\d+, start/) {
$len = $3 + ($2 * 60) + ($1 * 60 * 60);
}
if (/^\s*Stream.*Video:.*, (\d+)x(\d+).*, (\d+\.?\d*) tbr/) {
$w = $1;
$h = $2;
$fr = $3;
}
}
#print "DEBUG (extract_meta) - $f - L: $len W: $w H: $h FR: $fr\n";
return ($len, $w, $h, $fr);
}
#
# convert video in a format suitable for DVD
#
sub convert_file {
my ($f, $aspect, $fr) = @_;
my $newfn = $f;
$newfn =~ s/\.[^\.]+$//; # strip extension
$newfn =~ s/.*\///g; # strip path
$newfn .= '.mpg';
print "Converting $f to MPEG...\n";
system($bin_ffmpeg, ('-i', $f, '-r', $fr, '-target', 'dvd', '-copyts', '-aspect', $aspect, "$dir/files/$newfn")) == 0
or carp "Problem converting $f, $?";
return $newfn;
}
#
# add an entry string to dvdauthor file content
#
sub add_dvdauthor_string {
my ($f, $len) = @_;
my $chapter_len = int($len / 5);
my $chapter_str = '0';
my $counter = 0;
while($counter < $len) {
$counter += $chapter_len;
my $m = sprintf "%02d", int($counter / 60);
my $s = sprintf("%02d", $counter % 60);
$chapter_str .= ",$m:$s";
}
# <pgc><vob file="output.mpg" chapters="0,5:00,10:00,15:00,20:00"/></pgc>
$content_dvdauthor .= "<pgc><vob file=\"$dir/files/$f\" chapters=\"$chapter_str\"/></pgc>";
}
# ----------
# main cycle
# ----------
if @ARGV < 1 {
&print_help();
exit 1;
}
# create subdirs
mkdir("$dir/dvd") or carp "Can't create dvd dir in $dir";
mkdir("$dir/files") or carp "Can't create dvd dir in $dir";
foreach my $file (@ARGV) {
if (-f $file and -r $file) {
# extract meta
my ($l, $w, $h, $fr) = &extract_meta($file);
# convert file (file name and aspect ratio)
my $fn = &convert_file($file, ($w/$h), $fr);
# add an entry to dvdauthor xml (new file name and length in seconds)
&add_dvdauthor_string($fn, $l);
} else {
carp "$file unreadable or not a proper file, $!";
}
}
print "Writing dvdauthor XML ...\n";
open my $dvdxml, '>', "$dir/dvdauthor.xml";
print $dvdxml $header_dvdauthor;
print $dvdxml $content_dvdauthor;
print $dvdxml $footer_dvdauthor;
close $dvdxml;
# debug
system("cat $dir/dvdauthor.xml");
print "\n\n";
print "Running dvdauthor ...\n";
system($bin_dvdauthor, '-x', "$dir/dvdauthor.xml");
print "Running genisoimage ...\n";
system($bin_genisoimage, '-dvd-video', '-o', 'dvdfromyt.iso', "$dir/dvd");
# end (tempdir will be removed)