Merge branch 'master' into subfolders-8.3
[pyTivo.git] / plugins / video / transcode.py
blobc9513d635eaa04661a23b4709176530f9dcb3cc0
1 import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache
2 import config
4 info_cache = lrucache.LRUCache(1000)
7 debug = config.getDebug()
8 MAX_VIDEO_BR = config.getMaxVideoBR()
9 BUFF_SIZE = config.getBuffSize()
11 FFMPEG = config.get('Server', 'ffmpeg')
13 def debug_write(data):
14 if debug:
15 debug_out = []
16 for x in data:
17 debug_out.append(str(x))
18 fdebug = open('debug.txt', 'a')
19 fdebug.write(' '.join(debug_out))
20 fdebug.close()
22 # XXX BIG HACK
23 # subprocess is broken for me on windows so super hack
24 def patchSubprocess():
25 o = subprocess.Popen._make_inheritable
27 def _make_inheritable(self, handle):
28 if not handle: return subprocess.GetCurrentProcess()
29 return o(self, handle)
31 subprocess.Popen._make_inheritable = _make_inheritable
32 mswindows = (sys.platform == "win32")
33 if mswindows:
34 patchSubprocess()
36 def output_video(inFile, outFile, tsn=''):
37 if tivo_compatable(inFile):
38 debug_write(['output_video: ', inFile, ' is tivo compatible\n'])
39 f = file(inFile, 'rb')
40 shutil.copyfileobj(f, outFile)
41 f.close()
42 else:
43 debug_write(['output_video: ', inFile, ' is not tivo compatible\n'])
44 transcode(inFile, outFile, tsn)
46 def transcode(inFile, outFile, tsn=''):
48 settings = {}
49 settings['audio_br'] = config.getAudioBR(tsn)
50 settings['video_br'] = config.getVideoBR(tsn)
51 settings['max_video_br'] = MAX_VIDEO_BR
52 settings['buff_size'] = BUFF_SIZE
53 settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn))
55 cmd_string = config.getFFMPEGTemplate(tsn) % settings
57 cmd = [FFMPEG, '-i', inFile] + cmd_string.split()
59 debug_write(['transcode: ffmpeg command is ', ' '.join(cmd), '\n'])
60 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
61 try:
62 shutil.copyfileobj(ffmpeg.stdout, outFile)
63 except:
64 kill(ffmpeg.pid)
66 def select_aspect(inFile, tsn = ''):
67 TIVO_WIDTH = config.getTivoWidth(tsn)
68 TIVO_HEIGHT = config.getTivoHeight(tsn)
70 type, width, height, fps, millisecs = video_info(inFile)
72 debug_write(['tsn:', tsn, '\n'])
74 aspect169 = config.get169Setting(tsn)
76 debug_write(['aspect169:', aspect169, '\n'])
78 optres = config.getOptres()
80 debug_write(['optres:', optres, '\n'])
82 if optres:
83 optHeight = Config.nearestTivoHeight(height)
84 optWidth = Config.nearestTivoWidth(width)
85 if optHeight < TIVO_HEIGHT:
86 TIVO_HEIGHT = optHeight
87 if optWidth < TIVO_WIDTH:
88 TIVO_WIDTH = optWidth
90 d = gcd(height,width)
91 ratio = (width*100)/height
92 rheight, rwidth = height/d, width/d
94 debug_write(['select_aspect: File=', inFile, ' Type=', type, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, ' ratio=', ratio, ' rheight=', rheight, ' rwidth=', rwidth, ' TIVO_HEIGHT=', TIVO_HEIGHT, 'TIVO_WIDTH=', TIVO_WIDTH, '\n'])
96 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH)
97 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH)
99 if (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
100 debug_write(['select_aspect: File is within 4:3 list.\n'])
101 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
102 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169:
103 debug_write(['select_aspect: File is within 16:9 list and 16:9 allowed.\n'])
104 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
105 else:
106 settings = []
107 #If video is wider than 4:3 add top and bottom padding
108 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
109 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
110 if (ratio > 177):#too short needs padding top and bottom
111 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier16by9)
112 settings.append('-aspect')
113 settings.append('16:9')
114 if endHeight % 2:
115 endHeight -= 1
116 if endHeight < TIVO_HEIGHT * 0.99:
117 settings.append('-s')
118 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
120 topPadding = ((TIVO_HEIGHT - endHeight)/2)
121 if topPadding % 2:
122 topPadding -= 1
124 settings.append('-padtop')
125 settings.append(str(topPadding))
126 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
127 settings.append('-padbottom')
128 settings.append(str(bottomPadding))
129 else: #if only very small amount of padding needed, then just stretch it
130 settings.append('-s')
131 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
132 debug_write(['select_aspect: 16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n', ' '.join(settings), '\n'])
133 else: #too skinny needs padding on left and right.
134 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier16by9))
135 settings.append('-aspect')
136 settings.append('16:9')
137 if endWidth % 2:
138 endWidth -= 1
139 if endWidth < (TIVO_WIDTH-10):
140 settings.append('-s')
141 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
143 leftPadding = ((TIVO_WIDTH - endWidth)/2)
144 if leftPadding % 2:
145 leftPadding -= 1
147 settings.append('-padleft')
148 settings.append(str(leftPadding))
149 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
150 settings.append('-padright')
151 settings.append(str(rightPadding))
152 else: #if only very small amount of padding needed, then just stretch it
153 settings.append('-s')
154 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
155 debug_write(['select_aspect: 16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings), '\n'])
156 else: #this is a 4:3 file or 16:9 output not allowed
157 settings.append('-aspect')
158 settings.append('4:3')
159 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier4by3)
160 if endHeight % 2:
161 endHeight -= 1
162 if endHeight < TIVO_HEIGHT * 0.99:
163 settings.append('-s')
164 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
166 topPadding = ((TIVO_HEIGHT - endHeight)/2)
167 if topPadding % 2:
168 topPadding -= 1
170 settings.append('-padtop')
171 settings.append(str(topPadding))
172 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
173 settings.append('-padbottom')
174 settings.append(str(bottomPadding))
175 else: #if only very small amount of padding needed, then just stretch it
176 settings.append('-s')
177 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
178 debug_write(['select_aspect: File is wider than 4:3 padding top and bottom\n', ' '.join(settings), '\n'])
180 return settings
181 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
182 #an aspect ratio of 4:3 since they are so narrow.
183 else:
184 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
185 settings.append('-aspect')
186 settings.append('4:3')
187 if endWidth % 2:
188 endWidth -= 1
189 if endWidth < (TIVO_WIDTH * 0.99):
190 settings.append('-s')
191 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
193 leftPadding = ((TIVO_WIDTH - endWidth)/2)
194 if leftPadding % 2:
195 leftPadding -= 1
197 settings.append('-padleft')
198 settings.append(str(leftPadding))
199 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
200 settings.append('-padright')
201 settings.append(str(rightPadding))
202 else: #if only very small amount of padding needed, then just stretch it
203 settings.append('-s')
204 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
206 debug_write(['select_aspect: File is taller than 4:3 padding left and right\n', ' '.join(settings), '\n'])
208 return settings
210 def tivo_compatable(inFile):
211 suportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
212 type, width, height, fps, millisecs = video_info(inFile)
213 #print type, width, height, fps, millisecs
215 if (inFile[-5:]).lower() == '.tivo':
216 debug_write(['tivo_compatible: ', inFile, ' ends with .tivo\n'])
217 return True
219 if not type == 'mpeg2video':
220 #print 'Not Tivo Codec'
221 debug_write(['tivo_compatible: ', inFile, ' is not mpeg2video it is ', type, '\n'])
222 return False
224 if not fps == '29.97':
225 #print 'Not Tivo fps'
226 debug_write(['tivo_compatible: ', inFile, ' is not correct fps it is ', fps, '\n'])
227 return False
229 for mode in suportedModes:
230 if (mode[0], mode[1]) == (width, height):
231 #print 'Is TiVo!'
232 debug_write(['tivo_compatible: ', inFile, ' has correct width of ', width, ' and height of ', height, '\n'])
233 return True
234 #print 'Not Tivo dimensions'
235 return False
237 def video_info(inFile):
238 mtime = os.stat(inFile).st_mtime
239 if inFile in info_cache and info_cache[inFile][0] == mtime:
240 debug_write(['video_info: ', inFile, ' cache hit!', '\n'])
241 return info_cache[inFile][1]
243 if (inFile[-5:]).lower() == '.tivo':
244 info_cache[inFile] = (mtime, (True, True, True, True, True))
245 debug_write(['video_info: ', inFile, ' ends in .tivo.\n'])
246 return True, True, True, True, True
248 cmd = [FFMPEG, '-i', inFile ]
249 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
251 # wait 4 sec if ffmpeg is not back give up
252 for i in range(80):
253 time.sleep(.05)
254 if not ffmpeg.poll() == None:
255 break
257 if ffmpeg.poll() == None:
258 kill(ffmpeg.pid)
259 info_cache[inFile] = (mtime, (None, None, None, None, None))
260 return None, None, None, None, None
262 output = ffmpeg.stderr.read()
263 debug_write(['video_info: ffmpeg output=', output, '\n'])
265 rezre = re.compile(r'.*Video: ([^,]+),.*')
266 x = rezre.search(output)
267 if x:
268 codec = x.group(1)
269 else:
270 info_cache[inFile] = (mtime, (None, None, None, None, None))
271 debug_write(['video_info: failed at codec\n'])
272 return None, None, None, None, None
274 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
275 x = rezre.search(output)
276 if x:
277 width = int(x.group(1))
278 height = int(x.group(2))
279 else:
280 info_cache[inFile] = (mtime, (None, None, None, None, None))
281 debug_write(['video_info: failed at width/height\n'])
282 return None, None, None, None, None
284 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
285 x = rezre.search(output)
286 if x:
287 fps = x.group(1)
288 else:
289 info_cache[inFile] = (mtime, (None, None, None, None, None))
290 debug_write(['video_info: failed at fps\n'])
291 return None, None, None, None, None
293 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
294 if (not fps == '29.97') and (codec == 'mpeg2video'):
295 # First look for the build 7215 version
296 rezre = re.compile(r'.*film source: 29.97.*')
297 x = rezre.search(output.lower() )
298 if x:
299 debug_write(['video_info: film source: 29.97 setting fps to 29.97\n'])
300 fps = '29.97'
301 else:
302 # for build 8047:
303 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
304 debug_write(['video_info: Bug in VideoReDo\n'])
305 x = rezre.search(output.lower() )
306 if x:
307 fps = '29.97'
309 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
310 d = durre.search(output)
311 if d:
312 millisecs = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
313 else:
314 millisecs = 0
316 info_cache[inFile] = (mtime, (codec, width, height, fps, millisecs))
317 debug_write(['video_info: Codec=', codec, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, '\n'])
318 return codec, width, height, fps, millisecs
320 def suported_format(inFile):
321 if video_info(inFile)[0]:
322 return True
323 else:
324 debug_write(['supported_format: ', inFile, ' is not supported\n'])
325 return False
327 def kill(pid):
328 debug_write(['kill: killing pid=', str(pid), '\n'])
329 if mswindows:
330 win32kill(pid)
331 else:
332 import os, signal
333 os.kill(pid, signal.SIGTERM)
334 def win32kill(pid):
335 import ctypes
336 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
337 ctypes.windll.kernel32.TerminateProcess(handle, -1)
338 ctypes.windll.kernel32.CloseHandle(handle)
340 def gcd(a,b):
341 while b:
342 a, b = b, a % b
343 return a