.mpv not tivo compatible
[pyTivo/wmcbrine.git] / plugins / video / transcode.py
blobde4e6a86f6e816b9be7ca3567ba3f0392e49809d
1 import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache
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['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 = 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 = 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 = 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 = 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 height <= TIVO_HEIGHT and not optres:
166 return [] #pass all resolutions to S3/HD, except heights greater than conf height
167 # else, optres is enabled and resizes SD video to the "S2" standard on S3/HD.
168 elif (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
169 debug_write(__name__, fn_attr(), ['File is within 4:3 list.'])
170 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
171 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169:
172 debug_write(__name__, fn_attr(), ['File is within 16:9 list and 16:9 allowed.'])
173 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
174 else:
175 settings = []
176 #If video is wider than 4:3 add top and bottom padding
177 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
178 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
179 if (ratio > 177):#too short needs padding top and bottom
180 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier16by9)
181 settings.append('-aspect')
182 settings.append('16:9')
183 if endHeight % 2:
184 endHeight -= 1
185 if endHeight < TIVO_HEIGHT * 0.99:
186 settings.append('-s')
187 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
189 topPadding = ((TIVO_HEIGHT - endHeight)/2)
190 if topPadding % 2:
191 topPadding -= 1
193 settings.append('-padtop')
194 settings.append(str(topPadding))
195 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
196 settings.append('-padbottom')
197 settings.append(str(bottomPadding))
198 else: #if only very small amount of padding needed, then just stretch it
199 settings.append('-s')
200 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
201 debug_write(__name__, fn_attr(), ['16:9 aspect allowed, file is wider than 16:9 padding top and bottom', ' '.join(settings)])
202 else: #too skinny needs padding on left and right.
203 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier16by9))
204 settings.append('-aspect')
205 settings.append('16:9')
206 if endWidth % 2:
207 endWidth -= 1
208 if endWidth < (TIVO_WIDTH-10):
209 settings.append('-s')
210 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
212 leftPadding = ((TIVO_WIDTH - endWidth)/2)
213 if leftPadding % 2:
214 leftPadding -= 1
216 settings.append('-padleft')
217 settings.append(str(leftPadding))
218 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
219 settings.append('-padright')
220 settings.append(str(rightPadding))
221 else: #if only very small amount of padding needed, then just stretch it
222 settings.append('-s')
223 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
224 debug_write(__name__, fn_attr(), ['16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings)])
225 else: #this is a 4:3 file or 16:9 output not allowed
226 settings.append('-aspect')
227 settings.append('4:3')
228 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier4by3)
229 if endHeight % 2:
230 endHeight -= 1
231 if endHeight < TIVO_HEIGHT * 0.99:
232 settings.append('-s')
233 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
235 topPadding = ((TIVO_HEIGHT - endHeight)/2)
236 if topPadding % 2:
237 topPadding -= 1
239 settings.append('-padtop')
240 settings.append(str(topPadding))
241 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
242 settings.append('-padbottom')
243 settings.append(str(bottomPadding))
244 else: #if only very small amount of padding needed, then just stretch it
245 settings.append('-s')
246 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
247 debug_write(__name__, fn_attr(), ['File is wider than 4:3 padding top and bottom\n', ' '.join(settings)])
249 return settings
250 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
251 #an aspect ratio of 4:3 since they are so narrow.
252 else:
253 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
254 settings.append('-aspect')
255 settings.append('4:3')
256 if endWidth % 2:
257 endWidth -= 1
258 if endWidth < (TIVO_WIDTH * 0.99):
259 settings.append('-s')
260 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
262 leftPadding = ((TIVO_WIDTH - endWidth)/2)
263 if leftPadding % 2:
264 leftPadding -= 1
266 settings.append('-padleft')
267 settings.append(str(leftPadding))
268 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
269 settings.append('-padright')
270 settings.append(str(rightPadding))
271 else: #if only very small amount of padding needed, then just stretch it
272 settings.append('-s')
273 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
275 debug_write(__name__, fn_attr(), ['File is taller than 4:3 padding left and right\n', ' '.join(settings)])
277 return settings
279 def tivo_compatable(inFile, tsn = ''):
280 supportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
281 type, width, height, fps, millisecs, kbps, akbps, acodec, afreq = video_info(inFile)
282 #print type, width, height, fps, millisecs, kbps, akbps, acodec
284 if (inFile[-5:]).lower() == '.tivo':
285 debug_write(__name__, fn_attr(), ['TRUE, ends with .tivo.', inFile])
286 return True
288 if not type == 'mpeg2video':
289 #print 'Not Tivo Codec'
290 debug_write(__name__, fn_attr(), ['FALSE, type', type, 'not mpeg2video.', inFile])
291 return False
293 if os.path.splitext(inFile)[-1].lower() in ('.ts', '.mpv'):
294 debug_write(__name__, fn_attr(), ['FALSE, ext', os.path.splitext(inFile)[-1],\
295 'not tivo compatible.', inFile])
296 return False
298 if acodec == 'dca':
299 debug_write(__name__, fn_attr(), ['FALSE, acodec', acodec, ', not supported.', inFile])
300 return False
302 if acodec != None:
303 if not akbps or int(akbps) > config.getMaxAudioBR(tsn):
304 debug_write(__name__, fn_attr(), ['FALSE,', akbps, 'kbps exceeds max audio bitrate.', inFile])
305 return False
307 if kbps != None:
308 abit = max('0', akbps)
309 if int(kbps)-int(abit) > config.strtod(config.getMaxVideoBR())/1000:
310 debug_write(__name__, fn_attr(), ['FALSE,', kbps, 'kbps exceeds max video bitrate.', inFile])
311 return False
312 else:
313 debug_write(__name__, fn_attr(), ['FALSE,', kbps, 'kbps not supported.', inFile])
314 return False
316 if config.isHDtivo(tsn):
317 debug_write(__name__, fn_attr(), ['TRUE, HD Tivo detected, skipping remaining tests', inFile])
318 return True
320 if not fps == '29.97':
321 #print 'Not Tivo fps'
322 debug_write(__name__, fn_attr(), ['FALSE,', fps, 'fps, should be 29.97.', inFile])
323 return False
325 for mode in supportedModes:
326 if (mode[0], mode[1]) == (width, height):
327 #print 'Is TiVo!'
328 debug_write(__name__, fn_attr(), ['TRUE,', width, 'x', height, 'is valid.', inFile])
329 return True
330 #print 'Not Tivo dimensions'
331 debug_write(__name__, fn_attr(), ['FALSE,', width, 'x', height, 'not in supported modes.', inFile])
332 return False
334 def video_info(inFile):
335 mtime = os.stat(inFile).st_mtime
336 if inFile != videotest:
337 if inFile in info_cache and info_cache[inFile][0] == mtime:
338 debug_write(__name__, fn_attr(), ['CACHE HIT!', inFile])
339 return info_cache[inFile][1]
341 if (inFile[-5:]).lower() == '.tivo':
342 info_cache[inFile] = (mtime, (True, True, True, True, True, True, True, True, True))
343 debug_write(__name__, fn_attr(), ['VALID, ends in .tivo.', inFile])
344 return True, True, True, True, True, True, True, True, True
346 cmd = [ffmpeg_path(), '-i', inFile ]
347 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
349 # wait 10 sec if ffmpeg is not back give up
350 for i in xrange(200):
351 time.sleep(.05)
352 if not ffmpeg.poll() == None:
353 break
355 if ffmpeg.poll() == None:
356 kill(ffmpeg.pid)
357 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None))
358 return None, None, None, None, None, None, None, None, None
360 output = ffmpeg.stderr.read()
361 debug_write(__name__, fn_attr(), ['ffmpeg output=', output])
363 rezre = re.compile(r'.*Video: ([^,]+),.*')
364 x = rezre.search(output)
365 if x:
366 codec = x.group(1)
367 else:
368 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None))
369 debug_write(__name__, fn_attr(), ['failed at video codec'])
370 return None, None, None, None, None, None, None, None, None
372 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
373 x = rezre.search(output)
374 if x:
375 width = int(x.group(1))
376 height = int(x.group(2))
377 else:
378 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None))
379 debug_write(__name__, fn_attr(), ['failed at width/height'])
380 return None, None, None, None, None, None, None, None, None
382 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
383 x = rezre.search(output)
384 if x:
385 fps = x.group(1)
386 else:
387 info_cache[inFile] = (mtime, (None, None, None, None, None, None, None, None, None))
388 debug_write(__name__, fn_attr(), ['failed at fps'])
389 return None, None, None, None, None, None, None, None, None
391 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
392 if (not fps == '29.97') and (codec == 'mpeg2video'):
393 # First look for the build 7215 version
394 rezre = re.compile(r'.*film source: 29.97.*')
395 x = rezre.search(output.lower() )
396 if x:
397 debug_write(__name__, fn_attr(), ['film source: 29.97 setting fps to 29.97'])
398 fps = '29.97'
399 else:
400 # for build 8047:
401 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
402 debug_write(__name__, fn_attr(), ['Bug in VideoReDo'])
403 x = rezre.search(output.lower() )
404 if x:
405 fps = '29.97'
407 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
408 d = durre.search(output)
409 if d:
410 millisecs = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
411 else:
412 millisecs = 0
414 #get bitrate of source for tivo compatibility test.
415 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
416 x = rezre.search(output)
417 if x:
418 kbps = x.group(1)
419 else:
420 kbps = None
421 debug_write(__name__, fn_attr(), ['failed at kbps'])
423 #get audio bitrate of source for tivo compatibility test.
424 rezre = re.compile(r'.*Audio: .+, (.+) (?:kb/s).*')
425 x = rezre.search(output)
426 if x:
427 akbps = x.group(1)
428 else:
429 akbps = None
430 debug_write(__name__, fn_attr(), ['failed at akbps'])
432 #get audio codec of source for tivo compatibility test.
433 rezre = re.compile(r'.*Audio: ([^,]+),.*')
434 x = rezre.search(output)
435 if x:
436 acodec = x.group(1)
437 else:
438 acodec = None
439 debug_write(__name__, fn_attr(), ['failed at acodec'])
441 #get audio frequency of source for tivo compatibility test.
442 rezre = re.compile(r'.*Audio: .+, (.+) (?:Hz).*')
443 x = rezre.search(output)
444 if x:
445 afreq = x.group(1)
446 else:
447 afreq = None
448 debug_write(__name__, fn_attr(), ['failed at afreq'])
450 info_cache[inFile] = (mtime, (codec, width, height, fps, millisecs, kbps, akbps, acodec, afreq))
451 debug_write(__name__, fn_attr(), ['Codec=', codec, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, ' kbps=', kbps, ' akbps=', akbps, ' acodec=', acodec, ' afreq=', afreq])
452 return codec, width, height, fps, millisecs, kbps, akbps, acodec, afreq
454 def video_check(inFile, cmd_string):
455 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
456 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
457 try:
458 shutil.copyfileobj(ffmpeg.stdout, open(videotest, 'wb'))
459 return True
460 except:
461 kill(ffmpeg.pid)
462 return False
464 def supported_format(inFile):
465 if video_info(inFile)[0]:
466 return True
467 else:
468 debug_write(__name__, fn_attr(), ['FALSE, file not supported', inFile])
469 return False
471 def kill(pid):
472 debug_write(__name__, fn_attr(), ['killing pid=', str(pid)])
473 if mswindows:
474 win32kill(pid)
475 else:
476 import os, signal
477 os.kill(pid, signal.SIGTERM)
479 def win32kill(pid):
480 import ctypes
481 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
482 ctypes.windll.kernel32.TerminateProcess(handle, -1)
483 ctypes.windll.kernel32.CloseHandle(handle)
485 def gcd(a,b):
486 while b:
487 a, b = b, a % b
488 return a