-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_mp4.pl
executable file
·155 lines (117 loc) · 3.66 KB
/
batch_mp4.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
#! /usr/bin/perl
# hdtv, anim, movie
# usage: batch_mp4.pl <preset>
use strict;
use File::Copy;
my $preset = lc $ARGV[0];
my $doit = lc $ARGV[1];
my $indir = "/opt/cli_handbrake/drop/";
my $outdir = "/opt/cli_handbrake/dump/";
my $ext = "TS";
my $ext1 = "mp4";
my $hbcli = "/usr/bin/HandBrakeCLI";
$hbcli = "/opt/cli_handbrake/bin/HandBrakeCLI";
my $cmd = "ls -1 $indir | egrep '\.(mp4|MP4|M2TS|TS)'";
print "CMD: $cmd\n";
my @files = `$cmd`;
my $flags = &get_flags($preset);
my @test;
my $tmpVar = 0;
my $count=@files;
$SIG{INT} = sub {
print "Yup.. we're outta here!\n";
exit();
};
print "\n$count Files to process:\n\n";
print "################\n";
foreach my $foo (@files) {
chomp $foo;
if ($foo =~ /\.$ext$/) {
print "$foo\n";
} else {
print "$foo : Wont process not a .$ext file\n";
}
}
print "\n\n";
while (@files ne @test) {
if ($tmpVar > 0) { @files = @test; }
&process;
@test = `ls -1 $indir | grep .$ext`;
$tmpVar++;
}
sub process {
print "Starting processing...\n\n";
foreach my $infile (@files) {
chomp $infile;
print "FILE : $infile\n\n";
my $foo = $infile;
$foo =~ s/\.$ext$//;
print "$foo\n\n";
my $outfile = $foo . ".$ext1";
my $logfile = $foo . ".log";
if (-e "$outdir/$outfile") {
print "$outdir/$outfile -- EXISTS!!!! SKIPPING!!!\n\n";
if (-e "$outdir/$foo.lock") { next; }
elsif ($doit eq "doit") { move ("$indir/$infile", "$indir/done/"); }
next;
}
if (-e "$outdir/$foo.lock") {
print "FILE processed by parallel process\n\n";
} else {
# lock
system ("touch '$outdir/$foo.lock'");
my $cmd = "$hbcli $flags -i \"$indir/$infile\" -o \"$outdir/$outfile\"";
print "CLI CMD: ---> $cmd\n\n";
print "Command: $cmd\n";
if ($doit eq "doit") { system($cmd); }
if (-e "$outdir/$outfile") {
print "Encode seems to have produced something!\n";
if ($doit eq "doit") {
# make the done directory if it doesn't already exist
mkdir "$indir/done" if (! -e "$indir/done/");
move ("$indir/$infile", "$indir/done/");
}
} else {
# error detected
#system ("mv \"$infile\" error");
}
# unlock
# unlink "$outdoor/$foo.lock";
system ("rm '$outdir/$foo.lock'");
}
}
}
sub get_flags {
my $preset = shift;
my ($x264_opts,$video_opts,$audio_opts);
my @psets;
my %validArgs = (
'hdtv' => 1,
'anim' => 1,
'movie' => 1
);
foreach my $foo (sort keys %validArgs) {
push (@psets, $foo);
}
my $pset = join ('|',@psets);
if (not defined $validArgs{$preset}) {
print "\n\t --usage $0 preset [doit]\n\n";
print "\t $0 $pset [doit]\n\n";
exit 1;
}
if ($preset eq "hdtv") {
$x264_opts = 'mixed-refs:bframes=6:weightb:direct=auto:b-pyramid=0:me=umh:subme=9:analyse=all:nr=150:no-fast-pskip=1:psy-rd=1,1';
$video_opts = "-b 1500";
$audio_opts = "-E copy";
} elsif ($preset eq "anim") {
$x264_opts = 'mixed-refs:bframes=6:weightb:direct=auto:b-pyramid=0:me=umh:subme=9:analyse=all:nr=150:no-fast-pskip=1:psy-rd=1,1';
$video_opts = "-b 1000";
$audio_opts = "-E copy";
} elsif ($preset eq "movie") {
$x264_opts = 'mixed-refs:bframes=6:weightb:direct=auto:b-pyramid=0:me=umh:subme=9:analyse=all:nr=150:no-fast-pskip=1:psy-rd=1,1';
$video_opts = "-b 3000";
$audio_opts = "-E copy";
}
my $flags = "--optimize -f $ext1 -m -2 -T -e x264 -x $x264_opts $video_opts $audio_opts";
return($flags);
}