couple more vInfo corrections
[pyTivo/krkeegan.git] / plugins / video / transcode.py
blob10af301bcd21fd42debe39404651144e899ea2b7
1 import subprocess, shutil, os, re, sys, tempfile, ConfigParser, time, lrucache, math
2 import config
3 import logging
4 from plugin import GetPlugin
6 logger = logging.getLogger('pyTivo.video.transcode')
8 info_cache = lrucache.LRUCache(1000)
9 videotest = os.path.join(os.path.dirname(__file__), 'videotest.mpg')
11 BAD_MPEG_FPS = ['15.00']
13 def ffmpeg_path():
14 return config.get('Server', 'ffmpeg')
16 # XXX BIG HACK
17 # subprocess is broken for me on windows so super hack
18 def patchSubprocess():
19 o = subprocess.Popen._make_inheritable
21 def _make_inheritable(self, handle):
22 if not handle: return subprocess.GetCurrentProcess()
23 return o(self, handle)
25 subprocess.Popen._make_inheritable = _make_inheritable
26 mswindows = (sys.platform == "win32")
27 if mswindows:
28 patchSubprocess()
30 def output_video(inFile, outFile, tsn=''):
31 if tivo_compatable(inFile, tsn):
32 logger.debug('%s is tivo compatible' % inFile)
33 f = file(inFile, 'rb')
34 shutil.copyfileobj(f, outFile)
35 f.close()
36 else:
37 logger.debug('%s is not tivo compatible' % inFile)
38 transcode(inFile, outFile, tsn)
40 def transcode(inFile, outFile, tsn=''):
42 settings = {}
43 settings['video_codec'] = select_videocodec(tsn)
44 settings['video_br'] = select_videobr(inFile, tsn)
45 settings['video_fps'] = select_videofps(inFile, tsn)
46 settings['max_video_br'] = select_maxvideobr()
47 settings['buff_size'] = select_buffsize()
48 settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn))
49 settings['audio_br'] = select_audiobr(tsn)
50 settings['audio_fr'] = select_audiofr(inFile, tsn)
51 settings['audio_ch'] = select_audioch(tsn)
52 settings['audio_codec'] = select_audiocodec(inFile, tsn)
53 settings['ffmpeg_pram'] = select_ffmpegprams(tsn)
54 settings['format'] = select_format(tsn)
56 cmd_string = config.getFFmpegTemplate(tsn) % settings
58 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
59 logging.debug('transcoding to tivo model '+tsn[:3]+' using ffmpeg command:')
60 logging.debug(' '.join(cmd))
61 ffmpeg = subprocess.Popen(cmd, bufsize=512*1024, stdout=subprocess.PIPE)
63 try:
64 shutil.copyfileobj(ffmpeg.stdout, outFile)
65 except:
66 kill(ffmpeg.pid)
68 def select_audiocodec(inFile, tsn = ''):
69 # Default, compatible with all TiVo's
70 codec = 'ac3'
71 vInfo = video_info(inFile)
72 codectype = vInfo['codec']
73 if config.getAudioCodec(tsn) == None:
74 if vInfo['acodec'] in ('ac3', 'liba52', 'mp2'):
75 if vInfo['akbps'] == None:
76 cmd_string = '-y -vcodec mpeg2video -r 29.97 -b 1000k -acodec copy -t 00:00:01 -f vob -'
77 if video_check(inFile, cmd_string):
78 vInfo = video_info(videotest)
79 if not vInfo['akbps'] == None and int(vInfo['akbps']) <= config.getMaxAudioBR(tsn):
80 # compatible codec and bitrate, do not reencode audio
81 codec = 'copy'
82 else:
83 codec = config.getAudioCodec(tsn)
84 copyts = ' -copyts'
85 if (codec == 'copy' and config.getCopyTS(tsn).lower() == 'none' \
86 and codectype == 'mpeg2video') or config.getCopyTS(tsn).lower() == 'false':
87 copyts = ''
88 return '-acodec '+codec+copyts
90 def select_audiofr(inFile, tsn):
91 freq = '48000' #default
92 vInfo = video_info(inFile)
93 if not vInfo['afreq'] == None and vInfo['afreq'] in ('44100', '48000'):
94 # compatible frequency
95 freq = vInfo['afreq']
96 if config.getAudioFR(tsn) != None:
97 freq = config.getAudioFR(tsn)
98 return '-ar '+freq
100 def select_audioch(tsn):
101 if config.getAudioCH(tsn) != None:
102 return '-ac '+config.getAudioCH(tsn)
103 return ''
105 def select_videofps(inFile, tsn):
106 vInfo = video_info(inFile)
107 vfps = '-r 29.97' #default
108 if config.isHDtivo(tsn) and vInfo['fps'] not in BAD_MPEG_FPS:
109 vfps = ' '
110 if config.getVideoFPS(tsn) != None:
111 vfps = '-r '+config.getVideoFPS(tsn)
112 return vfps
114 def select_videocodec(tsn):
115 vcodec = 'mpeg2video' #default
116 if config.getVideoCodec(tsn) != None:
117 vcodec = config.getVideoCodec(tsn)
118 return '-vcodec '+vcodec
120 def select_videobr(inFile, tsn):
121 return '-b '+select_videostr(inFile, tsn)
123 def select_videostr(inFile, tsn):
124 video_str = config.getVideoBR(tsn)
125 if config.isHDtivo(tsn):
126 vInfo = video_info(inFile)
127 if vInfo['kbps'] != None and config.getVideoPCT() > 0:
128 video_percent = int(vInfo['kbps'])*10*config.getVideoPCT()
129 video_bitrate = max(config.strtod(video_str), video_percent)
130 video_str = str(int(min(config.strtod(config.getMaxVideoBR())*0.95, video_bitrate)))
131 return video_str
133 def select_audiobr(tsn):
134 return '-ab '+config.getAudioBR(tsn)
136 def select_maxvideobr():
137 return '-maxrate '+config.getMaxVideoBR()
139 def select_buffsize():
140 return '-bufsize '+config.getBuffSize()
142 def select_ffmpegprams(tsn):
143 if config.getFFmpegPrams(tsn) != None:
144 return config.getFFmpegPrams(tsn)
145 return ''
147 def select_format(tsn):
148 fmt = 'vob'
149 if config.getFormat(tsn) != None:
150 fmt = config.getFormat(tsn)
151 return '-f '+fmt+' -'
153 def select_aspect(inFile, tsn = ''):
154 TIVO_WIDTH = config.getTivoWidth(tsn)
155 TIVO_HEIGHT = config.getTivoHeight(tsn)
157 vInfo = video_info(inFile)
159 logging.debug('tsn: %s' % tsn)
161 aspect169 = config.get169Setting(tsn)
163 logging.debug('aspect169:%s' % aspect169)
165 optres = config.getOptres(tsn)
167 logging.debug('optres:%s' % optres)
169 if optres:
170 optHeight = config.nearestTivoHeight(vInfo['height'])
171 optWidth = config.nearestTivoWidth(vInfo['width'])
172 if optHeight < TIVO_HEIGHT:
173 TIVO_HEIGHT = optHeight
174 if optWidth < TIVO_WIDTH:
175 TIVO_WIDTH = optWidth
177 d = gcd(vInfo['height'],vInfo['width'])
178 ratio = (vInfo['width']*100)/vInfo['height']
179 rheight, rwidth = vInfo['height']/d, vInfo['width']/d
181 logger.debug('File=%s codec=%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))
183 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH)
184 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH)
186 if config.isHDtivo(tsn) and not optres:
187 if config.getPixelAR(0):
188 if vInfo['par2'] == None:
189 npar = config.getPixelAR(1)
190 else:
191 npar = vInfo['par2']
192 # adjust for pixel aspect ratio, if set, because TiVo expects square pixels
193 if npar<1.0:
194 return ['-s', str(vInfo['width']) + 'x' + str(int(math.ceil(vInfo['height']/npar)))]
195 elif npar>1.0:
196 # FFMPEG expects width to be a multiple of two
197 return ['-s', str(int(math.ceil(vInfo['width']*npar/2.0)*2)) + 'x' + str(vInfo['height'])]
198 if vInfo['height'] <= TIVO_HEIGHT:
199 # pass all resolutions to S3, except heights greater than conf height
200 return []
201 # else, resize video.
202 if (rwidth, rheight) in [(1, 1)] and par1 == '8:9':
203 logger.debug('File + PAR is within 4:3.')
204 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
205 elif (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)] or vInfo['dar1'] == '4:3':
206 logger.debug('File is within 4:3 list.')
207 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
208 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)] or vInfo['dar1'] == '16:9')\
209 and (aspect169 or config.get169Letterbox(tsn)):
210 logger.debug('File is within 16:9 list and 16:9 allowed.')
211 if config.get169Blacklist(tsn) or (aspect169 and config.get169Letterbox(tsn)):
212 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
213 else:
214 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
215 else:
216 settings = []
217 #If video is wider than 4:3 add top and bottom padding
218 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
219 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
220 if (ratio > 177):#too short needs padding top and bottom
221 endHeight = int(((TIVO_WIDTH*vInfo['height'])/vInfo['width']) * multiplier16by9)
222 settings.append('-aspect')
223 if config.get169Blacklist(tsn) or config.get169Letterbox(tsn):
224 settings.append('4:3')
225 else:
226 settings.append('16:9')
227 if endHeight % 2:
228 endHeight -= 1
229 if endHeight < TIVO_HEIGHT * 0.99:
230 settings.append('-s')
231 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
233 topPadding = ((TIVO_HEIGHT - endHeight)/2)
234 if topPadding % 2:
235 topPadding -= 1
237 settings.append('-padtop')
238 settings.append(str(topPadding))
239 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
240 settings.append('-padbottom')
241 settings.append(str(bottomPadding))
242 else: #if only very small amount of padding needed, then just stretch it
243 settings.append('-s')
244 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
245 logger.debug('16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n%s' % ' '.join(settings))
246 else: #too skinny needs padding on left and right.
247 endWidth = int((TIVO_HEIGHT*vInfo['width'])/(vInfo['height']*multiplier16by9))
248 settings.append('-aspect')
249 if config.get169Blacklist(tsn) or config.get169Letterbox(tsn):
250 settings.append('4:3')
251 else:
252 settings.append('16:9')
253 if endWidth % 2:
254 endWidth -= 1
255 if endWidth < (TIVO_WIDTH-10):
256 settings.append('-s')
257 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
259 leftPadding = ((TIVO_WIDTH - endWidth)/2)
260 if leftPadding % 2:
261 leftPadding -= 1
263 settings.append('-padleft')
264 settings.append(str(leftPadding))
265 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
266 settings.append('-padright')
267 settings.append(str(rightPadding))
268 else: #if only very small amount of padding needed, then just stretch it
269 settings.append('-s')
270 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
271 logger.debug('16:9 aspect allowed, file is narrower than 16:9 padding left and right\n%s' % ' '.join(settings))
272 else: #this is a 4:3 file or 16:9 output not allowed
273 multiplier = multiplier4by3
274 settings.append('-aspect')
275 if ratio > 135 and config.get169Letterbox(tsn):
276 settings.append('16:9')
277 multiplier = multiplier16by9
278 else:
279 settings.append('4:3')
280 endHeight = int(((TIVO_WIDTH*vInfo['height'])/vInfo['width']) * multiplier)
281 if endHeight % 2:
282 endHeight -= 1
283 if endHeight < TIVO_HEIGHT * 0.99:
284 settings.append('-s')
285 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
287 topPadding = ((TIVO_HEIGHT - endHeight)/2)
288 if topPadding % 2:
289 topPadding -= 1
291 settings.append('-padtop')
292 settings.append(str(topPadding))
293 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
294 settings.append('-padbottom')
295 settings.append(str(bottomPadding))
296 else: #if only very small amount of padding needed, then just stretch it
297 settings.append('-s')
298 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
299 logging.debug('File is wider than 4:3 padding top and bottom\n%s' % ' '.join(settings))
301 return settings
302 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
303 #an aspect ratio of 4:3 since they are so narrow.
304 else:
305 endWidth = int((TIVO_HEIGHT*vInfo['width'])/(vInfo['height']*multiplier4by3))
306 settings.append('-aspect')
307 settings.append('4:3')
308 if endWidth % 2:
309 endWidth -= 1
310 if endWidth < (TIVO_WIDTH * 0.99):
311 settings.append('-s')
312 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
314 leftPadding = ((TIVO_WIDTH - endWidth)/2)
315 if leftPadding % 2:
316 leftPadding -= 1
318 settings.append('-padleft')
319 settings.append(str(leftPadding))
320 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
321 settings.append('-padright')
322 settings.append(str(rightPadding))
323 else: #if only very small amount of padding needed, then just stretch it
324 settings.append('-s')
325 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
327 logger.debug('File is taller than 4:3 padding left and right\n%s' % ' '.join(settings))
329 return settings
331 def tivo_compatable(inFile, tsn = ''):
332 supportedModes = [[720, 480], [704, 480], [544, 480], [528, 480], [480, 480], [352, 480]]
333 vInfo = video_info(inFile)
335 if (inFile[-5:]).lower() == '.tivo':
336 logger.debug('TRUE, ends with .tivo. %s' % inFile)
337 return True
339 if not vInfo['codec'] == 'mpeg2video':
340 #print 'Not Tivo Codec'
341 logger.debug('FALSE, codec %s not mpeg2video. %s' % (vInfo['codec'], inFile))
342 return False
344 if os.path.splitext(inFile)[-1].lower() in ('.ts', '.mpv', '.tp'):
345 logger.debug('FALSE, ext %s not tivo compatible. %s' % (os.path.splitext(inFile)[-1], inFile))
346 return False
348 if vInfo['acodec'] == 'dca':
349 logger.debug('FALSE, acodec %s not supported. %s' % (vInfo['acodec'], inFile))
350 return False
352 if vInfo['acodec'] != None:
353 if not vInfo['akbps'] or int(vInfo['akbps']) > config.getMaxAudioBR(tsn):
354 logger.debug('FALSE, %s kbps exceeds max audio bitrate. %s' % (vInfo['akbps'], inFile))
355 return False
357 if vInfo['kbps'] != None:
358 abit = max('0', vInfo['akbps'])
359 if int(vInfo['kbps'])-int(abit) > config.strtod(config.getMaxVideoBR())/1000:
360 logger.debug('FALSE, %s kbps exceeds max video bitrate. %s' % (vInfo['kbps'], inFile))
361 return False
362 else:
363 logger.debug('FALSE, %s kbps not supported. %s' % (vInfo['kbps'], inFile))
364 return False
366 if config.isHDtivo(tsn):
367 if vInfo['par2'] != 1.0:
368 if config.getPixelAR(0):
369 if vInfo['par2'] != None or config.getPixelAR(1) != 1.0:
370 logger.debug('FALSE, %s not correct PAR, %s' % (vInfo['par2'], inFile))
371 return False
372 logger.debug('TRUE, HD Tivo detected, skipping remaining tests %s' % inFile)
373 return True
375 if not vInfo['fps'] == '29.97':
376 #print 'Not Tivo fps'
377 logger.debug('FALSE, %s fps, should be 29.97. %s' % (vInfo['fps'], inFile))
378 return False
380 if (config.get169Blacklist(tsn) and not config.get169Setting(tsn))\
381 or (config.get169Letterbox(tsn) and config.get169Setting(tsn)):
382 if vInfo['dar1'] == None or not vInfo['dar1'] in ('4:3', '8:9'):
383 debug_write(__name__, fn_attr(), ['FALSE, DAR', vInfo['dar1'], 'not supported by BLACKLIST_169 tivos.', inFile])
384 return False
386 for mode in supportedModes:
387 if (mode[0], mode[1]) == (vInfo['width'], vInfo['height']):
388 logger.debug('TRUE, %s x %s is valid. %s' % (vInfo['width'], vInfo['height'], inFile))
389 return True
390 #print 'Not Tivo dimensions'
391 logger.debug('FALSE, %s x %s not in supported modes. %s' % (vInfo['width'], vInfo['height'], inFile))
392 return False
394 def video_info(inFile):
395 vInfo = dict()
396 mtime = os.stat(inFile).st_mtime
397 if inFile != videotest:
398 if inFile in info_cache and info_cache[inFile][0] == mtime:
399 logging.debug('CACHE HIT! %s' % inFile)
400 return info_cache[inFile][1]
402 vInfo['Supported'] = True
404 if (inFile[-5:]).lower() == '.tivo':
405 vInfo['millisecs'] = 0
406 info_cache[inFile] = (mtime, vInfo)
407 logger.debug('VALID, ends in .tivo. %s' % inFile)
408 return vInfo
410 cmd = [ffmpeg_path(), '-i', inFile ]
411 # Windows and other OS buffer 4096 and ffmpeg can output more than that.
412 err_tmp = tempfile.TemporaryFile()
413 ffmpeg = subprocess.Popen(cmd, stderr=err_tmp, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
415 # wait 10 sec if ffmpeg is not back give up
416 for i in xrange(200):
417 time.sleep(.05)
418 if not ffmpeg.poll() == None:
419 break
421 if ffmpeg.poll() == None:
422 kill(ffmpeg.pid)
423 vInfo['Supported'] = False
424 info_cache[inFile] = (mtime, vInfo)
425 return vInfo
427 err_tmp.seek(0)
428 output = err_tmp.read()
429 err_tmp.close()
430 logging.debug('ffmpeg output=%s' % output)
432 rezre = re.compile(r'.*Video: ([^,]+),.*')
433 x = rezre.search(output)
434 if x:
435 vInfo['codec'] = x.group(1)
436 else:
437 vInfo['codec'] = ''
438 vInfo['Supported'] = False
439 logger.debug('failed at codec')
441 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
442 x = rezre.search(output)
443 if x:
444 vInfo['width'] = int(x.group(1))
445 vInfo['height'] = int(x.group(2))
446 else:
447 vInfo['width'] = ''
448 vInfo['height'] = ''
449 vInfo['Supported'] = False
450 logger.debug('failed at width/height')
452 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
453 x = rezre.search(output)
454 if x:
455 vInfo['fps'] = x.group(1)
456 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
457 if (not vInfo['fps'] == '29.97') and (vInfo['codec'] == 'mpeg2video'):
458 # First look for the build 7215 version
459 rezre = re.compile(r'.*film source: 29.97.*')
460 x = rezre.search(output.lower() )
461 if x:
462 logger.debug('film source: 29.97 setting fps to 29.97')
463 vInfo['fps'] = '29.97'
464 else:
465 # for build 8047:
466 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
467 logger.debug('Bug in VideoReDo')
468 x = rezre.search(output.lower() )
469 if x:
470 vInfo['fps'] = '29.97'
471 else:
472 vInfo['fps'] = ''
473 vInfo['Supported'] = False
474 logger.debug('failed at fps')
476 durre = re.compile(r'.*Duration: ([0-9]+):([0-9]+):([0-9]+)\.([0-9]+),')
477 d = durre.search(output)
478 if d:
479 vInfo['millisecs'] = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
480 else:
481 vInfo['millisecs'] = 0
483 #get bitrate of source for tivo compatibility test.
484 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
485 x = rezre.search(output)
486 if x:
487 vInfo['kbps'] = x.group(1)
488 else:
489 vInfo['kbps'] = None
490 logger.debug('failed at kbps')
492 #get audio bitrate of source for tivo compatibility test.
493 rezre = re.compile(r'.*Audio: .+, (.+) (?:kb/s).*')
494 x = rezre.search(output)
495 if x:
496 vInfo['akbps'] = x.group(1)
497 else:
498 vInfo['akbps'] = None
499 logger.debug('failed at akbps')
501 #get audio codec of source for tivo compatibility test.
502 rezre = re.compile(r'.*Audio: ([^,]+),.*')
503 x = rezre.search(output)
504 if x:
505 vInfo['acodec'] = x.group(1)
506 else:
507 vInfo['acodec'] = None
508 logger.debug('failed at acodec')
510 #get audio frequency of source for tivo compatibility test.
511 rezre = re.compile(r'.*Audio: .+, (.+) (?:Hz).*')
512 x = rezre.search(output)
513 if x:
514 vInfo['afreq'] = x.group(1)
515 else:
516 vInfo['afreq'] = None
517 logger.debug('failed at afreq')
519 #get par.
520 rezre = re.compile(r'.*Video: .+PAR ([0-9]+):([0-9]+) DAR [0-9:]+.*')
521 x = rezre.search(output)
522 if x and x.group(1)!="0" and x.group(2)!="0":
523 vInfo['par1'], vInfo['par2'] = x.group(1)+':'+x.group(2), float(x.group(1))/float(x.group(2))
524 else:
525 vInfo['par1'], vInfo['par2'] = None, None
527 #get dar.
528 rezre = re.compile(r'.*Video: .+DAR ([0-9]+):([0-9]+).*')
529 x = rezre.search(output)
530 if x and x.group(1)!="0" and x.group(2)!="0":
531 vInfo['dar1'], vInfo['dar2'] = x.group(1)+':'+x.group(2), float(x.group(1))/float(x.group(2))
532 else:
533 vInfo['dar1'], vInfo['dar2'] = None, None
535 videoPlugin = GetPlugin('video')
536 metadata = videoPlugin.getMetadataFromTxt(inFile)
538 for key in metadata:
539 if key.startswith('Override_'):
540 vInfo['Supported'] = True
541 vInfo[key.replace('Override_','')] = metadata[key]
543 info_cache[inFile] = (mtime, vInfo)
544 logger.debug("; ".join(["%s=%s" % (k, v) for k, v in vInfo.items()]))
545 return vInfo
547 def video_check(inFile, cmd_string):
548 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
549 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
550 try:
551 shutil.copyfileobj(ffmpeg.stdout, open(videotest, 'wb'))
552 return True
553 except:
554 kill(ffmpeg.pid)
555 return False
557 def supported_format(inFile):
558 if video_info(inFile)['Supported']:
559 return True
560 else:
561 logger.debug('FALSE, file not supported %s' % inFile)
562 return False
564 def kill(pid):
565 logger.debug('killing pid=%s' % str(pid))
566 if mswindows:
567 win32kill(pid)
568 else:
569 import os, signal
570 os.kill(pid, signal.SIGTERM)
572 def win32kill(pid):
573 import ctypes
574 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
575 ctypes.windll.kernel32.TerminateProcess(handle, -1)
576 ctypes.windll.kernel32.CloseHandle(handle)
578 def gcd(a,b):
579 while b:
580 a, b = b, a % b
581 return a