1 import subprocess
, shutil
, os
, re
, sys
, tempfile
, ConfigParser
, time
, lrucache
, math
5 logger
= logging
.getLogger('pyTivo.video.transcode')
7 info_cache
= lrucache
.LRUCache(1000)
8 videotest
= os
.path
.join(os
.path
.dirname(__file__
), 'videotest.mpg')
10 BAD_MPEG_FPS
= ['15.00']
13 return config
.get('Server', 'ffmpeg')
16 # subprocess is broken for me on windows so super hack
17 def patchSubprocess():
18 o
= subprocess
.Popen
._make
_inheritable
20 def _make_inheritable(self
, handle
):
21 if not handle
: return subprocess
.GetCurrentProcess()
22 return o(self
, handle
)
24 subprocess
.Popen
._make
_inheritable
= _make_inheritable
25 mswindows
= (sys
.platform
== "win32")
29 def output_video(inFile
, outFile
, tsn
=''):
30 if tivo_compatable(inFile
, tsn
):
31 logger
.debug('%s is tivo compatible' % inFile
)
32 f
= file(inFile
, 'rb')
33 shutil
.copyfileobj(f
, outFile
)
36 logger
.debug('%s is not tivo compatible' % inFile
)
37 transcode(inFile
, outFile
, tsn
)
39 def transcode(inFile
, outFile
, tsn
=''):
42 settings
['video_codec'] = select_videocodec(tsn
)
43 settings
['video_br'] = select_videobr(inFile
, tsn
)
44 settings
['video_fps'] = select_videofps(inFile
, tsn
)
45 settings
['max_video_br'] = select_maxvideobr()
46 settings
['buff_size'] = select_buffsize()
47 settings
['aspect_ratio'] = ' '.join(select_aspect(inFile
, tsn
))
48 settings
['audio_br'] = select_audiobr(tsn
)
49 settings
['audio_fr'] = select_audiofr(inFile
, tsn
)
50 settings
['audio_ch'] = select_audioch(tsn
)
51 settings
['audio_codec'] = select_audiocodec(inFile
, tsn
)
52 settings
['ffmpeg_pram'] = select_ffmpegprams(tsn
)
53 settings
['format'] = select_format(tsn
)
55 cmd_string
= config
.getFFmpegTemplate(tsn
) % settings
57 cmd
= [ffmpeg_path(), '-i', inFile
] + cmd_string
.split()
58 logging
.debug('transcoding to tivo model '+tsn
[:3]+' using ffmpeg command:')
59 logging
.debug(' '.join(cmd
))
60 ffmpeg
= subprocess
.Popen(cmd
, bufsize
=512*1024, stdout
=subprocess
.PIPE
)
63 shutil
.copyfileobj(ffmpeg
.stdout
, outFile
)
67 def select_audiocodec(inFile
, tsn
= ''):
68 # Default, compatible with all TiVo's
70 vInfo
= video_info(inFile
)
71 if config
.getAudioCodec(tsn
) == None:
72 if vInfo
['acodec'] in ('ac3', 'liba52', 'mp2'):
73 if vInfo
['akbps'] == None:
74 cmd_string
= '-y -vcodec mpeg2video -r 29.97 -b 1000k -acodec copy -t 00:00:01 -f vob -'
75 if video_check(inFile
, cmd_string
):
76 vInfo
= video_info(videotest
)
77 if not vInfo
['akbps'] == None and int(vInfo
['akbps']) <= config
.getMaxAudioBR(tsn
):
78 # compatible codec and bitrate, do not reencode audio
81 codec
= config
.getAudioCodec(tsn
)
83 if (codec
== 'copy' and config
.getCopyTS(tsn
).lower() == 'none' \
84 and type == 'mpeg2video') or config
.getCopyTS(tsn
).lower() == 'false':
86 return '-acodec '+codec
+copyts
88 def select_audiofr(inFile
, tsn
):
89 freq
= '48000' #default
90 vInfo
= video_info(inFile
)
91 if not vInfo
['afreq'] == None and vInfo
['afreq'] in ('44100', '48000'):
92 # compatible frequency
94 if config
.getAudioFR(tsn
) != None:
95 freq
= config
.getAudioFR(tsn
)
98 def select_audioch(tsn
):
99 if config
.getAudioCH(tsn
) != None:
100 return '-ac '+config
.getAudioCH(tsn
)
103 def select_videofps(inFile
, tsn
):
104 vInfo
= video_info(inFile
)
105 vfps
= '-r 29.97' #default
106 if config
.isHDtivo(tsn
) and vInfo
['fps'] not in BAD_MPEG_FPS
:
108 if config
.getVideoFPS(tsn
) != None:
109 vfps
= '-r '+config
.getVideoFPS(tsn
)
112 def select_videocodec(tsn
):
113 vcodec
= 'mpeg2video' #default
114 if config
.getVideoCodec(tsn
) != None:
115 vcodec
= config
.getVideoCodec(tsn
)
116 return '-vcodec '+vcodec
118 def select_videobr(inFile
, tsn
):
119 return '-b '+select_videostr(inFile
, tsn
)
121 def select_videostr(inFile
, tsn
):
122 video_str
= config
.getVideoBR(tsn
)
123 if config
.isHDtivo(tsn
):
124 vInfo
= video_info(inFile
)
125 if vInfo
['kbps'] != None and config
.getVideoPCT() > 0:
126 video_percent
= int(vInfo
['kbps'])*10*config
.getVideoPCT()
127 video_bitrate
= max(config
.strtod(video_str
), video_percent
)
128 video_str
= str(int(min(config
.strtod(config
.getMaxVideoBR())*0.95, video_bitrate
)))
131 def select_audiobr(tsn
):
132 return '-ab '+config
.getAudioBR(tsn
)
134 def select_maxvideobr():
135 return '-maxrate '+config
.getMaxVideoBR()
137 def select_buffsize():
138 return '-bufsize '+config
.getBuffSize()
140 def select_ffmpegprams(tsn
):
141 if config
.getFFmpegPrams(tsn
) != None:
142 return config
.getFFmpegPrams(tsn
)
145 def select_format(tsn
):
147 if config
.getFormat(tsn
) != None:
148 fmt
= config
.getFormat(tsn
)
149 return '-f '+fmt
+' -'
151 def select_aspect(inFile
, tsn
= ''):
152 TIVO_WIDTH
= config
.getTivoWidth(tsn
)
153 TIVO_HEIGHT
= config
.getTivoHeight(tsn
)
155 vInfo
= video_info(inFile
)
157 logging
.debug('tsn: %s' % tsn
)
159 aspect169
= config
.get169Setting(tsn
)
161 logging
.debug('aspect169:%s' % aspect169
)
163 optres
= config
.getOptres(tsn
)
165 logging
.debug('optres:%s' % optres
)
168 optHeight
= config
.nearestTivoHeight(vInfo
['height'])
169 optWidth
= config
.nearestTivoWidth(vInfo
['width'])
170 if optHeight
< TIVO_HEIGHT
:
171 TIVO_HEIGHT
= optHeight
172 if optWidth
< TIVO_WIDTH
:
173 TIVO_WIDTH
= optWidth
175 d
= gcd(vInfo
['height'],vInfo
['width'])
176 ratio
= (width
*100)/vInfo
['height']
177 rheight
, rwidth
= vInfo
['height']/d
, vInfo
['width']/d
179 logger
.debug('File=%s Type=%s width=%s height=%s fps=%s millisecs=%s ratio=%s rheight=%s rwidth=%s TIVO_HEIGHT=%sTIVO_WIDTH=%s' % (inFile
, vInfo
['codec'], vInfo
['width'], vInfo
['height'], vInfo
['fps'], vInfo
['millisecs'], ratio
, rheight
, rwidth
, TIVO_HEIGHT
, TIVO_WIDTH
))
181 multiplier16by9
= (16.0 * TIVO_HEIGHT
) / (9.0 * TIVO_WIDTH
)
182 multiplier4by3
= (4.0 * TIVO_HEIGHT
) / (3.0 * TIVO_WIDTH
)
184 if config
.isHDtivo(tsn
) and not optres
:
185 if config
.getPixelAR(0):
187 npar
= config
.getPixelAR(1)
190 # adjust for pixel aspect ratio, if set, because TiVo expects square pixels
192 return ['-s', str(width
) + 'x' + str(int(math
.ceil(height
/npar
)))]
194 # FFMPEG expects width to be a multiple of two
195 return ['-s', str(int(math
.ceil(width
*npar
/2.0)*2)) + 'x' + str(height
)]
196 if height
<= TIVO_HEIGHT
:
197 # pass all resolutions to S3, except heights greater than conf height
199 # else, resize video.
200 if (rwidth
, rheight
) in [(1, 1)] and par1
== '8:9':
201 logger
.debug('File + PAR is within 4:3.')
202 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
203 elif (rwidth
, rheight
) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)] or dar1
== '4:3':
204 logger
.debug('File is within 4:3 list.')
205 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
206 elif ((rwidth
, rheight
) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)] or dar1
== '16:9')\
207 and (aspect169
or config
.get169Letterbox(tsn
)):
208 logger
.debug('File is within 16:9 list and 16:9 allowed.')
209 if config
.get169Blacklist(tsn
) or (aspect169
and config
.get169Letterbox(tsn
)):
210 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
212 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
215 #If video is wider than 4:3 add top and bottom padding
216 if (ratio
> 133): #Might be 16:9 file, or just need padding on top and bottom
217 if aspect169
and (ratio
> 135): #If file would fall in 4:3 assume it is supposed to be 4:3
218 if (ratio
> 177):#too short needs padding top and bottom
219 endHeight
= int(((TIVO_WIDTH
*height
)/width
) * multiplier16by9
)
220 settings
.append('-aspect')
221 if config
.get169Blacklist(tsn
) or config
.get169Letterbox(tsn
):
222 settings
.append('4:3')
224 settings
.append('16:9')
227 if endHeight
< TIVO_HEIGHT
* 0.99:
228 settings
.append('-s')
229 settings
.append(str(TIVO_WIDTH
) + 'x' + str(endHeight
))
231 topPadding
= ((TIVO_HEIGHT
- endHeight
)/2)
235 settings
.append('-padtop')
236 settings
.append(str(topPadding
))
237 bottomPadding
= (TIVO_HEIGHT
- endHeight
) - topPadding
238 settings
.append('-padbottom')
239 settings
.append(str(bottomPadding
))
240 else: #if only very small amount of padding needed, then just stretch it
241 settings
.append('-s')
242 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
243 logger
.debug('16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n%s' % ' '.join(settings
))
244 else: #too skinny needs padding on left and right.
245 endWidth
= int((TIVO_HEIGHT
*width
)/(height
*multiplier16by9
))
246 settings
.append('-aspect')
247 if config
.get169Blacklist(tsn
) or config
.get169Letterbox(tsn
):
248 settings
.append('4:3')
250 settings
.append('16:9')
253 if endWidth
< (TIVO_WIDTH
-10):
254 settings
.append('-s')
255 settings
.append(str(endWidth
) + 'x' + str(TIVO_HEIGHT
))
257 leftPadding
= ((TIVO_WIDTH
- endWidth
)/2)
261 settings
.append('-padleft')
262 settings
.append(str(leftPadding
))
263 rightPadding
= (TIVO_WIDTH
- endWidth
) - leftPadding
264 settings
.append('-padright')
265 settings
.append(str(rightPadding
))
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 logger
.debug('16:9 aspect allowed, file is narrower than 16:9 padding left and right\n%s' % ' '.join(settings
))
270 else: #this is a 4:3 file or 16:9 output not allowed
271 multiplier
= multiplier4by3
272 settings
.append('-aspect')
273 if ratio
> 135 and config
.get169Letterbox(tsn
):
274 settings
.append('16:9')
275 multiplier
= multiplier16by9
277 settings
.append('4:3')
278 endHeight
= int(((TIVO_WIDTH
*height
)/width
) * multiplier
)
281 if endHeight
< TIVO_HEIGHT
* 0.99:
282 settings
.append('-s')
283 settings
.append(str(TIVO_WIDTH
) + 'x' + str(endHeight
))
285 topPadding
= ((TIVO_HEIGHT
- endHeight
)/2)
289 settings
.append('-padtop')
290 settings
.append(str(topPadding
))
291 bottomPadding
= (TIVO_HEIGHT
- endHeight
) - topPadding
292 settings
.append('-padbottom')
293 settings
.append(str(bottomPadding
))
294 else: #if only very small amount of padding needed, then just stretch it
295 settings
.append('-s')
296 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
297 logging
.debug('File is wider than 4:3 padding top and bottom\n%s' % ' '.join(settings
))
300 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
301 #an aspect ratio of 4:3 since they are so narrow.
303 endWidth
= int((TIVO_HEIGHT
*width
)/(height
*multiplier4by3
))
304 settings
.append('-aspect')
305 settings
.append('4:3')
308 if endWidth
< (TIVO_WIDTH
* 0.99):
309 settings
.append('-s')
310 settings
.append(str(endWidth
) + 'x' + str(TIVO_HEIGHT
))
312 leftPadding
= ((TIVO_WIDTH
- endWidth
)/2)
316 settings
.append('-padleft')
317 settings
.append(str(leftPadding
))
318 rightPadding
= (TIVO_WIDTH
- endWidth
) - leftPadding
319 settings
.append('-padright')
320 settings
.append(str(rightPadding
))
321 else: #if only very small amount of padding needed, then just stretch it
322 settings
.append('-s')
323 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
325 logger
.debug('File is taller than 4:3 padding left and right\n%s' % ' '.join(settings
))
329 def tivo_compatable(inFile
, tsn
= ''):
330 supportedModes
= [[720, 480], [704, 480], [544, 480], [528, 480], [480, 480], [352, 480]]
331 vInfo
= video_info(inFile
)
332 #print type, width, height, fps, millisecs, kbps, akbps, acodec
334 if (inFile
[-5:]).lower() == '.tivo':
335 logger
.debug('TRUE, ends with .tivo. %s' % inFile
)
338 if not vInfo
['codec'] == 'mpeg2video':
339 #print 'Not Tivo Codec'
340 logger
.debug('FALSE, type %s not mpeg2video. %s' % (vInfo
['codec'], inFile
))
343 if os
.path
.splitext(inFile
)[-1].lower() in ('.ts', '.mpv', '.tp'):
344 logger
.debug('FALSE, ext %s not tivo compatible. %s' % (os
.path
.splitext(inFile
)[-1], inFile
))
347 if vInfo
['acodec'] == 'dca':
348 logger
.debug('FALSE, acodec %s not supported. %s' % (vInfo
['acodec'], inFile
))
351 if vInfo
['acodec'] != None:
352 if not vInfo
['akbps'] or int(vInfo
['akbps']) > config
.getMaxAudioBR(tsn
):
353 logger
.debug('FALSE, %s kbps exceeds max audio bitrate. %s' % (vInfo
['akbps'], inFile
))
356 if vInfo
['kbps'] != None:
357 abit
= max('0', vInfo
['akbps'])
358 if int(vInfo
['kbps'])-int(abit
) > config
.strtod(config
.getMaxVideoBR())/1000:
359 logger
.debug('FALSE, %s kbps exceeds max video bitrate. %s' % (vInfo
['kbps'], inFile
))
362 logger
.debug('FALSE, %s kbps not supported. %s' % (vInfo
['kbps'], inFile
))
365 if config
.isHDtivo(tsn
):
366 if vInfo
['par2'] != 1.0:
367 if config
.getPixelAR(0):
368 if vInfo
['par2'] != None or config
.getPixelAR(1) != 1.0:
369 logger
.debug('FALSE, %s not correct PAR, %s' % (vInfo
['par2'], inFile
))
371 logger
.debug('TRUE, HD Tivo detected, skipping remaining tests %s' % inFile
)
374 if not vInfo
['fps'] == '29.97':
375 #print 'Not Tivo fps'
376 logger
.debug('FALSE, %s fps, should be 29.97. %s' % (vInfo
['fps'], inFile
))
379 if (config
.get169Blacklist(tsn
) and not config
.get169Setting(tsn
))\
380 or (config
.get169Letterbox(tsn
) and config
.get169Setting(tsn
)):
381 if vInfo
['dar1'] == None or not vInfo
['dar1'] in ('4:3', '8:9'):
382 debug_write(__name__
, fn_attr(), ['FALSE, DAR', vInfo
['dar1'], 'not supported by BLACKLIST_169 tivos.', inFile
])
385 for mode
in supportedModes
:
386 if (mode
[0], mode
[1]) == (vInfo
['width'], vInfo
['height']):
387 logger
.debug('TRUE, %s x %s is valid. %s' % (vInfo
['width'], vInfo
['height'], inFile
))
389 #print 'Not Tivo dimensions'
390 logger
.debug('FALSE, %s x %s not in supported modes. %s' % (vInfo
['width'], vInfo
['height'], inFile
))
393 def video_info(inFile
):
395 mtime
= os
.stat(inFile
).st_mtime
396 if inFile
!= videotest
:
397 if inFile
in info_cache
and info_cache
[inFile
][0] == mtime
:
398 logging
.debug('CACHE HIT! %s' % inFile
)
399 return info_cache
[inFile
][1]
401 if (inFile
[-5:]).lower() == '.tivo':
402 vInfo
['Supported'] = True
403 vInfo
['millisecs'] = 0
404 info_cache
[inFile
] = (mtime
, vInfo
)
405 logger
.debug('VALID, ends in .tivo. %s' % inFile
)
408 cmd
= [ffmpeg_path(), '-i', inFile
]
409 # Windows and other OS buffer 4096 and ffmpeg can output more than that.
410 err_tmp
= tempfile
.TemporaryFile()
411 ffmpeg
= subprocess
.Popen(cmd
, stderr
=err_tmp
, stdout
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
413 # wait 10 sec if ffmpeg is not back give up
414 for i
in xrange(200):
416 if not ffmpeg
.poll() == None:
419 if ffmpeg
.poll() == None:
421 vInfo
['Supported'] = False
422 info_cache
[inFile
] = (mtime
, vInfo
)
426 output
= err_tmp
.read()
428 logging
.debug('ffmpeg output=%s' % output
)
430 rezre
= re
.compile(r
'.*Video: ([^,]+),.*')
431 x
= rezre
.search(output
)
433 vInfo
['codec'] = x
.group(1)
435 vInfo
['Supported'] = False
436 info_cache
[inFile
] = (mtime
, vInfo
)
437 logging
.debug('failed at video codec')
440 rezre
= re
.compile(r
'.*Video: .+, (\d+)x(\d+)[, ].*')
441 x
= rezre
.search(output
)
443 vInfo
['width'] = int(x
.group(1))
444 vInfo
['height'] = int(x
.group(2))
446 vInfo
['Supported'] = False
447 info_cache
[inFile
] = (mtime
, vInfo
)
448 logger
.debug('failed at width/height')
451 rezre
= re
.compile(r
'.*Video: .+, (.+) (?:fps|tb).*')
452 x
= rezre
.search(output
)
454 vInfo
['fps'] = x
.group(1)
456 vInfo
['Supported'] = False
457 info_cache
[inFile
] = (mtime
, vInfo
)
458 logging
.debug('failed at fps')
461 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
462 if (not vInfo
['fps'] == '29.97') and (vInfo
['codec'] == 'mpeg2video'):
463 # First look for the build 7215 version
464 rezre
= re
.compile(r
'.*film source: 29.97.*')
465 x
= rezre
.search(output
.lower() )
467 logger
.debug('film source: 29.97 setting fps to 29.97')
468 vInfo
['fps'] = '29.97'
471 rezre
= re
.compile(r
'.*frame rate differs from container frame rate: 29.97.*')
472 logger
.debug('Bug in VideoReDo')
473 x
= rezre
.search(output
.lower() )
475 vInfo
['fps'] = '29.97'
477 durre
= re
.compile(r
'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
478 d
= durre
.search(output
)
480 vInfo
['millisecs'] = ((int(d
.group(1))*3600) + (int(d
.group(2))*60) + int(d
.group(3)))*1000 + (int(d
.group(4))*100)
482 vInfo
['millisecs'] = 0
484 #get bitrate of source for tivo compatibility test.
485 rezre
= re
.compile(r
'.*bitrate: (.+) (?:kb/s).*')
486 x
= rezre
.search(output
)
488 vInfo
['kbps'] = x
.group(1)
491 logger
.debug('failed at kbps')
493 #get audio bitrate of source for tivo compatibility test.
494 rezre
= re
.compile(r
'.*Audio: .+, (.+) (?:kb/s).*')
495 x
= rezre
.search(output
)
497 vInfo
['akbps'] = x
.group(1)
499 vInfo
['akbps'] = None
500 logger
.debug('failed at akbps')
502 #get audio codec of source for tivo compatibility test.
503 rezre
= re
.compile(r
'.*Audio: ([^,]+),.*')
504 x
= rezre
.search(output
)
506 vInfo
['acodec'] = x
.group(1)
508 vInfo
['acodec'] = None
509 logger
.debug('failed at acodec')
511 #get audio frequency of source for tivo compatibility test.
512 rezre
= re
.compile(r
'.*Audio: .+, (.+) (?:Hz).*')
513 x
= rezre
.search(output
)
515 vInfo
['afreq'] = x
.group(1)
517 vInfo
['afreq'] = None
518 logger
.debug('failed at afreq')
521 rezre
= re
.compile(r
'.*Video: .+PAR ([0-9]+):([0-9]+) DAR [0-9:]+.*')
522 x
= rezre
.search(output
)
523 if x
and x
.group(1)!="0" and x
.group(2)!="0":
524 vInfo
['par1'], vInfo
['par2'] = x
.group(1)+':'+x
.group(2), float(x
.group(1))/float(x
.group(2))
526 vInfo
['par1'], vInfo
['par2'] = None, None
529 rezre
= re
.compile(r
'.*Video: .+DAR ([0-9]+):([0-9]+).*')
530 x
= rezre
.search(output
)
531 if x
and x
.group(1)!="0" and x
.group(2)!="0":
532 vInfo
['dar1'], vInfo
['dar2'] = x
.group(1)+':'+x
.group(2), float(x
.group(1))/float(x
.group(2))
534 vInfo
['dar1'], vInfo
['dar2'] = None, None
536 info_cache
[inFile
] = (mtime
, vInfo
)
537 logger
.debug('Codec=%(codec)s width=%(width)s height=%(height)s fps=%(fps)s millisecs=%(millisecs)s kbps=%(kbps)s akbps=%(akbps)s acodec=%(acodec)s afreq=%(afreq)s par=%(par1)s %(par2)s dar=%(dar1)s %(dar2)s'\
541 def video_check(inFile
, cmd_string
):
542 cmd
= [ffmpeg_path(), '-i', inFile
] + cmd_string
.split()
543 ffmpeg
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
545 shutil
.copyfileobj(ffmpeg
.stdout
, open(videotest
, 'wb'))
551 def supported_format(inFile
):
552 if video_info(inFile
)['Supported']:
555 logger
.debug('FALSE, file not supported %s' % inFile
)
559 logger
.debug('killing pid=%s' % str(pid
))
564 os
.kill(pid
, signal
.SIGTERM
)
568 handle
= ctypes
.windll
.kernel32
.OpenProcess(1, False, pid
)
569 ctypes
.windll
.kernel32
.TerminateProcess(handle
, -1)
570 ctypes
.windll
.kernel32
.CloseHandle(handle
)