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