allow 2 channel AAC audio to transfer (without) reencoding while
[pyTivo/wmcbrine/lucasnz.git] / plugins / video / transcode.py
blobbbe3ce163975c0dd0f6b09673c4fb409971050f5
1 import logging
2 import math
3 import os
4 import re
5 import shutil
6 import subprocess
7 import sys
8 import tempfile
9 import threading
10 import time
12 import lrucache
14 import config
15 import metadata
17 logger = logging.getLogger('pyTivo.video.transcode')
19 info_cache = lrucache.LRUCache(1000)
20 ffmpeg_procs = {}
21 reapers = {}
23 GOOD_MPEG_FPS = ['23.98', '24.00', '25.00', '29.97',
24 '30.00', '50.00', '59.94', '60.00']
26 BLOCKSIZE = 512 * 1024
27 MAXBLOCKS = 2
28 TIMEOUT = 600
30 UNSET = 0
31 OLD_PAD = 1
32 NEW_PAD = 2
34 pad_style = UNSET
36 # XXX BIG HACK
37 # subprocess is broken for me on windows so super hack
38 def patchSubprocess():
39 o = subprocess.Popen._make_inheritable
41 def _make_inheritable(self, handle):
42 if not handle: return subprocess.GetCurrentProcess()
43 return o(self, handle)
45 subprocess.Popen._make_inheritable = _make_inheritable
46 mswindows = (sys.platform == "win32")
47 if mswindows:
48 patchSubprocess()
50 def debug(msg):
51 if type(msg) == str:
52 try:
53 msg = msg.decode('utf8')
54 except:
55 if sys.platform == 'darwin':
56 msg = msg.decode('macroman')
57 else:
58 msg = msg.decode('iso8859-1')
59 logger.debug(msg)
61 def transcode(isQuery, inFile, outFile, tsn=''):
62 settings = {'video_codec': select_videocodec(inFile, tsn),
63 'video_br': select_videobr(inFile, tsn),
64 'video_fps': select_videofps(inFile, tsn),
65 'max_video_br': select_maxvideobr(tsn),
66 'buff_size': select_buffsize(tsn),
67 'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
68 'audio_br': select_audiobr(tsn),
69 'audio_fr': select_audiofr(inFile, tsn),
70 'audio_ch': select_audioch(tsn),
71 'audio_codec': select_audiocodec(isQuery, inFile, tsn),
72 'audio_lang': select_audiolang(inFile, tsn),
73 'ffmpeg_pram': select_ffmpegprams(tsn),
74 'format': select_format(tsn)}
76 if isQuery:
77 return settings
79 ffmpeg_path = config.get_bin('ffmpeg')
80 cmd_string = config.getFFmpegTemplate(tsn) % settings
81 fname = unicode(inFile, 'utf-8')
82 if mswindows:
83 fname = fname.encode('iso8859-1')
85 if inFile[-5:].lower() == '.tivo':
86 tivodecode_path = config.get_bin('tivodecode')
87 tivo_mak = config.get_server('tivo_mak')
88 tcmd = [tivodecode_path, '-m', tivo_mak, fname]
89 tivodecode = subprocess.Popen(tcmd, stdout=subprocess.PIPE,
90 bufsize=(512 * 1024))
91 if tivo_compatible(inFile, tsn)[0]:
92 cmd = ''
93 ffmpeg = tivodecode
94 else:
95 cmd = [ffmpeg_path, '-i', '-'] + cmd_string.split()
96 ffmpeg = subprocess.Popen(cmd, stdin=tivodecode.stdout,
97 stdout=subprocess.PIPE,
98 bufsize=(512 * 1024))
99 else:
100 cmd = [ffmpeg_path, '-i', fname] + cmd_string.split()
101 ffmpeg = subprocess.Popen(cmd, bufsize=(512 * 1024),
102 stdout=subprocess.PIPE)
104 if cmd:
105 debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
106 debug(' '.join(cmd))
108 ffmpeg_procs[inFile] = {'process': ffmpeg, 'start': 0, 'end': 0,
109 'last_read': time.time(), 'blocks': []}
110 reap_process(inFile)
111 return transfer_blocks(inFile, outFile)
113 def is_resumable(inFile, offset):
114 if inFile in ffmpeg_procs:
115 proc = ffmpeg_procs[inFile]
116 if proc['start'] <= offset < proc['end']:
117 return True
118 else:
119 cleanup(inFile)
120 kill(proc['process'])
121 return False
123 def resume_transfer(inFile, outFile, offset):
124 proc = ffmpeg_procs[inFile]
125 offset -= proc['start']
126 count = 0
128 try:
129 for block in proc['blocks']:
130 length = len(block)
131 if offset < length:
132 if offset > 0:
133 block = block[offset:]
134 outFile.write('%x\r\n' % len(block))
135 outFile.write(block)
136 outFile.write('\r\n')
137 count += len(block)
138 offset -= length
139 outFile.flush()
140 except Exception, msg:
141 logger.info(msg)
142 return count
144 proc['start'] = proc['end']
145 proc['blocks'] = []
147 return count + transfer_blocks(inFile, outFile)
149 def transfer_blocks(inFile, outFile):
150 proc = ffmpeg_procs[inFile]
151 blocks = proc['blocks']
152 count = 0
154 while True:
155 try:
156 block = proc['process'].stdout.read(BLOCKSIZE)
157 proc['last_read'] = time.time()
158 except Exception, msg:
159 logger.info(msg)
160 cleanup(inFile)
161 kill(proc['process'])
162 break
164 if not block:
165 try:
166 outFile.flush()
167 except Exception, msg:
168 logger.info(msg)
169 else:
170 cleanup(inFile)
171 break
173 blocks.append(block)
174 proc['end'] += len(block)
175 if len(blocks) > MAXBLOCKS:
176 proc['start'] += len(blocks[0])
177 blocks.pop(0)
179 try:
180 outFile.write('%x\r\n' % len(block))
181 outFile.write(block)
182 outFile.write('\r\n')
183 count += len(block)
184 except Exception, msg:
185 logger.info(msg)
186 break
188 return count
190 def reap_process(inFile):
191 if ffmpeg_procs and inFile in ffmpeg_procs:
192 proc = ffmpeg_procs[inFile]
193 if proc['last_read'] + TIMEOUT < time.time():
194 del ffmpeg_procs[inFile]
195 del reapers[inFile]
196 kill(proc['process'])
197 else:
198 reaper = threading.Timer(TIMEOUT, reap_process, (inFile,))
199 reapers[inFile] = reaper
200 reaper.start()
202 def cleanup(inFile):
203 del ffmpeg_procs[inFile]
204 reapers[inFile].cancel()
205 del reapers[inFile]
207 def select_audiocodec(isQuery, inFile, tsn=''):
208 if inFile[-5:].lower() == '.tivo':
209 return '-acodec copy'
210 vInfo = video_info(inFile)
211 codectype = vInfo['vCodec']
212 codec = config.get_tsn('audio_codec', tsn)
213 if not codec:
214 # Default, compatible with all TiVo's
215 codec = 'ac3'
216 if vInfo['aCodec'] in ('libfaad', 'ac3', 'liba52', 'mp2'):
217 aKbps = vInfo['aKbps']
218 aCh = vInfo['aCh']
219 if aKbps == None:
220 if not isQuery:
221 vInfoQuery = audio_check(inFile, tsn)
222 if vInfoQuery == None:
223 aKbps = None
224 aCh = None
225 else:
226 aKbps = vInfoQuery['aKbps']
227 aCh = vInfoQuery['aCh']
228 else:
229 codec = 'TBD'
230 if aKbps != None and int(aKbps) <= config.getMaxAudioBR(tsn):
231 # compatible codec and bitrate, do not reencode audio
232 codec = 'copy'
233 if vInfo['aCodec'] == 'libfaad' and aCh != 2:
234 codec = 'ac3'
235 copy_flag = config.get_tsn('copy_ts', tsn)
236 copyts = ' -copyts'
237 if ((codec == 'copy' and codectype == 'mpeg2video' and not copy_flag) or
238 (copy_flag and copy_flag.lower() == 'false')):
239 copyts = ''
240 return '-acodec ' + codec + copyts
242 def select_audiofr(inFile, tsn):
243 freq = '48000' #default
244 vInfo = video_info(inFile)
245 if not vInfo['aFreq'] == None and vInfo['aFreq'] in ('44100', '48000'):
246 # compatible frequency
247 freq = vInfo['aFreq']
248 audio_fr = config.get_tsn('audio_fr', tsn)
249 if audio_fr != None:
250 freq = audio_fr
251 return '-ar ' + freq
253 def select_audioch(tsn):
254 ch = config.get_tsn('audio_ch', tsn)
255 if ch:
256 return '-ac ' + ch
257 return ''
259 def select_audiolang(inFile, tsn):
260 vInfo = video_info(inFile)
261 audio_lang = config.get_tsn('audio_lang', tsn)
262 debug('audio_lang: %s' % audio_lang)
263 if audio_lang != None and vInfo['mapVideo'] != None:
264 stream = vInfo['mapAudio'][0][0]
265 langmatch_curr = []
266 langmatch_prev = vInfo['mapAudio'][:]
267 for lang in audio_lang.replace(' ','').lower().split(','):
268 for s, l in langmatch_prev:
269 if lang in s + (l).replace(' ','').lower():
270 langmatch_curr.append((s, l))
271 stream = s
272 #if only 1 item matched we're done
273 if len(langmatch_curr) == 1:
274 del langmatch_prev[:]
275 break
276 #if more than 1 item matched copy the curr area to the prev array
277 #we only need to look at the new shorter list from now on
278 elif len(langmatch_curr) > 1:
279 del langmatch_prev[:]
280 langmatch_prev = langmatch_curr[:]
281 del langmatch_curr[:]
282 #if nothing matched we'll keep the prev array and clear the curr array
283 else:
284 del langmatch_curr[:]
285 #if we drop out of the loop with more than 1 item default to the first item
286 if len(langmatch_prev) > 1:
287 stream = langmatch_prev[0][0]
288 if stream is not '':
289 debug('selected audio stream: %s' % stream)
290 return '-map ' + vInfo['mapVideo'] + ' -map ' + stream
291 debug('selected audio stream: %s' % '')
292 return ''
294 def select_videofps(inFile, tsn):
295 vInfo = video_info(inFile)
296 fps = '-r 29.97' # default
297 if config.isHDtivo(tsn) and vInfo['vFps'] in GOOD_MPEG_FPS:
298 fps = ' '
299 video_fps = config.get_tsn('video_fps', tsn)
300 if video_fps != None:
301 fps = '-r ' + video_fps
302 return fps
304 def select_videocodec(inFile, tsn):
305 vInfo = video_info(inFile)
306 if tivo_compatible_video(vInfo, tsn)[0]:
307 codec = 'copy'
308 else:
309 codec = 'mpeg2video' # default
310 return '-vcodec ' + codec
312 def select_videobr(inFile, tsn):
313 return '-b ' + str(select_videostr(inFile, tsn) / 1000) + 'k'
315 def select_videostr(inFile, tsn):
316 vInfo = video_info(inFile)
317 if tivo_compatible_video(vInfo, tsn)[0]:
318 video_str = int(vInfo['kbps'])
319 if vInfo['aKbps']:
320 video_str -= int(vInfo['aKbps'])
321 video_str *= 1000
322 else:
323 video_str = config.strtod(config.getVideoBR(tsn))
324 if config.isHDtivo(tsn):
325 if vInfo['kbps'] != None and config.getVideoPCT(tsn) > 0:
326 video_percent = (int(vInfo['kbps']) * 10 *
327 config.getVideoPCT(tsn))
328 video_str = max(video_str, video_percent)
329 video_str = int(min(config.strtod(config.getMaxVideoBR(tsn)) * 0.95,
330 video_str))
331 return video_str
333 def select_audiobr(tsn):
334 return '-ab ' + config.getAudioBR(tsn)
336 def select_maxvideobr(tsn):
337 return '-maxrate ' + config.getMaxVideoBR(tsn)
339 def select_buffsize(tsn):
340 return '-bufsize ' + config.getBuffSize(tsn)
342 def select_ffmpegprams(tsn):
343 params = config.getFFmpegPrams(tsn)
344 if not params:
345 params = ''
346 return params
348 def select_format(tsn):
349 fmt = 'vob'
350 return '-f %s -' % fmt
352 def pad_check():
353 global pad_style
354 if pad_style == UNSET:
355 pad_style = OLD_PAD
356 filters = tempfile.TemporaryFile()
357 cmd = [config.get_bin('ffmpeg'), '-filters']
358 ffmpeg = subprocess.Popen(cmd, stdout=filters, stderr=subprocess.PIPE)
359 ffmpeg.wait()
360 filters.seek(0)
361 for line in filters:
362 if line.startswith('pad'):
363 pad_style = NEW_PAD
364 break
365 filters.close()
366 return pad_style == NEW_PAD
368 def pad_TB(TIVO_WIDTH, TIVO_HEIGHT, multiplier, vInfo):
369 endHeight = int(((TIVO_WIDTH * vInfo['vHeight']) /
370 vInfo['vWidth']) * multiplier)
371 if endHeight % 2:
372 endHeight -= 1
373 if endHeight < TIVO_HEIGHT * 0.99:
374 topPadding = (TIVO_HEIGHT - endHeight) / 2
375 if topPadding % 2:
376 topPadding -= 1
377 newpad = pad_check()
378 if newpad:
379 return ['-s', '%sx%s' % (TIVO_WIDTH, endHeight), '-vf',
380 'pad=%d:%d:0:%d' % (TIVO_WIDTH, TIVO_HEIGHT, topPadding)]
381 else:
382 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
383 return ['-s', '%sx%s' % (TIVO_WIDTH, endHeight),
384 '-padtop', str(topPadding),
385 '-padbottom', str(bottomPadding)]
386 else: # if only very small amount of padding needed, then
387 # just stretch it
388 return ['-s', '%sx%s' % (TIVO_WIDTH, TIVO_HEIGHT)]
390 def pad_LR(TIVO_WIDTH, TIVO_HEIGHT, multiplier, vInfo):
391 endWidth = int((TIVO_HEIGHT * vInfo['vWidth']) /
392 (vInfo['vHeight'] * multiplier))
393 if endWidth % 2:
394 endWidth -= 1
395 if endWidth < TIVO_WIDTH * 0.99:
396 leftPadding = (TIVO_WIDTH - endWidth) / 2
397 if leftPadding % 2:
398 leftPadding -= 1
399 newpad = pad_check()
400 if newpad:
401 return ['-s', '%sx%s' % (endWidth, TIVO_HEIGHT), '-vf',
402 'pad=%d:%d:%d:0' % (TIVO_WIDTH, TIVO_HEIGHT, leftPadding)]
403 else:
404 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
405 return ['-s', '%sx%s' % (endWidth, TIVO_HEIGHT),
406 '-padleft', str(leftPadding),
407 '-padright', str(rightPadding)]
408 else: # if only very small amount of padding needed, then
409 # just stretch it
410 return ['-s', '%sx%s' % (TIVO_WIDTH, TIVO_HEIGHT)]
412 def select_aspect(inFile, tsn = ''):
413 TIVO_WIDTH = config.getTivoWidth(tsn)
414 TIVO_HEIGHT = config.getTivoHeight(tsn)
416 vInfo = video_info(inFile)
418 debug('tsn: %s' % tsn)
420 aspect169 = config.get169Setting(tsn)
422 debug('aspect169: %s' % aspect169)
424 optres = config.getOptres(tsn)
426 debug('optres: %s' % optres)
428 if optres:
429 optHeight = config.nearestTivoHeight(vInfo['vHeight'])
430 optWidth = config.nearestTivoWidth(vInfo['vWidth'])
431 if optHeight < TIVO_HEIGHT:
432 TIVO_HEIGHT = optHeight
433 if optWidth < TIVO_WIDTH:
434 TIVO_WIDTH = optWidth
436 if vInfo.get('par2'):
437 par2 = vInfo['par2']
438 elif vInfo.get('par'):
439 par2 = float(vInfo['par'])
440 else:
441 # Assume PAR = 1.0
442 par2 = 1.0
444 debug(('File=%s vCodec=%s vWidth=%s vHeight=%s vFps=%s millisecs=%s ' +
445 'TIVO_HEIGHT=%s TIVO_WIDTH=%s') % (inFile, vInfo['vCodec'],
446 vInfo['vWidth'], vInfo['vHeight'], vInfo['vFps'],
447 vInfo['millisecs'], TIVO_HEIGHT, TIVO_WIDTH))
449 if config.isHDtivo(tsn) and not optres:
450 if config.getPixelAR(0) or vInfo['par']:
451 if vInfo['par2'] == None:
452 if vInfo['par']:
453 npar = par2
454 else:
455 npar = config.getPixelAR(1)
456 else:
457 npar = par2
459 # adjust for pixel aspect ratio, if set
461 if npar < 1.0:
462 return ['-s', '%dx%d' % (vInfo['vWidth'],
463 math.ceil(vInfo['vHeight'] / npar))]
464 elif npar > 1.0:
465 # FFMPEG expects width to be a multiple of two
466 return ['-s', '%dx%d' % (math.ceil(vInfo['vWidth']*npar/2.0)*2,
467 vInfo['vHeight'])]
469 if vInfo['vHeight'] <= TIVO_HEIGHT:
470 # pass all resolutions to S3, except heights greater than
471 # conf height
472 return []
473 # else, resize video.
475 d = gcd(vInfo['vHeight'], vInfo['vWidth'])
476 rheight, rwidth = vInfo['vHeight'] / d, vInfo['vWidth'] / d
477 debug('rheight=%s rwidth=%s' % (rheight, rwidth))
479 if (rwidth, rheight) in [(1, 1)] and vInfo['par1'] == '8:9':
480 debug('File + PAR is within 4:3.')
481 return ['-aspect', '4:3', '-s', '%sx%s' % (TIVO_WIDTH, TIVO_HEIGHT)]
483 elif ((rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54),
484 (59, 72), (59, 36), (59, 54)] or
485 vInfo['dar1'] == '4:3'):
486 debug('File is within 4:3 list.')
487 return ['-aspect', '4:3', '-s', '%sx%s' % (TIVO_WIDTH, TIVO_HEIGHT)]
489 elif (((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81),
490 (59, 27)] or vInfo['dar1'] == '16:9')
491 and (aspect169 or config.get169Letterbox(tsn))):
492 debug('File is within 16:9 list and 16:9 allowed.')
494 if config.get169Blacklist(tsn) or (aspect169 and
495 config.get169Letterbox(tsn)):
496 aspect = '4:3'
497 else:
498 aspect = '16:9'
499 return ['-aspect', aspect, '-s', '%sx%s' % (TIVO_WIDTH, TIVO_HEIGHT)]
501 else:
502 settings = ['-aspect']
504 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH) / par2
505 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH) / par2
506 ratio = vInfo['vWidth'] * 100 * par2 / vInfo['vHeight']
507 debug('par2=%.3f ratio=%.3f mult4by3=%.3f' % (par2, ratio,
508 multiplier4by3))
510 # If video is wider than 4:3 add top and bottom padding
512 if ratio > 133: # Might be 16:9 file, or just need padding on
513 # top and bottom
515 if aspect169 and ratio > 135: # If file would fall in 4:3
516 # assume it is supposed to be 4:3
518 if (config.get169Blacklist(tsn) or
519 config.get169Letterbox(tsn)):
520 settings.append('4:3')
521 else:
522 settings.append('16:9')
524 if ratio > 177: # too short needs padding top and bottom
525 settings += pad_TB(TIVO_WIDTH, TIVO_HEIGHT,
526 multiplier16by9, vInfo)
527 debug(('16:9 aspect allowed, file is wider ' +
528 'than 16:9 padding top and bottom\n%s') %
529 ' '.join(settings))
531 else: # too skinny needs padding on left and right.
532 settings += pad_LR(TIVO_WIDTH, TIVO_HEIGHT,
533 multiplier16by9, vInfo)
534 debug(('16:9 aspect allowed, file is narrower ' +
535 'than 16:9 padding left and right\n%s') %
536 ' '.join(settings))
538 else: # this is a 4:3 file or 16:9 output not allowed
539 if ratio > 135 and config.get169Letterbox(tsn):
540 settings.append('16:9')
541 multiplier = multiplier16by9
542 else:
543 settings.append('4:3')
544 multiplier = multiplier4by3
545 settings += pad_TB(TIVO_WIDTH, TIVO_HEIGHT,
546 multiplier, vInfo)
547 debug(('File is wider than 4:3 padding ' +
548 'top and bottom\n%s') % ' '.join(settings))
550 # If video is taller than 4:3 add left and right padding, this
551 # is rare. All of these files will always be sent in an aspect
552 # ratio of 4:3 since they are so narrow.
554 else:
555 settings.append('4:3')
556 settings += pad_LR(TIVO_WIDTH, TIVO_HEIGHT, multiplier4by3, vInfo)
557 debug('File is taller than 4:3 padding left and right\n%s'
558 % ' '.join(settings))
560 return settings
562 def tivo_compatible_video(vInfo, tsn, mime=''):
563 message = (True, '')
564 while True:
565 codec = vInfo['vCodec']
566 if mime == 'video/mp4':
567 if codec != 'h264':
568 message = (False, 'vCodec %s not compatible' % codec)
570 break
572 if mime == 'video/bif':
573 if codec != 'vc1':
574 message = (False, 'vCodec %s not compatible' % codec)
576 break
578 if codec not in ('mpeg2video', 'mpeg1video'):
579 message = (False, 'vCodec %s not compatible' % codec)
580 break
582 if vInfo['kbps'] != None:
583 abit = max('0', vInfo['aKbps'])
584 if (int(vInfo['kbps']) - int(abit) >
585 config.strtod(config.getMaxVideoBR(tsn)) / 1000):
586 message = (False, '%s kbps exceeds max video bitrate' %
587 vInfo['kbps'])
588 break
589 else:
590 message = (False, '%s kbps not supported' % vInfo['kbps'])
591 break
593 if config.isHDtivo(tsn):
594 if vInfo['par2'] != 1.0:
595 if config.getPixelAR(0):
596 if vInfo['par2'] != None or config.getPixelAR(1) != 1.0:
597 message = (False, '%s not correct PAR' % vInfo['par2'])
598 break
599 # HD Tivo detected, skipping remaining tests.
600 break
602 if not vInfo['vFps'] in ['29.97', '59.94']:
603 message = (False, '%s vFps, should be 29.97' % vInfo['vFps'])
604 break
606 if ((config.get169Blacklist(tsn) and not config.get169Setting(tsn))
607 or (config.get169Letterbox(tsn) and config.get169Setting(tsn))):
608 if vInfo['dar1'] and vInfo['dar1'] not in ('4:3', '8:9', '880:657'):
609 message = (False, ('DAR %s not supported ' +
610 'by BLACKLIST_169 tivos') % vInfo['dar1'])
611 break
613 mode = (vInfo['vWidth'], vInfo['vHeight'])
614 if mode not in [(720, 480), (704, 480), (544, 480),
615 (528, 480), (480, 480), (352, 480), (352, 240)]:
616 message = (False, '%s x %s not in supported modes' % mode)
617 break
619 return message
621 def tivo_compatible_audio(vInfo, inFile, tsn, mime=''):
622 message = (True, '')
623 while True:
624 codec = vInfo['aCodec']
625 if mime == 'video/mp4':
626 if codec not in ('mpeg4aac', 'libfaad', 'mp4a', 'aac',
627 'ac3', 'liba52'):
628 message = (False, 'aCodec %s not compatible' % codec)
629 break
630 audio_lang = config.get_tsn('audio_lang', tsn)
631 if audio_lang:
632 if vInfo['mapAudio'][0][0] != select_audiolang(inFile, tsn)[-3:]:
633 message = (False, '%s preferred audio track exists' %
634 audio_lang)
635 break
637 if mime == 'video/bif':
638 if codec != 'wmav2':
639 message = (False, 'aCodec %s not compatible' % codec)
641 break
643 if inFile[-5:].lower() == '.tivo':
644 break
646 if codec not in ('ac3', 'liba52', 'mp2'):
647 message = (False, 'aCodec %s not compatible' % codec)
648 break
650 if (not vInfo['aKbps'] or
651 int(vInfo['aKbps']) > config.getMaxAudioBR(tsn)):
652 message = (False, '%s kbps exceeds max audio bitrate' %
653 vInfo['aKbps'])
654 break
656 audio_lang = config.get_tsn('audio_lang', tsn)
657 if audio_lang:
658 if vInfo['mapAudio'][0][0] != select_audiolang(inFile, tsn)[-3:]:
659 message = (False, '%s preferred audio track exists' %
660 audio_lang)
661 break
663 return message
665 def tivo_compatible_container(vInfo, inFile, mime=''):
666 message = (True, '')
667 container = vInfo['container']
668 if ((mime == 'video/mp4' and
669 (container != 'mov' or inFile.endswith('.mov'))) or
670 (mime == 'video/bif' and container != 'asf') or
671 (mime in ['video/mpeg', ''] and
672 (container != 'mpeg' or vInfo['vCodec'] == 'mpeg1video'))):
673 message = (False, 'container %s not compatible' % container)
675 return message
677 def mp4_remuxable(inFile, tsn=''):
678 vInfo = video_info(inFile)
679 return tivo_compatible_video(vInfo, tsn, 'video/mp4')[0]
681 def mp4_remux(inFile, basename, tsn=''):
682 outFile = inFile + '.pyTivo-temp'
683 newname = basename + '.pyTivo-temp'
684 if os.path.exists(outFile):
685 return None # ugh!
687 ffmpeg_path = config.get_bin('ffmpeg')
688 fname = unicode(inFile, 'utf-8')
689 oname = unicode(outFile, 'utf-8')
690 if mswindows:
691 fname = fname.encode('iso8859-1')
692 oname = oname.encode('iso8859-1')
694 settings = {'video_codec': '-vcodec copy',
695 'video_br': select_videobr(inFile, tsn),
696 'video_fps': select_videofps(inFile, tsn),
697 'max_video_br': select_maxvideobr(tsn),
698 'buff_size': select_buffsize(tsn),
699 'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
700 'audio_br': select_audiobr(tsn),
701 'audio_fr': select_audiofr(inFile, tsn),
702 'audio_ch': select_audioch(tsn),
703 'audio_codec': select_audiocodec(False, inFile, tsn),
704 'audio_lang': select_audiolang(inFile, tsn),
705 'ffmpeg_pram': select_ffmpegprams(tsn),
706 'format': '-f mp4'}
708 cmd_string = config.getFFmpegTemplate(tsn) % settings
709 cmd = [ffmpeg_path, '-i', fname] + cmd_string.split() + [oname]
711 debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
712 debug(' '.join(cmd))
714 ffmpeg = subprocess.Popen(cmd)
715 debug('remuxing ' + inFile + ' to ' + outFile)
716 if ffmpeg.wait():
717 debug('error during remuxing')
718 os.remove(outFile)
719 return None
721 return newname
723 def tivo_compatible(inFile, tsn='', mime=''):
724 vInfo = video_info(inFile)
726 message = (True, 'all compatible')
727 if not config.get_bin('ffmpeg'):
728 if mime not in ['video/x-tivo-mpeg', 'video/mpeg', '']:
729 message = (False, 'no ffmpeg')
730 return message
732 while True:
733 vmessage = tivo_compatible_video(vInfo, tsn, mime)
734 if not vmessage[0]:
735 message = vmessage
736 break
738 amessage = tivo_compatible_audio(vInfo, inFile, tsn, mime)
739 if not amessage[0]:
740 message = amessage
741 break
743 cmessage = tivo_compatible_container(vInfo, inFile, mime)
744 if not cmessage[0]:
745 message = cmessage
747 break
749 debug('TRANSCODE=%s, %s, %s' % (['YES', 'NO'][message[0]],
750 message[1], inFile))
751 return message
753 def video_info(inFile, cache=True):
754 vInfo = dict()
755 fname = unicode(inFile, 'utf-8')
756 mtime = os.stat(fname).st_mtime
757 if cache:
758 if inFile in info_cache and info_cache[inFile][0] == mtime:
759 debug('CACHE HIT! %s' % inFile)
760 return info_cache[inFile][1]
762 vInfo['Supported'] = True
764 ffmpeg_path = config.get_bin('ffmpeg')
765 if not ffmpeg_path:
766 if os.path.splitext(inFile)[1].lower() not in ['.mpg', '.mpeg',
767 '.vob', '.tivo']:
768 vInfo['Supported'] = False
769 vInfo.update({'millisecs': 0, 'vWidth': 704, 'vHeight': 480})
770 if cache:
771 info_cache[inFile] = (mtime, vInfo)
772 return vInfo
774 if mswindows:
775 fname = fname.encode('iso8859-1')
776 cmd = [ffmpeg_path, '-i', fname]
777 # Windows and other OS buffer 4096 and ffmpeg can output more than that.
778 err_tmp = tempfile.TemporaryFile()
779 ffmpeg = subprocess.Popen(cmd, stderr=err_tmp, stdout=subprocess.PIPE,
780 stdin=subprocess.PIPE)
782 # wait configured # of seconds: if ffmpeg is not back give up
783 wait = config.getFFmpegWait()
784 debug('starting ffmpeg, will wait %s seconds for it to complete' % wait)
785 for i in xrange(wait * 20):
786 time.sleep(.05)
787 if not ffmpeg.poll() == None:
788 break
790 if ffmpeg.poll() == None:
791 kill(ffmpeg)
792 vInfo['Supported'] = False
793 if cache:
794 info_cache[inFile] = (mtime, vInfo)
795 return vInfo
797 err_tmp.seek(0)
798 output = err_tmp.read()
799 err_tmp.close()
800 debug('ffmpeg output=%s' % output)
802 attrs = {'container': r'Input #0, ([^,]+),',
803 'vCodec': r'Video: ([^, ]+)', # video codec
804 'aKbps': r'.*Audio: .+, (.+) (?:kb/s).*', # audio bitrate
805 'aCodec': r'.*Audio: ([^,]+),.*', # audio codec
806 'aFreq': r'.*Audio: .+, (.+) (?:Hz).*', # audio frequency
807 'mapVideo': r'([0-9]+\.[0-9]+).*: Video:.*'} # video mapping
809 for attr in attrs:
810 rezre = re.compile(attrs[attr])
811 x = rezre.search(output)
812 if x:
813 vInfo[attr] = x.group(1)
814 else:
815 if attr in ['container', 'vCodec']:
816 vInfo[attr] = ''
817 vInfo['Supported'] = False
818 else:
819 vInfo[attr] = None
820 debug('failed at ' + attr)
822 rezre = re.compile(r'.*Audio: .+, (?:(\d+)(?:(?:\.(\d))?(?: channels)?)|stereo),.*')
823 x = rezre.search(output)
824 if x:
825 if x.group(1) == 'stereo':
826 vInfo['aCh'] = 2
827 elif x.group(2):
828 vInfo['aCh'] = int(x.group(1)) + int(x.group(2))
829 else:
830 vInfo['aCh'] = int(x.group(1))
831 else:
832 vInfo['aCh'] = ''
833 debug('failed at aCh')
835 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
836 x = rezre.search(output)
837 if x:
838 vInfo['vWidth'] = int(x.group(1))
839 vInfo['vHeight'] = int(x.group(2))
840 else:
841 vInfo['vWidth'] = ''
842 vInfo['vHeight'] = ''
843 vInfo['Supported'] = False
844 debug('failed at vWidth/vHeight')
846 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb\(r\)|tbr).*')
847 x = rezre.search(output)
848 if x:
849 vInfo['vFps'] = x.group(1)
850 if '.' not in vInfo['vFps']:
851 vInfo['vFps'] += '.00'
853 # Allow override only if it is mpeg2 and frame rate was doubled
854 # to 59.94
856 if vInfo['vCodec'] == 'mpeg2video' and vInfo['vFps'] != '29.97':
857 # First look for the build 7215 version
858 rezre = re.compile(r'.*film source: 29.97.*')
859 x = rezre.search(output.lower())
860 if x:
861 debug('film source: 29.97 setting vFps to 29.97')
862 vInfo['vFps'] = '29.97'
863 else:
864 # for build 8047:
865 rezre = re.compile(r'.*frame rate differs from container ' +
866 r'frame rate: 29.97.*')
867 debug('Bug in VideoReDo')
868 x = rezre.search(output.lower())
869 if x:
870 vInfo['vFps'] = '29.97'
871 else:
872 vInfo['vFps'] = ''
873 vInfo['Supported'] = False
874 debug('failed at vFps')
876 durre = re.compile(r'.*Duration: ([0-9]+):([0-9]+):([0-9]+)\.([0-9]+),')
877 d = durre.search(output)
879 if d:
880 vInfo['millisecs'] = ((int(d.group(1)) * 3600 +
881 int(d.group(2)) * 60 +
882 int(d.group(3))) * 1000 +
883 int(d.group(4)) * (10 ** (3 - len(d.group(4)))))
884 else:
885 vInfo['millisecs'] = 0
887 # get bitrate of source for tivo compatibility test.
888 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
889 x = rezre.search(output)
890 if x:
891 vInfo['kbps'] = x.group(1)
892 else:
893 # Fallback method of getting video bitrate
894 # Sample line: Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p,
895 # 720x480 [PAR 32:27 DAR 16:9], 9800 kb/s, 59.94 tb(r)
896 rezre = re.compile(r'.*Stream #0\.0\[.*\]: Video: mpeg2video, ' +
897 r'\S+, \S+ \[.*\], (\d+) (?:kb/s).*')
898 x = rezre.search(output)
899 if x:
900 vInfo['kbps'] = x.group(1)
901 else:
902 vInfo['kbps'] = None
903 debug('failed at kbps')
905 # get par.
906 rezre = re.compile(r'.*Video: .+PAR ([0-9]+):([0-9]+) DAR [0-9:]+.*')
907 x = rezre.search(output)
908 if x and x.group(1) != "0" and x.group(2) != "0":
909 vInfo['par1'] = x.group(1) + ':' + x.group(2)
910 vInfo['par2'] = float(x.group(1)) / float(x.group(2))
911 else:
912 vInfo['par1'], vInfo['par2'] = None, None
914 # get dar.
915 rezre = re.compile(r'.*Video: .+DAR ([0-9]+):([0-9]+).*')
916 x = rezre.search(output)
917 if x and x.group(1) != "0" and x.group(2) != "0":
918 vInfo['dar1'] = x.group(1) + ':' + x.group(2)
919 else:
920 vInfo['dar1'] = None
922 # get Audio Stream mapping.
923 rezre = re.compile(r'([0-9]+\.[0-9]+)(.*): Audio:(.*)')
924 x = rezre.search(output)
925 amap = []
926 if x:
927 for x in rezre.finditer(output):
928 amap.append((x.group(1), x.group(2)+x.group(3)))
929 else:
930 amap.append(('', ''))
931 debug('failed at mapAudio')
932 vInfo['mapAudio'] = amap
934 vInfo['par'] = None
936 data = metadata.from_text(inFile)
937 for key in data:
938 if key.startswith('Override_'):
939 vInfo['Supported'] = True
940 if key.startswith('Override_mapAudio'):
941 audiomap = dict(vInfo['mapAudio'])
942 stream = key.replace('Override_mapAudio', '').strip()
943 if stream in audiomap:
944 newaudiomap = (stream, data[key])
945 audiomap.update([newaudiomap])
946 vInfo['mapAudio'] = sorted(audiomap.items(),
947 key=lambda (k,v): (k,v))
948 elif key.startswith('Override_millisecs'):
949 vInfo[key.replace('Override_', '')] = int(data[key])
950 else:
951 vInfo[key.replace('Override_', '')] = data[key]
953 if cache:
954 info_cache[inFile] = (mtime, vInfo)
955 debug("; ".join(["%s=%s" % (k, v) for k, v in vInfo.items()]))
956 return vInfo
958 def audio_check(inFile, tsn):
959 cmd_string = ('-y -vcodec mpeg2video -r 29.97 -b 1000k -acodec copy ' +
960 select_audiolang(inFile, tsn) + ' -t 00:00:01 -f vob -')
961 fname = unicode(inFile, 'utf-8')
962 if mswindows:
963 fname = fname.encode('iso8859-1')
964 cmd = [config.get_bin('ffmpeg'), '-i', fname] + cmd_string.split()
965 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
966 fd, testname = tempfile.mkstemp()
967 testfile = os.fdopen(fd, 'wb')
968 try:
969 shutil.copyfileobj(ffmpeg.stdout, testfile)
970 except:
971 kill(ffmpeg)
972 testfile.close()
973 vInfo = None
974 else:
975 testfile.close()
976 vInfo = video_info(testname, False)
977 os.remove(testname)
978 return vInfo
980 def supported_format(inFile):
981 if video_info(inFile)['Supported']:
982 return True
983 else:
984 debug('FALSE, file not supported %s' % inFile)
985 return False
987 def kill(popen):
988 debug('killing pid=%s' % str(popen.pid))
989 if mswindows:
990 win32kill(popen.pid)
991 else:
992 import os, signal
993 for i in xrange(3):
994 debug('sending SIGTERM to pid: %s' % popen.pid)
995 os.kill(popen.pid, signal.SIGTERM)
996 time.sleep(.5)
997 if popen.poll() is not None:
998 debug('process %s has exited' % popen.pid)
999 break
1000 else:
1001 while popen.poll() is None:
1002 debug('sending SIGKILL to pid: %s' % popen.pid)
1003 os.kill(popen.pid, signal.SIGKILL)
1004 time.sleep(.5)
1006 def win32kill(pid):
1007 import ctypes
1008 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
1009 ctypes.windll.kernel32.TerminateProcess(handle, -1)
1010 ctypes.windll.kernel32.CloseHandle(handle)
1012 def gcd(a, b):
1013 while b:
1014 a, b = b, a % b
1015 return a