-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvidconv.pl
executable file
·235 lines (183 loc) · 5.82 KB
/
vidconv.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl
# This program runs simple, standard video transcodings.
# It provides a simple, sane interface to the most common transcoding tasks,
# something that ffmpeg spectactually fails to do.
#
# Hack: assumes a single video track, track 1, so that can subtract
# the number of the audio track by 1 (say, 4th - 1 = 3rd audio track)
# Bug: The "Delay relative to Video" is NOT the audio delay cf the video
# It the delay of the start of the audio cf the start of the video in
# order to have no audio - video offset
# TODO: Supress the selection of an English soundtrack in a multi-soundtrack file
use common::sense;
use IO::All;
use autodie;
use English;
use charnames qw< :full >;
$0 = basename($0); # shorter messages
$| = 1; # autoflush: solving perl's stupidities 1 by 1
my @OkFormats = ("avi", "mp4", "mkv", "mov", "divx", "wmv", "mpeg", "ogm");
# --- commandline parametes ---
use Getopt::Long qw(:config no_ignore_case bundling);
my $cmdLineDelayOfAudio;
my $singleInfile ;
my $showHelp = 0;
my $informat ;
my $outformat = "avi";
my $okOptions = GetOptions(
"delayAudio:f" => \$cmdLineDelayOfAudio, # -conf optional string
"i:s" => \$singleInfile, # -b compulsary int
"h" => \$showHelp, # show the usage help
"informat=s" => \$informat,
"outformat=s" => \$outformat,
);
# --- Useage message
my $shortProgName = `basename $PROGRAM_NAME`;
chomp $shortProgName;
my $usageMsg = <<USAGE;
Usage:
$shortProgName
[--informat xyz ] # which *.xyz files in dir to process
[-i inFile.zzz ] # transcode a single file
[--outformat abc ] # vids' output format (default: "avi")
[--delayAudio 2.3 ] # delay or advance ( -2.3s ) the audio
[ -h ] # show this message
This program transcodes videos into other formats, *.avi by default
The formats can be any of: "@OkFormats"
Either '-i infile.zzz', or '--informat yyy' must be specified.
USAGE
# --- test the commandline input
if ($showHelp) {
print $usageMsg;
exit(0);
}
if (! $okOptions) {
print $usageMsg;
exit(0);
}
if (!$informat && !$singleInfile){
print "either --informat or -i/--infile \n";
print $usageMsg;
exit(1);
}
if (!$singleInfile) { # informat
if (! (grep(/$informat/i,@OkFormats))){
print "can not process $informat, only @OkFormats\n";
print $usageMsg;
exit(1);
}
}
if (! (grep(/$outformat/i,@OkFormats))){
print "can not produce $informat files, only @OkFormats\n";
print $usageMsg;
exit(1);
}
# - test that required external programs are present
my $mediainfoPresent = `which mediainfo`;
chomp $mediainfoPresent;
if (! -x $mediainfoPresent ){
print "an executable copy of mediainfo is required\n";
print $usageMsg;
exit (1);
}
my $ffprobePresent = `which ffprobe`;
chomp $ffprobePresent;
if (! -x $ffprobePresent ){
print "an executable copy of ffprobe is required\n";
print $usageMsg;
exit (1);
}
my $ffmpegPresent = `which ffmpeg`;
chomp $ffmpegPresent;
if (! -x $ffmpegPresent ){
print "an executable copy of ffmpeg is required\n";
print $usageMsg;
exit (1);
}
# - Bugs:
# should test that $delayAudio is a number
# should test that the infile exists
my ($outfile, $audiotrackSelector, $videotrackSelector );
# get the file list to transcode
my @infiles;
if ($singleInfile) {
@infiles[0] = $singleInfile;
} else {
@infiles = io->dir('.')->glob("*.$informat");
@infiles = map{ $_->name } @infiles;
}
# --- do the transcoding
foreach my $thisInFile (@infiles){
# (1) identify any sound - video offset
my $delayOfAudio = 0;
if ($cmdLineDelayOfAudio) {
$delayOfAudio = $cmdLineDelayOfAudio;
}
else {
my @mediainfoResults = `/usr/bin/mediainfo $thisInFile `;
chomp @mediainfoResults;
# read a block - ends at an empty line
my ($englishLine, $delayedLine);
foreach my $line (@mediainfoResults){
chomp $line;
$englishLine = $line
if ($line =~ /Language\s*: English/);
$delayedLine = $line
if ($line =~ /Delay relative to video/);
# at end-of-block : and empty line. Is there are delay noted?
if (0 == length($line)) {
if (($englishLine) && ($delayedLine)) {
$delayedLine =~ /: (\d*) s (\d*) ms/;
$delayOfAudio = $1 + 0.00 + $2/1000;
} else {
$delayedLine = $englishLine = ();
}
}
}
}
# because need two input files to delay the audio
my $audioInputFile = $delayOfAudio?1:0;
# (2) identify the english audiotrack
my @ffprobeData = `/usr/bin/ffprobe $thisInFile 2>&1 `;
chomp @ffprobeData;
my ($audioline) = grep (/Stream.+\(eng\).+Audio/,@ffprobeData);
chomp $audioline;
my $audiotrack = "a";
if ($audioline) { # a special "eng" audio line was found
$audioline =~ /0:(\d)/;
$audiotrack = $1 ;
}
$videotrackSelector = "0:0:v"; # assume one video track
$audiotrackSelector = ($audiotrack eq "a")
? "$audioInputFile:a" : "$audioInputFile:$audiotrack:a";
# (3) make the outfile's name
my $outfile = ($thisInFile =~ s/\.*$/\.avi/r);
`rm -f $outfile`;
# (4) do transcoding
# subcommand for delaying the audio
# adds the audio as a second copy of the infile
# itsoffset has to be before the file it refers to
# HACK - delay to 1 sec
my $delayAudioCmd = ($delayOfAudio)
? " -itsoffset $delayOfAudio -i \"$thisInFile\" "
: " ";
my $cmd =" "
. " /usr/bin/nice -n 20 /usr/bin/ffmpeg -i \"$thisInFile\" "
. $delayAudioCmd
. " -map $videotrackSelector -map $audiotrackSelector "
. " -f $outformat -max_muxing_queue_size 4000 "
. " -filter:v fps=20 -r 20 -c:v libxvid -b:v 2000k "
. " -vtag xvid "
. " -c:a libmp3lame -b:a 100k "
. " -loglevel error "
. " \"$outfile\" ";
print "\n".`date`;
print $cmd."\n";
my $errorVals = `$cmd 2>&1`;
print "cmd output: $errorVals \n";
print `date`."\n";
sleep 10 ;
}
# Want B-frames ?
# -g 300 -bf 2
#