如题。。
因为不想玩了,所以只是半成品,导出图片还需要用PR之类的合成视频!!
[Python] 纯文本查看 复制代码 import time
import os
import threading
import win32gui
import win32con
import win32ui
from PIL import Image
from queue import Queue
import ctypes
class Timer(object):
def __init__(self):
freq = ctypes.c_longlong(0)
ctypes.windll.kernel32.QueryPerformanceFrequency(ctypes.byref(freq))
self.__freq=freq.value
self.__beginCount = self.counter()
def counter(self):
freq = ctypes.c_longlong(0)
ctypes.windll.kernel32.QueryPerformanceCounter(ctypes.byref(freq))
return freq.value
def beginCount(self):
self.__beginCount = self.counter()
#时间差,精确到微秒
def secondsDiff(self):
self.__endCount = self.counter()
return (self.__endCount-self.__beginCount)/(self.__freq+0.)
class Record():
def __init__(self):
self.hwnd = 0
self.get_hwnd()
self.images = Queue(maxsize=0)
self.flag = False
def set_fps(self, fps):
self._fps = fps
def set_length(self, length):
self._length = length
def set_output(self, path):
self.path = path
def fecth_image(self):
hwnd = self.hwnd
rect = win32gui.GetWindowRect(hwnd)
width = rect[2] - rect[0]
height = rect[3] - rect[1]
hwndDC=win32gui.GetWindowDC(hwnd)
mfcDC=win32ui.CreateDCFromHandle(hwndDC)
neicunDC=mfcDC.CreateCompatibleDC()
savebitmap=win32ui.CreateBitmap()
savebitmap.CreateCompatibleBitmap(mfcDC,width,height)
neicunDC.SelectObject(savebitmap)
neicunDC.BitBlt((0,0),(width, height),mfcDC,(0,0),win32con.SRCCOPY)
bmpinfo = savebitmap.GetInfo()
bmpstr = savebitmap.GetBitmapBits(True)
image = Image.frombuffer("RGB", (width, height), bmpstr, 'raw', 'BGRX', 0, 1)
self.images.put(image)
#savebitmap.SaveBitmapFile(neicunDC,filename)
neicunDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd,hwndDC)
win32gui.DeleteObject(savebitmap.GetHandle())
def write_out(self):
i = 1
path = self.path
if not os.path.exists(path):
os.mkdir(path)
while True:
if self.flags and self.images.empty():
break
if not self.images.empty():
image = self.images.get()
filename = os.path.join(path, str(i).zfill(5)+".jpg")
image.save(filename)
i+=1
time.sleep(0.02)
def record(self):
n = int(self._fps * self._length)
wait = 1 / self._fps
self.flags = False
t = threading.Thread(target=self.write_out, args=())
t.start()
t0 = time.time()
for i in range(n):
timer = Timer()
t = threading.Thread(target=self.fecth_image, args=())
t.start()
t.join()
while True:
if int(timer.secondsDiff()*1000000) >= int(wait * 1000000):
break
self.flags = True
print(time.time() - t0)
def get_hwnd(self):
win32gui.EnumWindows(self.e_fun_1, 0)
def e_fun_1(self, hwnd, param):
if win32gui.GetClassName(hwnd) == "WorkerW":
win32gui.EnumChildWindows(hwnd, self.e_fun_2, None)
def e_fun_2(self, hwnd, param):
if win32gui.GetClassName(hwnd) == "KWallpaperPlayerWindow" and win32gui.GetWindowText(hwnd) == "KWallpaper":
self.hwnd = win32gui.GetParent(hwnd)
Record = Record()
Record.set_fps(25) #帧率
Record.set_length(30) #录制时长
Record.set_output("./月亮") #输出文件夹
Record.record()
分享两个自己录的,看下效果....
链接: https://pan.baidu.com/s/1P-LlY0uS8-a-JtOENZ9UZA
提取码: umv6 |