-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner.py
172 lines (129 loc) · 4.08 KB
/
runner.py
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/env python
"""
FFMPEG Tracker
==============
Tracks an ffmpeg process and communicates the percentage status of when it is complete
uses threading and an output file to track progress.
Usage:
------
code = FuncThread(runTranscode, Command_Line, 'read2.txt')
track = FuncThread(runTracker, 'read2.txt')
code.start()
track.start()
"""
import os
from subprocess import Popen, PIPE, STDOUT
import sys
import re
import time
import threading
def get_framerate(output):
ex = r' \d+.\d+ tb\(r\)'
n = re.compile(ex)
found = n.search(output)
rate = 0.0
if found:
rate = float(found.group(0).replace('tb(r)', '').replace(' ', ''))
return rate
def get_length(output):
ex = r'Duration: \d+:\d+:\d+.\d+'
n = re.compile(ex)
found = n.search(output)
hours, min, sec = 0.0, 0.0, 0.0
if found:
clean = found.group(0).replace('Duration: ', '')
ar = clean.split(':')
hours = float(ar[0])
min = float(ar[1])
sec = float(ar[2])
return hours, min, sec
def get_seconds(hr, min, sec):
secs = ((hr * 60) * 60) + (min * 60) + sec
return secs
def calculate_total_frames(hr, min, sec, frate):
secs = get_seconds(hr, min, sec)
frames = frate * secs
return frames
def get_current_frame(output):
ex = r'frame=\s+\d+'
n = re.compile(ex)
found = n.findall(output)
rate = 0.0
if found:
rate = float(found[len(found) - 1].replace('frame=', '').replace(' ', ''))
return rate
def get_current_time(output):
ex = r'time=\d+.\d+'
n = re.compile(ex)
found = n.findall(output)
time = 0.0
if found:
time = float(found[len(found) - 1].replace('time=', '').replace(' ', ''))
return time
def check_tick(outfile):
perc = 0.0
try:
f = open(outfile, 'rb')
out = f.read()
f.close()
if out.lower().find('error') == -1:
rate = get_framerate(out)
hr, min, sec = get_length(out)
frames = calculate_total_frames(hr, min, sec, rate)
current_frame = get_current_frame(out)
current_time = get_current_time(out)
total_time_seconds = get_seconds(hr, min, sec)
perc = (current_time / total_time_seconds) * 100
perc = int(round(perc))
else:
return -1
except:
pass
return perc
def get_final_output(outfile):
f = open(outfile, 'rb')
out = f.read()
f.close()
return out
# Thready things
class FuncThread(threading.Thread):
"""
subclass of Thread in order to pass values to out transcode an tracking threads
"""
def __init__(self, target, *args):
self._target = target
self._args = args
threading.Thread.__init__(self)
def run(self):
self._target(*self._args)
def runTranscode(commandline, outfile):
# Use the ffmpeg class here to build commands from options
outcode = Popen(commandline, shell=True)
while outcode.poll() == None:
pass
if outcode.poll() == 0:
# All is well
pass
else:
#Report error
print get_final_output(outfile)
# Signal other thread (output may not contain error message, so we
# add one manually just in case)
f = open(outfile, 'a')
f.write('error code:' + (str(outcode.poll())))
f.close()
def runTracker(outfile):
a = 0
while a <= 100:
time.sleep(1)
a = check_tick(outfile)
if a == 100:
print "%i%%" % a
print "Transcode complete!"
#TODO: Cleanup!
break
if a == -1:
print "There was a problem"
#TODO: Cleanup!
break
print "%i%%" % a