eightHundreds
1/30/2018 - 3:15 AM

屏幕截图

import threading, os, time, sys
from PIL import ImageGrab
from moviepy.editor import ImageSequenceClip

fileName ='demo'
if not os.path.isdir(fileName):
    os.mkdir(fileName)
recording, imageList, recordTime,current_record_start_time = True, [], 0,time.time()
def get_pictures():
    global recordTime,current_record_start_time
    while True:
        if not recording:
            time.sleep(1)
            continue
        image = ImageGrab.grab()
        imageName = os.path.join(fileName, '%s.jpg' % int(time.time() * 1e3))
        image.save(imageName)
        imageList.append(imageName)
        recordTime = time.time() - current_record_start_time
        time.sleep(0.1)
t = threading.Thread(target=get_pictures, daemon=True)
t.start()

start_time=time.time()
video_count=0

def run():
    global recording,recordTime,video_count,imageList,current_record_start_time
    while True:
        if int(time.time()-start_time)%5==0 and recordTime!=0:
            recording=False
            print(recordTime)
            clip = ImageSequenceClip(imageList,fps=int(len(imageList) / recordTime))
            clip.write_videofile('%s_%d.mp4' % (fileName,video_count))
            video_count+=1
            recordTime=0
            imageList=[]
            recording=True
            current_record_start_time=time.time()
        time.sleep(0.1)
    
run()