move codec function
[pyTivo/wmcbrine.git] / plugins / video / transcode.py
blob7854bb657e263b9fdbd3755949215b290d1d69fb
1 import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache
2 import config
4 info_cache = lrucache.LRUCache(1000)
7 debug = config.getDebug()
8 BUFF_SIZE = config.getBuffSize()
10 FFMPEG = config.get('Server', 'ffmpeg')
12 def debug_write(data):
13 if debug:
14 debug_out = []
15 for x in data:
16 debug_out.append(str(x))
17 fdebug = open('debug.txt', 'a')
18 fdebug.write(' '.join(debug_out))
19 fdebug.close()
21 # XXX BIG HACK
22 # subprocess is broken for me on windows so super hack
23 def patchSubprocess():
24 o = subprocess.Popen._make_inheritable
26 def _make_inheritable(self, handle):
27 if not handle: return subprocess.GetCurrentProcess()
28 return o(self, handle)
30 subprocess.Popen._make_inheritable = _make_inheritable
31 mswindows = (sys.platform == "win32")
32 if mswindows:
33 patchSubprocess()
35 def output_video(inFile, outFile, tsn=''):
36 if tivo_compatable(inFile, tsn):
37 debug_write(['output_video: ', inFile, ' is tivo compatible\n'])
38 f = file(inFile, 'rb')
39 shutil.copyfileobj(f, outFile)
40 f.close()
41 else:
42 debug_write(['output_video: ', inFile, ' is not tivo compatible\n'])
43 transcode(inFile, outFile, tsn)
45 def transcode(inFile, outFile, tsn=''):
47 settings = {}
48 settings['audio_br'] = config.getAudioBR(tsn)
49 settings['audio_codec'] = select_audiocodec(tsn)
50 settings['video_br'] = config.getVideoBR(tsn)
51 settings['max_video_br'] = config.getMaxVideoBR()
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()
58 print cmd
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_audiocodec(tsn = ''):
67 #check for HD tivo and return compatible audio parameters
68 if tsn and tsn[:3] in config.getHDtivos():
69 return '-acodec ac3 -ar 48000'
70 else:
71 return '-acodec mp2 -ac 2 -ar 44100'
73 def select_aspect(inFile, tsn = ''):
74 TIVO_WIDTH = config.getTivoWidth(tsn)
75 TIVO_HEIGHT = config.getTivoHeight(tsn)
77 type, width, height, fps, millisecs, kbps, akbps = video_info(inFile)
79 debug_write(['tsn:', tsn, '\n'])
81 aspect169 = config.get169Setting(tsn)
83 debug_write(['aspect169:', aspect169, '\n'])
85 optres = config.getOptres()
87 debug_write(['optres:', optres, '\n'])
89 if optres:
90 optHeight = config.nearestTivoHeight(height)
91 optWidth = config.nearestTivoWidth(width)
92 if optHeight < TIVO_HEIGHT:
93 TIVO_HEIGHT = optHeight
94 if optWidth < TIVO_WIDTH:
95 TIVO_WIDTH = optWidth
97 d = gcd(height,width)
98 ratio = (width*100)/height
99 rheight, rwidth = height/d, width/d
101 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'])
103 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH)
104 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH)
106 if tsn[:3] in config.getHDtivos() and height <= TIVO_HEIGHT and config.getOptres() == False:
107 return [] #pass all resolutions to S3/HD, except heights greater than conf height
108 # else, optres is enabled and resizes SD video to the "S2" standard on S3/HD.
109 elif (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
110 debug_write(['select_aspect: File is within 4:3 list.\n'])
111 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
112 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169:
113 debug_write(['select_aspect: File is within 16:9 list and 16:9 allowed.\n'])
114 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
115 else:
116 settings = []
117 #If video is wider than 4:3 add top and bottom padding
118 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
119 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
120 if (ratio > 177):#too short needs padding top and bottom
121 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier16by9)
122 settings.append('-aspect')
123 settings.append('16:9')
124 if endHeight % 2:
125 endHeight -= 1
126 if endHeight < TIVO_HEIGHT * 0.99:
127 settings.append('-s')
128 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
130 topPadding = ((TIVO_HEIGHT - endHeight)/2)
131 if topPadding % 2:
132 topPadding -= 1
134 settings.append('-padtop')
135 settings.append(str(topPadding))
136 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
137 settings.append('-padbottom')
138 settings.append(str(bottomPadding))
139 else: #if only very small amount of padding needed, then just stretch it
140 settings.append('-s')
141 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
142 debug_write(['select_aspect: 16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n', ' '.join(settings), '\n'])
143 else: #too skinny needs padding on left and right.
144 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier16by9))
145 settings.append('-aspect')
146 settings.append('16:9')
147 if endWidth % 2:
148 endWidth -= 1
149 if endWidth < (TIVO_WIDTH-10):
150 settings.append('-s')
151 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
153 leftPadding = ((TIVO_WIDTH - endWidth)/2)
154 if leftPadding % 2:
155 leftPadding -= 1
157 settings.append('-padleft')
158 settings.append(str(leftPadding))
159 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
160 settings.append('-padright')
161 settings.append(str(rightPadding))
162 else: #if only very small amount of padding needed, then just stretch it
163 settings.append('-s')
164 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
165 debug_write(['select_aspect: 16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings), '\n'])
166 else: #this is a 4:3 file or 16:9 output not allowed
167 settings.append('-aspect')
168 settings.append('4:3')
169 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier4by3)
170 if endHeight % 2:
171 endHeight -= 1
172 if endHeight < TIVO_HEIGHT * 0.99:
173 settings.append('-s')
174 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
176 topPadding = ((TIVO_HEIGHT - endHeight)/2)
177 if topPadding % 2:
178 topPadding -= 1
180 settings.append('-padtop')
181 settings.append(str(topPadding))
182 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
183 settings.append('-padbottom')
184 settings.append(str(bottomPadding))
185 else: #if only very small amount of padding needed, then just stretch it
186 settings.append('-s')
187 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
188 debug_write(['select_aspect: File is wider than 4:3 padding top and bottom\n', ' '.join(settings), '\n'])
190 return settings
191 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
192 #an aspect ratio of 4:3 since they are so narrow.
193 else:
194 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
195 settings.append('-aspect')
196 settings.append('4:3')
197 if endWidth % 2:
198 endWidth -= 1
199 if endWidth < (TIVO_WIDTH * 0.99):
200 settings.append('-s')
201 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
203 leftPadding = ((TIVO_WIDTH - endWidth)/2)
204 if leftPadding % 2:
205 leftPadding -= 1
207 settings.append('-padleft')
208 settings.append(str(leftPadding))
209 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
210 settings.append('-padright')
211 settings.append(str(rightPadding))
212 else: #if only very small amount of padding needed, then just stretch it
213 settings.append('-s')
214 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
216 debug_write(['select_aspect: File is taller than 4:3 padding left and right\n', ' '.join(settings), '\n'])
218 return settings
220 def tivo_compatable(inFile, tsn = ''):
221 supportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
222 type, width, height, fps, millisecs, kbps, akbps = video_info(inFile)
223 #print type, width, height, fps, millisecs, kbps, akbps
225 if (inFile[-5:]).lower() == '.tivo':
226 debug_write(['tivo_compatible: ', inFile, ' ends with .tivo\n'])
227 return True
229 if not type == 'mpeg2video':
230 #print 'Not Tivo Codec'
231 debug_write(['tivo_compatible: ', inFile, ' is not mpeg2video it is ', type, '\n'])
232 return False
234 if (inFile[-3:]).lower() == '.ts':
235 debug_write(['tivo_compatible: ', inFile, ' transport stream not supported ', '\n'])
236 return False
238 if not akbps or int(akbps) > config.getMaxAudioBR(tsn):
239 debug_write(['tivo_compatible: ', inFile, ' max audio bitrate exceeded it is ', akbps, '\n'])
240 return False
242 if not kbps or int(kbps)-int(akbps) > config.strtod(config.getMaxVideoBR())/1000:
243 debug_write(['tivo_compatible: ', inFile, ' max video bitrate exceeded it is ', kbps, '\n'])
244 return False
246 if tsn[:3] in config.getHDtivos():
247 debug_write(['tivo_compatible: ', inFile, ' you have a S3 skiping the rest of the tests', '\n'])
248 return True
250 if not fps == '29.97':
251 #print 'Not Tivo fps'
252 debug_write(['tivo_compatible: ', inFile, ' is not correct fps it is ', fps, '\n'])
253 return False
255 for mode in supportedModes:
256 if (mode[0], mode[1]) == (width, height):
257 #print 'Is TiVo!'
258 debug_write(['tivo_compatible: ', inFile, ' has correct width of ', width, ' and height of ', height, '\n'])
259 return True
260 #print 'Not Tivo dimensions'
261 return False
263 def video_info(inFile):
264 mtime = os.stat(inFile).st_mtime
265 if inFile in info_cache and info_cache[inFile][0] == mtime:
266 debug_write(['video_info: ', inFile, ' cache hit!', '\n'])
267 return info_cache[inFile][1]
269 if (inFile[-5:]).lower() == '.tivo':
270 info_cache[inFile] = (mtime, (True, True, True, True, True, True, True))
271 debug_write(['video_info: ', inFile, ' ends in .tivo.\n'])
272 return True, True, True, True, True, True, True
274 cmd = [FFMPEG, '-i', inFile ]
275 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
277 # wait 4 sec if ffmpeg is not back give up
278 for i in range(80):
279 time.sleep(.05)
280 if not ffmpeg.poll() == None:
281 break
283 if ffmpeg.poll() == None:
284 kill(ffmpeg.pid)
285 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None))
286 return None, None, None, None, None, None, None
288 output = ffmpeg.stderr.read()
289 debug_write(['video_info: ffmpeg output=', output, '\n'])
291 rezre = re.compile(r'.*Video: ([^,]+),.*')
292 x = rezre.search(output)
293 if x:
294 codec = x.group(1)
295 else:
296 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None))
297 debug_write(['video_info: failed at codec\n'])
298 return None, None, None, None, None, None, None
300 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
301 x = rezre.search(output)
302 if x:
303 width = int(x.group(1))
304 height = int(x.group(2))
305 else:
306 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None))
307 debug_write(['video_info: failed at width/height\n'])
308 return None, None, None, None, None, None, None
310 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
311 x = rezre.search(output)
312 if x:
313 fps = x.group(1)
314 else:
315 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None))
316 debug_write(['video_info: failed at fps\n'])
317 return None, None, None, None, None, None, None
319 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
320 if (not fps == '29.97') and (codec == 'mpeg2video'):
321 # First look for the build 7215 version
322 rezre = re.compile(r'.*film source: 29.97.*')
323 x = rezre.search(output.lower() )
324 if x:
325 debug_write(['video_info: film source: 29.97 setting fps to 29.97\n'])
326 fps = '29.97'
327 else:
328 # for build 8047:
329 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
330 debug_write(['video_info: Bug in VideoReDo\n'])
331 x = rezre.search(output.lower() )
332 if x:
333 fps = '29.97'
335 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
336 d = durre.search(output)
337 if d:
338 millisecs = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
339 else:
340 millisecs = 0
342 #get bitrate of source for tivo compatibility test.
343 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
344 x = rezre.search(output)
345 if x:
346 kbps = x.group(1)
347 else:
348 kbps = None
349 debug_write(['video_info: failed at kbps\n'])
351 #get audio bitrate of source for tivo compatibility test.
352 rezre = re.compile(r'.*Audio: .+, (.+) (?:kb/s).*')
353 x = rezre.search(output)
354 if x:
355 akbps = x.group(1)
356 else:
357 akbps = None
358 debug_write(['video_info: failed at akbps\n'])
360 info_cache[inFile] = (mtime, (codec, width, height, fps, millisecs, kbps, akbps))
361 debug_write(['video_info: Codec=', codec, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, ' kbps=', kbps, ' akbps=', akbps, '\n'])
362 return codec, width, height, fps, millisecs, kbps, akbps
364 def supported_format(inFile):
365 if video_info(inFile)[0]:
366 return True
367 else:
368 debug_write(['supported_format: ', inFile, ' is not supported\n'])
369 return False
371 def kill(pid):
372 debug_write(['kill: killing pid=', str(pid), '\n'])
373 if mswindows:
374 win32kill(pid)
375 else:
376 import os, signal
377 os.kill(pid, signal.SIGTERM)
379 def win32kill(pid):
380 import ctypes
381 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
382 ctypes.windll.kernel32.TerminateProcess(handle, -1)
383 ctypes.windll.kernel32.CloseHandle(handle)
385 def gcd(a,b):
386 while b:
387 a, b = b, a % b
388 return a