17 logger
= logging
.getLogger('pyTivo.video.transcode')
19 info_cache
= lrucache
.LRUCache(1000)
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
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")
53 msg
= msg
.decode('utf8')
55 if sys
.platform
== 'darwin':
56 msg
= msg
.decode('macroman')
58 msg
= msg
.decode('iso8859-1')
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
)}
79 ffmpeg_path
= config
.get_bin('ffmpeg')
80 cmd_string
= config
.getFFmpegTemplate(tsn
) % settings
81 fname
= unicode(inFile
, 'utf-8')
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
,
91 if tivo_compatible(inFile
, tsn
)[0]:
95 cmd
= [ffmpeg_path
, '-i', '-'] + cmd_string
.split()
96 ffmpeg
= subprocess
.Popen(cmd
, stdin
=tivodecode
.stdout
,
97 stdout
=subprocess
.PIPE
,
100 cmd
= [ffmpeg_path
, '-i', fname
] + cmd_string
.split()
101 ffmpeg
= subprocess
.Popen(cmd
, bufsize
=(512 * 1024),
102 stdout
=subprocess
.PIPE
)
105 debug('transcoding to tivo model ' + tsn
[:3] + ' using ffmpeg command:')
108 ffmpeg_procs
[inFile
] = {'process': ffmpeg
, 'start': 0, 'end': 0,
109 'last_read': time
.time(), 'blocks': []}
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']:
120 kill(proc
['process'])
123 def resume_transfer(inFile
, outFile
, offset
):
124 proc
= ffmpeg_procs
[inFile
]
125 offset
-= proc
['start']
129 for block
in proc
['blocks']:
133 block
= block
[offset
:]
134 outFile
.write('%x\r\n' % len(block
))
136 outFile
.write('\r\n')
140 except Exception, msg
:
144 proc
['start'] = proc
['end']
147 return count
+ transfer_blocks(inFile
, outFile
)
149 def transfer_blocks(inFile
, outFile
):
150 proc
= ffmpeg_procs
[inFile
]
151 blocks
= proc
['blocks']
156 block
= proc
['process'].stdout
.read(BLOCKSIZE
)
157 proc
['last_read'] = time
.time()
158 except Exception, msg
:
161 kill(proc
['process'])
167 except Exception, msg
:
174 proc
['end'] += len(block
)
175 if len(blocks
) > MAXBLOCKS
:
176 proc
['start'] += len(blocks
[0])
180 outFile
.write('%x\r\n' % len(block
))
182 outFile
.write('\r\n')
184 except Exception, msg
:
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
]
196 kill(proc
['process'])
198 reaper
= threading
.Timer(TIMEOUT
, reap_process
, (inFile
,))
199 reapers
[inFile
] = reaper
203 del ffmpeg_procs
[inFile
]
204 reapers
[inFile
].cancel()
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
)
214 # Default, compatible with all TiVo's
216 if vInfo
['aCodec'] in ('libfaad', 'ac3', 'liba52', 'mp2'):
217 aKbps
= vInfo
['aKbps']
221 vInfoQuery
= audio_check(inFile
, tsn
)
222 if vInfoQuery
== None:
226 aKbps
= vInfoQuery
['aKbps']
227 aCh
= vInfoQuery
['aCh']
230 if aKbps
!= None and int(aKbps
) <= config
.getMaxAudioBR(tsn
):
231 # compatible codec and bitrate, do not reencode audio
233 if vInfo
['aCodec'] == 'libfaad' and aCh
!= 2:
235 copy_flag
= config
.get_tsn('copy_ts', tsn
)
237 if ((codec
== 'copy' and codectype
== 'mpeg2video' and not copy_flag
) or
238 (copy_flag
and copy_flag
.lower() == 'false')):
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
)
253 def select_audioch(tsn
):
254 ch
= config
.get_tsn('audio_ch', tsn
)
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]
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
))
272 #if only 1 item matched we're done
273 if len(langmatch_curr
) == 1:
274 del langmatch_prev
[:]
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
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]
289 debug('selected audio stream: %s' % stream
)
290 return '-map ' + vInfo
['mapVideo'] + ' -map ' + stream
291 debug('selected audio stream: %s' % '')
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
:
299 video_fps
= config
.get_tsn('video_fps', tsn
)
300 if video_fps
!= None:
301 fps
= '-r ' + video_fps
304 def select_videocodec(inFile
, tsn
):
305 vInfo
= video_info(inFile
)
306 if tivo_compatible_video(vInfo
, tsn
)[0]:
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'])
320 video_str
-= int(vInfo
['aKbps'])
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,
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
)
348 def select_format(tsn
):
350 return '-f %s -' % fmt
354 if pad_style
== UNSET
:
356 filters
= tempfile
.TemporaryFile()
357 cmd
= [config
.get_bin('ffmpeg'), '-filters']
358 ffmpeg
= subprocess
.Popen(cmd
, stdout
=filters
, stderr
=subprocess
.PIPE
)
362 if line
.startswith('pad'):
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
)
373 if endHeight
< TIVO_HEIGHT
* 0.99:
374 topPadding
= (TIVO_HEIGHT
- endHeight
) / 2
379 return ['-s', '%sx%s' % (TIVO_WIDTH
, endHeight
), '-vf',
380 'pad=%d:%d:0:%d' % (TIVO_WIDTH
, TIVO_HEIGHT
, topPadding
)]
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
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
))
395 if endWidth
< TIVO_WIDTH
* 0.99:
396 leftPadding
= (TIVO_WIDTH
- endWidth
) / 2
401 return ['-s', '%sx%s' % (endWidth
, TIVO_HEIGHT
), '-vf',
402 'pad=%d:%d:%d:0' % (TIVO_WIDTH
, TIVO_HEIGHT
, leftPadding
)]
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
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
)
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'):
438 elif vInfo
.get('par'):
439 par2
= float(vInfo
['par'])
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:
455 npar
= config
.getPixelAR(1)
459 # adjust for pixel aspect ratio, if set
462 return ['-s', '%dx%d' % (vInfo
['vWidth'],
463 math
.ceil(vInfo
['vHeight'] / npar
))]
465 # FFMPEG expects width to be a multiple of two
466 return ['-s', '%dx%d' % (math
.ceil(vInfo
['vWidth']*npar
/2.0)*2,
469 if vInfo
['vHeight'] <= TIVO_HEIGHT
:
470 # pass all resolutions to S3, except heights greater than
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
)):
499 return ['-aspect', aspect
, '-s', '%sx%s' % (TIVO_WIDTH
, TIVO_HEIGHT
)]
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
,
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
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')
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') %
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') %
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
543 settings
.append('4:3')
544 multiplier
= multiplier4by3
545 settings
+= pad_TB(TIVO_WIDTH
, TIVO_HEIGHT
,
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.
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
))
562 def tivo_compatible_video(vInfo
, tsn
, mime
=''):
565 codec
= vInfo
['vCodec']
566 if mime
== 'video/mp4':
568 message
= (False, 'vCodec %s not compatible' % codec
)
572 if mime
== 'video/bif':
574 message
= (False, 'vCodec %s not compatible' % codec
)
578 if codec
not in ('mpeg2video', 'mpeg1video'):
579 message
= (False, 'vCodec %s not compatible' % codec
)
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' %
590 message
= (False, '%s kbps not supported' % vInfo
['kbps'])
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'])
599 # HD Tivo detected, skipping remaining tests.
602 if not vInfo
['vFps'] in ['29.97', '59.94']:
603 message
= (False, '%s vFps, should be 29.97' % vInfo
['vFps'])
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'])
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
)
621 def tivo_compatible_audio(vInfo
, inFile
, tsn
, mime
=''):
624 codec
= vInfo
['aCodec']
625 if mime
== 'video/mp4':
626 if codec
not in ('mpeg4aac', 'libfaad', 'mp4a', 'aac',
628 message
= (False, 'aCodec %s not compatible' % codec
)
630 audio_lang
= config
.get_tsn('audio_lang', tsn
)
632 if vInfo
['mapAudio'][0][0] != select_audiolang(inFile
, tsn
)[-3:]:
633 message
= (False, '%s preferred audio track exists' %
637 if mime
== 'video/bif':
639 message
= (False, 'aCodec %s not compatible' % codec
)
643 if inFile
[-5:].lower() == '.tivo':
646 if codec
not in ('ac3', 'liba52', 'mp2'):
647 message
= (False, 'aCodec %s not compatible' % codec
)
650 if (not vInfo
['aKbps'] or
651 int(vInfo
['aKbps']) > config
.getMaxAudioBR(tsn
)):
652 message
= (False, '%s kbps exceeds max audio bitrate' %
656 audio_lang
= config
.get_tsn('audio_lang', tsn
)
658 if vInfo
['mapAudio'][0][0] != select_audiolang(inFile
, tsn
)[-3:]:
659 message
= (False, '%s preferred audio track exists' %
665 def tivo_compatible_container(vInfo
, inFile
, mime
=''):
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
)
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
):
687 ffmpeg_path
= config
.get_bin('ffmpeg')
688 fname
= unicode(inFile
, 'utf-8')
689 oname
= unicode(outFile
, 'utf-8')
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
),
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:')
714 ffmpeg
= subprocess
.Popen(cmd
)
715 debug('remuxing ' + inFile
+ ' to ' + outFile
)
717 debug('error during remuxing')
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')
733 vmessage
= tivo_compatible_video(vInfo
, tsn
, mime
)
738 amessage
= tivo_compatible_audio(vInfo
, inFile
, tsn
, mime
)
743 cmessage
= tivo_compatible_container(vInfo
, inFile
, mime
)
749 debug('TRANSCODE=%s, %s, %s' % (['YES', 'NO'][message
[0]],
753 def video_info(inFile
, cache
=True):
755 fname
= unicode(inFile
, 'utf-8')
756 mtime
= os
.stat(fname
).st_mtime
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')
766 if os
.path
.splitext(inFile
)[1].lower() not in ['.mpg', '.mpeg',
768 vInfo
['Supported'] = False
769 vInfo
.update({'millisecs': 0, 'vWidth': 704, 'vHeight': 480})
771 info_cache
[inFile
] = (mtime
, vInfo
)
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):
787 if not ffmpeg
.poll() == None:
790 if ffmpeg
.poll() == None:
792 vInfo
['Supported'] = False
794 info_cache
[inFile
] = (mtime
, vInfo
)
798 output
= err_tmp
.read()
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
810 rezre
= re
.compile(attrs
[attr
])
811 x
= rezre
.search(output
)
813 vInfo
[attr
] = x
.group(1)
815 if attr
in ['container', 'vCodec']:
817 vInfo
['Supported'] = False
820 debug('failed at ' + attr
)
822 rezre
= re
.compile(r
'.*Audio: .+, (?:(\d+)(?:(?:\.(\d))?(?: channels)?)|stereo),.*')
823 x
= rezre
.search(output
)
825 if x
.group(1) == 'stereo':
828 vInfo
['aCh'] = int(x
.group(1)) + int(x
.group(2))
830 vInfo
['aCh'] = int(x
.group(1))
833 debug('failed at aCh')
835 rezre
= re
.compile(r
'.*Video: .+, (\d+)x(\d+)[, ].*')
836 x
= rezre
.search(output
)
838 vInfo
['vWidth'] = int(x
.group(1))
839 vInfo
['vHeight'] = int(x
.group(2))
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
)
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
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())
861 debug('film source: 29.97 setting vFps to 29.97')
862 vInfo
['vFps'] = '29.97'
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())
870 vInfo
['vFps'] = '29.97'
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
)
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)))))
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
)
891 vInfo
['kbps'] = x
.group(1)
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
)
900 vInfo
['kbps'] = x
.group(1)
903 debug('failed at kbps')
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))
912 vInfo
['par1'], vInfo
['par2'] = None, None
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)
922 # get Audio Stream mapping.
923 rezre
= re
.compile(r
'([0-9]+\.[0-9]+)(.*): Audio:(.*)')
924 x
= rezre
.search(output
)
927 for x
in rezre
.finditer(output
):
928 amap
.append((x
.group(1), x
.group(2)+x
.group(3)))
930 amap
.append(('', ''))
931 debug('failed at mapAudio')
932 vInfo
['mapAudio'] = amap
936 data
= metadata
.from_text(inFile
)
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
])
951 vInfo
[key
.replace('Override_', '')] = data
[key
]
954 info_cache
[inFile
] = (mtime
, vInfo
)
955 debug("; ".join(["%s=%s" % (k
, v
) for k
, v
in vInfo
.items()]))
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')
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')
969 shutil
.copyfileobj(ffmpeg
.stdout
, testfile
)
976 vInfo
= video_info(testname
, False)
980 def supported_format(inFile
):
981 if video_info(inFile
)['Supported']:
984 debug('FALSE, file not supported %s' % inFile
)
988 debug('killing pid=%s' % str(popen
.pid
))
994 debug('sending SIGTERM to pid: %s' % popen
.pid
)
995 os
.kill(popen
.pid
, signal
.SIGTERM
)
997 if popen
.poll() is not None:
998 debug('process %s has exited' % popen
.pid
)
1001 while popen
.poll() is None:
1002 debug('sending SIGKILL to pid: %s' % popen
.pid
)
1003 os
.kill(popen
.pid
, signal
.SIGKILL
)
1008 handle
= ctypes
.windll
.kernel32
.OpenProcess(1, False, pid
)
1009 ctypes
.windll
.kernel32
.TerminateProcess(handle
, -1)
1010 ctypes
.windll
.kernel32
.CloseHandle(handle
)