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