Condense video_info
[pyTivo/krkeegan.git] / plugins / video / transcode.py
blob2cab6ed4f31c540e751282f013c2a7cdd4e4479e
1 import subprocess, shutil, os, re, sys, tempfile, ConfigParser, time, lrucache, math
2 import config
3 import logging
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']
12 def ffmpeg_path():
13 return config.get('Server', 'ffmpeg')
15 # XXX BIG HACK
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")
26 if mswindows:
27 patchSubprocess()
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)
34 f.close()
35 else:
36 logger.debug('%s is not tivo compatible' % inFile)
37 transcode(inFile, outFile, tsn)
39 def transcode(inFile, outFile, tsn=''):
41 settings = {}
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)
62 try:
63 shutil.copyfileobj(ffmpeg.stdout, outFile)
64 except:
65 kill(ffmpeg.pid)
67 def select_audiocodec(inFile, tsn = ''):
68 # Default, compatible with all TiVo's
69 codec = 'ac3'
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
79 codec = 'copy'
80 else:
81 codec = config.getAudioCodec(tsn)
82 copyts = ' -copyts'
83 if (codec == 'copy' and config.getCopyTS(tsn).lower() == 'none' \
84 and type == 'mpeg2video') or config.getCopyTS(tsn).lower() == 'false':
85 copyts = ''
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
93 freq = vInfo['afreq']
94 if config.getAudioFR(tsn) != None:
95 freq = config.getAudioFR(tsn)
96 return '-ar '+freq
98 def select_audioch(tsn):
99 if config.getAudioCH(tsn) != None:
100 return '-ac '+config.getAudioCH(tsn)
101 return ''
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:
107 vfps = ' '
108 if config.getVideoFPS(tsn) != None:
109 vfps = '-r '+config.getVideoFPS(tsn)
110 return vfps
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)))
129 return video_str
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)
143 return ''
145 def select_format(tsn):
146 fmt = 'vob'
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)
167 if 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):
186 if par2 == None:
187 npar = config.getPixelAR(1)
188 else:
189 npar = par2
190 # adjust for pixel aspect ratio, if set, because TiVo expects square pixels
191 if npar<1.0:
192 return ['-s', str(width) + 'x' + str(int(math.ceil(height/npar)))]
193 elif npar>1.0:
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
198 return []
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)]
211 else:
212 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
213 else:
214 settings = []
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')
223 else:
224 settings.append('16:9')
225 if endHeight % 2:
226 endHeight -= 1
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)
232 if topPadding % 2:
233 topPadding -= 1
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')
249 else:
250 settings.append('16:9')
251 if endWidth % 2:
252 endWidth -= 1
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)
258 if leftPadding % 2:
259 leftPadding -= 1
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
276 else:
277 settings.append('4:3')
278 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier)
279 if endHeight % 2:
280 endHeight -= 1
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)
286 if topPadding % 2:
287 topPadding -= 1
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))
299 return 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.
302 else:
303 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
304 settings.append('-aspect')
305 settings.append('4:3')
306 if endWidth % 2:
307 endWidth -= 1
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)
313 if leftPadding % 2:
314 leftPadding -= 1
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))
327 return 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)
336 return True
338 if not vInfo['codec'] == 'mpeg2video':
339 #print 'Not Tivo Codec'
340 logger.debug('FALSE, type %s not mpeg2video. %s' % (vInfo['codec'], inFile))
341 return False
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))
345 return False
347 if vInfo['acodec'] == 'dca':
348 logger.debug('FALSE, acodec %s not supported. %s' % (vInfo['acodec'], inFile))
349 return False
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))
354 return False
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))
360 return False
361 else:
362 logger.debug('FALSE, %s kbps not supported. %s' % (vInfo['kbps'], inFile))
363 return False
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))
370 return False
371 logger.debug('TRUE, HD Tivo detected, skipping remaining tests %s' % inFile)
372 return True
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))
377 return False
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])
383 return False
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))
388 return True
389 #print 'Not Tivo dimensions'
390 logger.debug('FALSE, %s x %s not in supported modes. %s' % (vInfo['width'], vInfo['height'], inFile))
391 return False
393 def video_info(inFile):
394 vInfo = dict()
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)
406 return vInfo
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):
415 time.sleep(.05)
416 if not ffmpeg.poll() == None:
417 break
419 if ffmpeg.poll() == None:
420 kill(ffmpeg.pid)
421 vInfo['Supported'] = False
422 info_cache[inFile] = (mtime, vInfo)
423 return vInfo
425 err_tmp.seek(0)
426 output = err_tmp.read()
427 err_tmp.close()
428 logging.debug('ffmpeg output=%s' % output)
430 rezre = re.compile(r'.*Video: ([^,]+),.*')
431 x = rezre.search(output)
432 if x:
433 vInfo['codec'] = x.group(1)
434 else:
435 vInfo['Supported'] = False
436 logger.debug('failed at codec')
438 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
439 x = rezre.search(output)
440 if x:
441 vInfo['width'] = int(x.group(1))
442 vInfo['height'] = int(x.group(2))
443 else:
444 vInfo['Supported'] = False
445 logger.debug('failed at width/height')
447 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
448 x = rezre.search(output)
449 if x:
450 vInfo['fps'] = x.group(1)
451 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
452 if (not vInfo['fps'] == '29.97') and (vInfo['codec'] == 'mpeg2video'):
453 # First look for the build 7215 version
454 rezre = re.compile(r'.*film source: 29.97.*')
455 x = rezre.search(output.lower() )
456 if x:
457 logger.debug('film source: 29.97 setting fps to 29.97')
458 vInfo['fps'] = '29.97'
459 else:
460 # for build 8047:
461 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
462 logger.debug('Bug in VideoReDo')
463 x = rezre.search(output.lower() )
464 if x:
465 vInfo['fps'] = '29.97'
466 else:
467 vInfo['Supported'] = False
468 logger.debug('failed at fps')
470 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
471 d = durre.search(output)
472 if d:
473 vInfo['millisecs'] = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
474 else:
475 vInfo['millisecs'] = 0
477 #get bitrate of source for tivo compatibility test.
478 rezre = re.compile(r'.*bitrate: (.+) (?:kb/s).*')
479 x = rezre.search(output)
480 if x:
481 vInfo['kbps'] = x.group(1)
482 else:
483 vInfo['kbps'] = None
484 logger.debug('failed at kbps')
486 #get audio bitrate of source for tivo compatibility test.
487 rezre = re.compile(r'.*Audio: .+, (.+) (?:kb/s).*')
488 x = rezre.search(output)
489 if x:
490 vInfo['akbps'] = x.group(1)
491 else:
492 vInfo['akbps'] = None
493 logger.debug('failed at akbps')
495 #get audio codec of source for tivo compatibility test.
496 rezre = re.compile(r'.*Audio: ([^,]+),.*')
497 x = rezre.search(output)
498 if x:
499 vInfo['acodec'] = x.group(1)
500 else:
501 vInfo['acodec'] = None
502 logger.debug('failed at acodec')
504 #get audio frequency of source for tivo compatibility test.
505 rezre = re.compile(r'.*Audio: .+, (.+) (?:Hz).*')
506 x = rezre.search(output)
507 if x:
508 vInfo['afreq'] = x.group(1)
509 else:
510 vInfo['afreq'] = None
511 logger.debug('failed at afreq')
513 #get par.
514 rezre = re.compile(r'.*Video: .+PAR ([0-9]+):([0-9]+) DAR [0-9:]+.*')
515 x = rezre.search(output)
516 if x and x.group(1)!="0" and x.group(2)!="0":
517 vInfo['par1'], vInfo['par2'] = x.group(1)+':'+x.group(2), float(x.group(1))/float(x.group(2))
518 else:
519 vInfo['par1'], vInfo['par2'] = None, None
521 #get dar.
522 rezre = re.compile(r'.*Video: .+DAR ([0-9]+):([0-9]+).*')
523 x = rezre.search(output)
524 if x and x.group(1)!="0" and x.group(2)!="0":
525 vInfo['dar1'], vInfo['dar2'] = x.group(1)+':'+x.group(2), float(x.group(1))/float(x.group(2))
526 else:
527 vInfo['dar1'], vInfo['dar2'] = None, None
529 info_cache[inFile] = (mtime, vInfo)
530 logger.debug("; ".join(["%s=%s" % (k, v) for k, v in vInfo.items()]))
531 return vInfo
533 def video_check(inFile, cmd_string):
534 cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
535 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
536 try:
537 shutil.copyfileobj(ffmpeg.stdout, open(videotest, 'wb'))
538 return True
539 except:
540 kill(ffmpeg.pid)
541 return False
543 def supported_format(inFile):
544 if video_info(inFile)['Supported']:
545 return True
546 else:
547 logger.debug('FALSE, file not supported %s' % inFile)
548 return False
550 def kill(pid):
551 logger.debug('killing pid=%s' % str(pid))
552 if mswindows:
553 win32kill(pid)
554 else:
555 import os, signal
556 os.kill(pid, signal.SIGTERM)
558 def win32kill(pid):
559 import ctypes
560 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
561 ctypes.windll.kernel32.TerminateProcess(handle, -1)
562 ctypes.windll.kernel32.CloseHandle(handle)
564 def gcd(a,b):
565 while b:
566 a, b = b, a % b
567 return a