Fixed imports in video
[pyTivo.git] / pyTivo / plugins / video / transcode.py
blob367d1487598f758773d568b4963a7b32ae1b6b2a
1 import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache, math
2 import pyTivo.config as config
3 from pyTivo.debug import debug_write, fn_attr
5 info_cache = lrucache.LRUCache(1000)
6 videotest = os.path.join(os.path.dirname(__file__), 'videotest.mpg')
8 def ffmpeg_path():
9 return config.get('Server', 'ffmpeg')
11 # XXX BIG HACK
12 # subprocess is broken for me on windows so super hack
13 def patchSubprocess():
14 o = subprocess.Popen._make_inheritable
16 def _make_inheritable(self, handle):
17 if not handle: return subprocess.GetCurrentProcess()
18 return o(self, handle)
20 subprocess.Popen._make_inheritable = _make_inheritable
21 mswindows = (sys.platform == "win32")
22 if mswindows:
23 patchSubprocess()
25 def output_video(inFile, outFile, tsn=''):
26 if tivo_compatable(inFile, tsn):
27 debug_write(__name__, fn_attr(), [inFile, ' is tivo compatible'])
28 f = file(inFile, 'rb')
29 shutil.copyfileobj(f, outFile)
30 f.close()
31 else:
32 debug_write(__name__, fn_attr(), [inFile, ' is not tivo compatible'])
33 transcode(inFile, outFile, tsn)
35 def transcode(inFile, outFile, tsn=''):
37 settings = {}
38 settings['video_codec'] = select_videocodec(tsn)
39 settings['video_br'] = select_videobr(tsn)
40 settings['video_fps'] = select_videofps(tsn)
41 settings['max_video_br'] = select_maxvideobr()
42 settings['buff_size'] = select_buffsize()
43 settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn))
44 settings['audio_br'] = select_audiobr(tsn)
45 settings['audio_fr'] = select_audiofr(inFile, tsn)
46 settings['audio_ch'] = select_audioch(tsn)
47 settings['audio_codec'] = select_audiocodec(inFile, tsn)
48 settings['ffmpeg_pram'] = select_ffmpegprams(tsn)
49 settings['format'] = select_format(tsn)
51 cmd_string = config.getFFmpegTemplate(tsn) % settings
53 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
54 print 'transcoding to tivo model '+tsn[:3]+' using ffmpeg command:'
55 print ' '.join(cmd)
56 debug_write(__name__, fn_attr(), ['ffmpeg command is ', ' '.join(cmd)])
57 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
58 try:
59 shutil.copyfileobj(ffmpeg.stdout, outFile)
60 except:
61 kill(ffmpeg.pid)
63 def select_audiocodec(inFile, tsn = ''):
64 # Default, compatible with all TiVo's
65 codec = 'ac3'
66 if config.getAudioCodec(tsn) == None:
67 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar = video_info(inFile)
68 if acodec in ('ac3', 'liba52', 'mp2'):
69 if akbps == None:
70 cmd_string = '-y -vcodec mpeg2video -r 29.97 -b 1000k -acodec copy -t 00:00:01 -f vob -'
71 if video_check(inFile, cmd_string):
72 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar = video_info(videotest)
73 if not akbps == None and int(akbps) <= config.getMaxAudioBR(tsn):
74 # compatible codec and bitrate, do not reencode audio
75 codec = 'copy'
76 else:
77 codec = config.getAudioCodec(tsn)
78 return '-acodec '+codec
80 def select_audiofr(inFile, tsn):
81 freq = '48000' #default
82 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar = video_info(inFile)
83 if not afreq == None and afreq in ('44100', '48000'):
84 # compatible frequency
85 freq = afreq
86 if config.getAudioFR(tsn) != None:
87 freq = config.getAudioFR(tsn)
88 return '-ar '+freq
90 def select_audioch(tsn):
91 if config.getAudioCH(tsn) != None:
92 return '-ac '+config.getAudioCH(tsn)
93 return ''
95 def select_videofps(tsn):
96 vfps = '-r 29.97' #default
97 if config.isHDtivo(tsn):
98 vfps = ' '
99 if config.getVideoFPS(tsn) != None:
100 vfps = '-r '+config.getVideoFPS(tsn)
101 return vfps
103 def select_videocodec(tsn):
104 vcodec = 'mpeg2video' #default
105 if config.getVideoCodec(tsn) != None:
106 vcodec = config.getVideoCodec(tsn)
107 return '-vcodec '+vcodec
109 def select_videobr(tsn):
110 return '-b '+config.getVideoBR(tsn)
112 def select_audiobr(tsn):
113 return '-ab '+config.getAudioBR(tsn)
115 def select_maxvideobr():
116 return '-maxrate '+config.getMaxVideoBR()
118 def select_buffsize():
119 return '-bufsize '+config.getBuffSize()
121 def select_ffmpegprams(tsn):
122 if config.getFFmpegPrams(tsn) != None:
123 return config.getFFmpegPrams(tsn)
124 return ''
126 def select_format(tsn):
127 fmt = 'vob'
128 if config.getFormat(tsn) != None:
129 fmt = config.getFormat(tsn)
130 return '-f '+fmt+' -'
132 def select_aspect(inFile, tsn = ''):
133 TIVO_WIDTH = config.getTivoWidth(tsn)
134 TIVO_HEIGHT = config.getTivoHeight(tsn)
136 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar = video_info(inFile)
138 debug_write(__name__, fn_attr(), ['tsn:', tsn])
140 aspect169 = config.get169Setting(tsn)
142 debug_write(__name__, fn_attr(), ['aspect169:', aspect169])
144 optres = config.getOptres(tsn)
146 debug_write(__name__, fn_attr(), ['optres:', optres])
148 if optres:
149 optHeight = config.nearestTivoHeight(height)
150 optWidth = config.nearestTivoWidth(width)
151 if optHeight < TIVO_HEIGHT:
152 TIVO_HEIGHT = optHeight
153 if optWidth < TIVO_WIDTH:
154 TIVO_WIDTH = optWidth
156 d = gcd(height,width)
157 ratio = (width*100)/height
158 rheight, rwidth = height/d, width/d
160 debug_write(__name__, fn_attr(), ['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])
162 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH)
163 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH)
165 if config.isHDtivo(tsn) and not optres:
166 if config.getPixelAR(0):
167 if vpar == None:
168 npar = config.getPixelAR(1)
169 else:
170 npar = vpar
171 # adjust for pixel aspect ratio, if set, because TiVo expects square pixels
172 if npar<1.0:
173 return ['-s', str(width) + 'x' + str(int(math.ceil(height/npar)))]
174 elif npar>1.0:
175 # FFMPEG expects width to be a multiple of two
176 return ['-s', str(int(math.ceil(width*npar/2.0)*2)) + 'x' + str(height)]
177 if height <= TIVO_HEIGHT:
178 # pass all resolutions to S3, except heights greater than conf height
179 return []
180 # else, resize video.
181 if (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
182 debug_write(__name__, fn_attr(), ['File is within 4:3 list.'])
183 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
184 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169:
185 debug_write(__name__, fn_attr(), ['File is within 16:9 list and 16:9 allowed.'])
186 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
187 else:
188 settings = []
189 #If video is wider than 4:3 add top and bottom padding
190 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
191 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
192 if (ratio > 177):#too short needs padding top and bottom
193 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier16by9)
194 settings.append('-aspect')
195 settings.append('16:9')
196 if endHeight % 2:
197 endHeight -= 1
198 if endHeight < TIVO_HEIGHT * 0.99:
199 settings.append('-s')
200 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
202 topPadding = ((TIVO_HEIGHT - endHeight)/2)
203 if topPadding % 2:
204 topPadding -= 1
206 settings.append('-padtop')
207 settings.append(str(topPadding))
208 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
209 settings.append('-padbottom')
210 settings.append(str(bottomPadding))
211 else: #if only very small amount of padding needed, then just stretch it
212 settings.append('-s')
213 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
214 debug_write(__name__, fn_attr(), ['16:9 aspect allowed, file is wider than 16:9 padding top and bottom', ' '.join(settings)])
215 else: #too skinny needs padding on left and right.
216 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier16by9))
217 settings.append('-aspect')
218 settings.append('16:9')
219 if endWidth % 2:
220 endWidth -= 1
221 if endWidth < (TIVO_WIDTH-10):
222 settings.append('-s')
223 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
225 leftPadding = ((TIVO_WIDTH - endWidth)/2)
226 if leftPadding % 2:
227 leftPadding -= 1
229 settings.append('-padleft')
230 settings.append(str(leftPadding))
231 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
232 settings.append('-padright')
233 settings.append(str(rightPadding))
234 else: #if only very small amount of padding needed, then just stretch it
235 settings.append('-s')
236 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
237 debug_write(__name__, fn_attr(), ['16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings)])
238 else: #this is a 4:3 file or 16:9 output not allowed
239 settings.append('-aspect')
240 settings.append('4:3')
241 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier4by3)
242 if endHeight % 2:
243 endHeight -= 1
244 if endHeight < TIVO_HEIGHT * 0.99:
245 settings.append('-s')
246 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
248 topPadding = ((TIVO_HEIGHT - endHeight)/2)
249 if topPadding % 2:
250 topPadding -= 1
252 settings.append('-padtop')
253 settings.append(str(topPadding))
254 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
255 settings.append('-padbottom')
256 settings.append(str(bottomPadding))
257 else: #if only very small amount of padding needed, then just stretch it
258 settings.append('-s')
259 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
260 debug_write(__name__, fn_attr(), ['File is wider than 4:3 padding top and bottom\n', ' '.join(settings)])
262 return settings
263 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
264 #an aspect ratio of 4:3 since they are so narrow.
265 else:
266 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
267 settings.append('-aspect')
268 settings.append('4:3')
269 if endWidth % 2:
270 endWidth -= 1
271 if endWidth < (TIVO_WIDTH * 0.99):
272 settings.append('-s')
273 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
275 leftPadding = ((TIVO_WIDTH - endWidth)/2)
276 if leftPadding % 2:
277 leftPadding -= 1
279 settings.append('-padleft')
280 settings.append(str(leftPadding))
281 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
282 settings.append('-padright')
283 settings.append(str(rightPadding))
284 else: #if only very small amount of padding needed, then just stretch it
285 settings.append('-s')
286 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
288 debug_write(__name__, fn_attr(), ['File is taller than 4:3 padding left and right\n', ' '.join(settings)])
290 return settings
292 def tivo_compatable(inFile, tsn = ''):
293 supportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
294 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar = video_info(inFile)
295 #print type, width, height, fps, millisecs, kbps, akbps, acodec
297 if (inFile[-5:]).lower() == '.tivo':
298 debug_write(__name__, fn_attr(), ['TRUE, ends with .tivo.', inFile])
299 return True
301 if not type == 'mpeg2video':
302 #print 'Not Tivo Codec'
303 debug_write(__name__, fn_attr(), ['FALSE, type', type, 'not mpeg2video.', inFile])
304 return False
306 if os.path.splitext(inFile)[-1].lower() in ('.ts', '.mpv'):
307 debug_write(__name__, fn_attr(), ['FALSE, ext', os.path.splitext(inFile)[-1],\
308 'not tivo compatible.', inFile])
309 return False
311 if acodec == 'dca':
312 debug_write(__name__, fn_attr(), ['FALSE, acodec', acodec, ', not supported.', inFile])
313 return False
315 if acodec != None:
316 if not akbps or int(akbps) > config.getMaxAudioBR(tsn):
317 debug_write(__name__, fn_attr(), ['FALSE,', akbps, 'kbps exceeds max audio bitrate.', inFile])
318 return False
320 if kbps != None:
321 abit = max('0', akbps)
322 if int(kbps)-int(abit) > config.strtod(config.getMaxVideoBR())/1000:
323 debug_write(__name__, fn_attr(), ['FALSE,', kbps, 'kbps exceeds max video bitrate.', inFile])
324 return False
325 else:
326 debug_write(__name__, fn_attr(), ['FALSE,', kbps, 'kbps not supported.', inFile])
327 return False
329 if config.isHDtivo(tsn):
330 if vpar != 1.0:
331 if config.getPixelAR(0):
332 if vpar != None or config.getPixelAR(1) != 1.0:
333 debug_write(__name__, fn_attr(), ['FALSE,', vpar, 'not correct PAR,', inFile])
334 return False
335 debug_write(__name__, fn_attr(), ['TRUE, HD Tivo detected, skipping remaining tests', inFile])
336 return True
338 if not fps == '29.97':
339 #print 'Not Tivo fps'
340 debug_write(__name__, fn_attr(), ['FALSE,', fps, 'fps, should be 29.97.', inFile])
341 return False
343 for mode in supportedModes:
344 if (mode[0], mode[1]) == (width, height):
345 #print 'Is TiVo!'
346 debug_write(__name__, fn_attr(), ['TRUE,', width, 'x', height, 'is valid.', inFile])
347 return True
348 #print 'Not Tivo dimensions'
349 debug_write(__name__, fn_attr(), ['FALSE,', width, 'x', height, 'not in supported modes.', inFile])
350 return False
352 def video_info(inFile):
353 mtime = os.stat(inFile).st_mtime
354 if inFile != videotest:
355 if inFile in info_cache and info_cache[inFile][0] == mtime:
356 debug_write(__name__, fn_attr(), ['CACHE HIT!', inFile])
357 return info_cache[inFile][1]
359 if (inFile[-5:]).lower() == '.tivo':
360 info_cache[inFile] = (mtime, (True, True, True, True, True, True, True, True, True, True))
361 debug_write(__name__, fn_attr(), ['VALID, ends in .tivo.', inFile])
362 return True, True, True, True, True, True, True, True, True, True
364 cmd = [ffmpeg_path(), '-i', inFile ]
365 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
367 # wait 10 sec if ffmpeg is not back give up
368 for i in xrange(200):
369 time.sleep(.05)
370 if not ffmpeg.poll() == None:
371 break
373 if ffmpeg.poll() == None:
374 kill(ffmpeg.pid)
375 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None, None))
376 return None, None, None, None, None, None, None, None, None, None
378 output = ffmpeg.stderr.read()
379 debug_write(__name__, fn_attr(), ['ffmpeg output=', output])
381 rezre = re.compile(r'.*Video: ([^,]+),.*')
382 x = rezre.search(output)
383 if x:
384 codec = x.group(1)
385 else:
386 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None, None))
387 debug_write(__name__, fn_attr(), ['failed at video codec'])
388 return None, None, None, None, None, None, None, None, None, None
390 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
391 x = rezre.search(output)
392 if x:
393 width = int(x.group(1))
394 height = int(x.group(2))
395 else:
396 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None, None))
397 debug_write(__name__, fn_attr(), ['failed at width/height'])
398 return None, None, None, None, None, None, None, None, None, None
400 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
401 x = rezre.search(output)
402 if x:
403 fps = x.group(1)
404 else:
405 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None, None))
406 debug_write(__name__, fn_attr(), ['failed at fps'])
407 return None, None, None, None, None, None, None, None, None, None
409 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
410 if (not fps == '29.97') and (codec == 'mpeg2video'):
411 # First look for the build 7215 version
412 rezre = re.compile(r'.*film source: 29.97.*')
413 x = rezre.search(output.lower() )
414 if x:
415 debug_write(__name__, fn_attr(), ['film source: 29.97 setting fps to 29.97'])
416 fps = '29.97'
417 else:
418 # for build 8047:
419 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
420 debug_write(__name__, fn_attr(), ['Bug in VideoReDo'])
421 x = rezre.search(output.lower() )
422 if x:
423 fps = '29.97'
425 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
426 d = durre.search(output)
427 if d:
428 millisecs = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
429 else:
430 millisecs = 0
432 #get bitrate of source for tivo compatibility test.
433 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
434 x = rezre.search(output)
435 if x:
436 kbps = x.group(1)
437 else:
438 kbps = None
439 debug_write(__name__, fn_attr(), ['failed at kbps'])
441 #get audio bitrate of source for tivo compatibility test.
442 rezre = re.compile(r'.*Audio: .+, (.+) (?:kb/s).*')
443 x = rezre.search(output)
444 if x:
445 akbps = x.group(1)
446 else:
447 akbps = None
448 debug_write(__name__, fn_attr(), ['failed at akbps'])
450 #get audio codec of source for tivo compatibility test.
451 rezre = re.compile(r'.*Audio: ([^,]+),.*')
452 x = rezre.search(output)
453 if x:
454 acodec = x.group(1)
455 else:
456 acodec = None
457 debug_write(__name__, fn_attr(), ['failed at acodec'])
459 #get audio frequency of source for tivo compatibility test.
460 rezre = re.compile(r'.*Audio: .+, (.+) (?:Hz).*')
461 x = rezre.search(output)
462 if x:
463 afreq = x.group(1)
464 else:
465 afreq = None
466 debug_write(__name__, fn_attr(), ['failed at afreq'])
468 #get par.
469 rezre = re.compile(r'.*Video: .+PAR ([0-9]+):([0-9]+) DAR [0-9:]+.*')
470 x = rezre.search(output)
471 if x and x.group(1)!="0" and x.group(2)!="0":
472 vpar = float(x.group(1))/float(x.group(2))
473 else:
474 vpar = None
476 info_cache[inFile] = (mtime, (codec, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar))
477 debug_write(__name__, fn_attr(), ['Codec=', codec, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, ' kbps=', kbps, ' akbps=', akbps, ' acodec=', acodec, ' afreq=', afreq, ' par=', vpar])
478 return codec, width, height, fps, millisecs, kbps, akbps, acodec, afreq, vpar
480 def video_check(inFile, cmd_string):
481 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
482 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
483 try:
484 shutil.copyfileobj(ffmpeg.stdout, open(videotest, 'wb'))
485 return True
486 except:
487 kill(ffmpeg.pid)
488 return False
490 def supported_format(inFile):
491 if video_info(inFile)[0]:
492 return True
493 else:
494 debug_write(__name__, fn_attr(), ['FALSE, file not supported', inFile])
495 return False
497 def kill(pid):
498 debug_write(__name__, fn_attr(), ['killing pid=', str(pid)])
499 if mswindows:
500 win32kill(pid)
501 else:
502 import os, signal
503 os.kill(pid, signal.SIGTERM)
505 def win32kill(pid):
506 import ctypes
507 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
508 ctypes.windll.kernel32.TerminateProcess(handle, -1)
509 ctypes.windll.kernel32.CloseHandle(handle)
511 def gcd(a,b):
512 while b:
513 a, b = b, a % b
514 return a