1
|
#!/usr/bin/perl
|
2
|
# Make some edits so multiple copies may run concurrent
|
3
|
#---------------------------------------------------------------------------------
|
4
|
# USE 'S section
|
5
|
#---------------------------------------------------------------------------------
|
6
|
use FileHandle;
|
7
|
use IPC::Open2;
|
8
|
use Socket;
|
9
|
use POSIX;
|
10
|
use Time::Local;
|
11
|
use Getopt::Long;
|
12
|
local ( $starttime,$stoptime,$elapsetime );
|
13
|
my ($t_sec,$t_min,$t_hour,$t_day,$t_mon,$t_year,$t_wday,$t_yday,$t_isdst);
|
14
|
|
15
|
# parse the connamd line options
|
16
|
@optl = ( "f=s","bg" );
|
17
|
die "Usage $0 -f <filename>\n" unless GetOptions @optl;
|
18
|
|
19
|
select(STDOUT); $|=1;
|
20
|
# Initialize some default variables
|
21
|
$prc_config{LOGNAME} = "mk_mkv_tv";
|
22
|
$prc_config{LOGINT} = "1d";
|
23
|
$prc_config{LOGDIR} = "/var/log";
|
24
|
$prc_config{BINDIR} = "/usr/local/bin";
|
25
|
$prc_config{DEBUGDIR} = "/var/log";
|
26
|
$prc_config{ROLLBACK} = 360;
|
27
|
$prc_config{PROCNAME} = 'comskip';
|
28
|
$prc_config{PROCINT} ="5m";
|
29
|
$prc_config{LOGSIZE} = 50000 ;
|
30
|
$prc_config{DEBUGSIZE} = 50000 ;
|
31
|
$prc_config{BLOCKTIME} = 5 ;
|
32
|
$prc_config{PROCESSNAME} = 'mk_mkv_tv' ;
|
33
|
|
34
|
$PRC_BASE = $prc_config{LOGNAME};
|
35
|
$PRC_LOGA = $PRC_BASE . ".log";
|
36
|
$PRC_DBG = $PRC_BASE . ".dbg";
|
37
|
$prc_active_log = $PRC_LOGA;
|
38
|
$prc_debug_file = $PRC_DBG;
|
39
|
$prc_block_time = $prc_config{BLOCKTIME};
|
40
|
# die "Process is already running or aborted abnormally remove $prc_config{LOGDIR}/$0.pid" if ( -e "$prc_config{LOGDIR}/$0.pid" );
|
41
|
# Calculate the number of seconds to wait based on the LOGINT setting.
|
42
|
( $intnum, $inttype ) = ( $prc_config{LOGINT} =~ /(\d+)(\w)/i );
|
43
|
$intsize = 1 if ( uc($inttype) eq 'S' );
|
44
|
$intsize = 60 if ( uc($inttype) eq 'M' );
|
45
|
$intsize = 3600 if ( uc($inttype) eq 'H' );
|
46
|
$intsize = 86400 if ( uc($inttype) eq 'D' );
|
47
|
$intsize = ( 86400 * 7 ) if ( uc($inttype) eq 'W' );
|
48
|
# Calculate the number of seconds to wait based on the PROCINT setting.
|
49
|
if ( $opt_bg )
|
50
|
{
|
51
|
# Fix up the IO for a background job
|
52
|
chdir("/tmp");
|
53
|
$ctermid = POSIX::ctermid();
|
54
|
open( TTY, $ctermid );
|
55
|
$cpgid = POSIX::tcgetpgrp(TTY);
|
56
|
close TTY;
|
57
|
$orig_pid = fork;
|
58
|
exit if ( $orig_pid );
|
59
|
print "$0 started with process ID $$ detaching from foreground\n";
|
60
|
close STDIN;
|
61
|
close STDOUT;
|
62
|
close STDERR;
|
63
|
$debug0 = 0; # Inside functions debugging read_config_file
|
64
|
$debug1 = 0; # Main program debugging
|
65
|
$debug2 = 0; # Inside functions debugging
|
66
|
$debug3 = 0; # Inside decode_nd function debugging for parsing
|
67
|
$debug4 = 0; # Inside chat extended logging
|
68
|
$debug5 = 0; # sql send/expect to debug file
|
69
|
$debug6 = 0; # chat send/expect to debug file
|
70
|
$my_pgid = POSIX::setsid();
|
71
|
open( STDOUT ,">>$prc_config{LOGDIR}/$0.log");
|
72
|
open( STDERR ,">>$prc_config{LOGDIR}/$0.err");
|
73
|
$ctermid = POSIX::ctermid();
|
74
|
open( TTY, $ctermid );
|
75
|
$cpgid = POSIX::tcgetpgrp(TTY);
|
76
|
close TTY;
|
77
|
# print "The controlling terminal is $ctermid with pgid of $cpgid and setsid returned $my_pgid and I am $$ \n";
|
78
|
# Note that this is VERY important. It provides a way to be SURE that the processes we start are ours later on.
|
79
|
# each process that is started from within this process will now have a process group ID of our PID.
|
80
|
# When we check for a process we can check that the PID lives ( with kill 0 ) and that it is our pid and not a transient
|
81
|
# ( with getpgrp(somepid) ) should come back with our PID if we started it or one of its parents.
|
82
|
} else
|
83
|
{
|
84
|
print "$0:$$:$prc_config{PROCNAME} \n";
|
85
|
print "$0:$$:Logging to $prc_active_log with $prc_config{LOGINT} archive interval\n";
|
86
|
}
|
87
|
MAIN: {
|
88
|
# Main is in a block though it does not loop it easily could.
|
89
|
# Get and format the time
|
90
|
$starttime = time;
|
91
|
$epochtime = $starttime;
|
92
|
($t_sec,$t_min,$t_hour,$t_day,$t_mon,$t_year,$t_wday,$t_yday,$t_isdst) = localtime($starttime);
|
93
|
$newtime = $starttime - ( $t_isdst * 3600 );
|
94
|
($t_sec,$t_min,$t_hour,$t_day,$t_mon,$t_year,$t_wday,$t_yday,$t_isdst) = localtime($newtime);
|
95
|
$t_mon++;
|
96
|
|
97
|
$t_sec = '0' . $t_sec if ( length($t_sec) == 1 );
|
98
|
$t_min = '0' . $t_min if ( length($t_min) == 1 );
|
99
|
$t_hour = '0' . $t_hour if ( length($t_hour) == 1 );
|
100
|
$t_day = '0' . $t_day if ( length($t_day) == 1 );
|
101
|
$t_mon = '0' . $t_mon if ( length($t_mon) == 1 );
|
102
|
$t_year = 1900 + $t_year;
|
103
|
|
104
|
$start_dt_stamp = "_" . $t_mon . $t_day . $t_year . "_" . $t_hour . $t_min . $t_sec;
|
105
|
$rolltime = $epochtime + ( $intnum * $intsize );
|
106
|
# Create a "pid" file containing our process ID so we may be stopped easily
|
107
|
# open (FHLOG,">$prc_config{LOGDIR}/$0.pid");autoflush FHLOG 1;
|
108
|
# print {FHLOG} "$0:$$\n";
|
109
|
# close FHLOG;
|
110
|
|
111
|
# Open the Logfile , Debug File and a Raw file
|
112
|
open (FHLOG,">>$prc_config{LOGDIR}/$prc_active_log");autoflush FHLOG 1;
|
113
|
if ( $opt_raw ) { open (FHRAW,">>$prc_config{LOGDIR}/$prc_active_log.raw");autoflush FHRAW 1;}
|
114
|
open (FHDBG,">>$prc_config{DEBUGDIR}/$prc_debug_file");autoflush FHDBG 1;
|
115
|
print {FHDBG} "$0:$$:$t_year\/$t_mon\/$t_day $t_hour:$t_min:$t_sec\n";
|
116
|
print {FHDBG} "$0:$$:Set to roll logs at localtime($rolltime)\n ";
|
117
|
$mfile = $opt_f;
|
118
|
$mediadst = `dirname $mfile`;
|
119
|
$mediatitle = $mfile;
|
120
|
$mediatitle =~ s/\.mkv|\.ts/\.mkv/;
|
121
|
$retuse = `fuser "$mfile"`;
|
122
|
if ( $retuse == '' )
|
123
|
{
|
124
|
print "$mfile not in use .. start encode to $mediatitle\n";
|
125
|
# Since comskip only appears to like MPEG this will probably never happen - no mkv to start allways MPEG_TS
|
126
|
if ( $mfile =~ /\.mkv/ ) # change to Switch to decide if blueray or dvd source
|
127
|
{
|
128
|
$enc_opts = '-map 0 -map -0:a:1 -c:v libx264 -s:v:0 640x480 -c:a:0 libfdk_aac -flags +qscale -global_quality 3 -afterburner 1 -ac:0 2 -c:s copy -crf 24';
|
129
|
$hbret = `/usr/bin/ffmpeg -i "${mfile}" -preset veryslow ${enc_opts} "${mediatitle}"`;
|
130
|
}
|
131
|
if ( $mfile =~ /\.ts/ ) # change to Switch to decide if mpeg transport raw ts source
|
132
|
{
|
133
|
$retcom = `$prc_config{BINDIR}/comskip --ini $prc_config{BINDIR}/comskip_ota.ini "${mfile}" $mediadst > /var/log/comskip.$$.log`;
|
134
|
$medialog = ${mfile};
|
135
|
$medialog =~ s/\.ts/\.log/;
|
136
|
$retcom = `rm "${medialog}"`; # Annoying comskip litter
|
137
|
$retcom = `mv /var/log/comskip.$$.log /var/log/comskip.log`; # Annoying comskip litter
|
138
|
$medialog =~ s/\.log/\.edl/;
|
139
|
# $enc_opts = '-map 0 -map -0:a:1 -c:v libx264 -s:v:0 640x480 -c:a:0 libfdk_aac -flags +qscale -global_quality 3 -afterburner 1 -ac:0 2 -c:s copy -crf 24';
|
140
|
# $hbret = `/usr/bin/ffmpeg -i "${mfile}" -preset veryslow ${enc_opts} "${mediatitle}"`;
|
141
|
$enc_opts = 'bframes=8:b-adapt=2:direct=auto:me=umh:merange=24:partitions=all:rc-lookahead=60:ref=16:subme=10:trellis=2:psy-rd=1.0,0.2';
|
142
|
$hbret = `/usr/bin/HandBrakeCLI -i ${mfile} -I -v -e x264 -q 24 -w 640 --loose-anamorphic -a 1 -E faac -B 128 -f mkv -N eng -s 1,2 -m -x ${enc_opts} -o "${mediatitle}"`;
|
143
|
print FHLOG "$0:$$:encode run :\n$hbret\n ";
|
144
|
$orig_dur = `/usr/bin/ffmpeg -i "${mfile}" 2>&1 | awk '/Duration:/ { print $2 }'`;
|
145
|
$new_dur = `/usr/bin/ffmpeg -i "${mediatitle}" 2>&1 | awk '/Duration:/ { print $2 }'`;
|
146
|
$orig_dur =~ s/(\d+:\d+):\d+?\..*/\1/;
|
147
|
$new_dur =~ s/(\d+:\d+):\d+?\..*/\1/;
|
148
|
# Delete large TS when testing complete
|
149
|
# Use ffmpeg to get HH:MM and make sure we have the full recording
|
150
|
$hbret = `rm ${mfile}` if ( $orig_dur eq $new_dur );
|
151
|
$htsid = `grep -l ${mfile} /root/.hts/tvheadend/dvr/log/*`;
|
152
|
$htsret = `htspdel --id $htsid`;
|
153
|
#$mnret = `mencoder "${mediatitle}" -o "${mediatitle}".mp4 -edl $medialog -ovc copy -oac copy -slang eng`;
|
154
|
}
|
155
|
} else
|
156
|
{
|
157
|
print "$mfile in use .. skip encode to $mediatitle\n $retuse\n\n";
|
158
|
sleep 60;
|
159
|
goto MAIN;
|
160
|
};
|
161
|
} # MAIN
|
162
|
|
163
|
exit 0;
|
164
|
|
165
|
|
166
|
|
167
|
--cut NOTES for later
|
168
|
|
169
|
|
170
|
# Note: --start-at duration:150 --stop-at duration:300 or --start-at frame:2000 --stop-at frame:5000
|
171
|
Mencoder will process -edl list from comskip
|