元气桌面动态壁纸录制
如题。。因为不想玩了,所以只是半成品,导出图片还需要用PR之类的合成视频!!
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 - rect
height = rect - rect
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 djjiewufeng 发表于 2022-3-3 16:46
链接挂了 求个鲸落
「SJ03.mkv」https://www.aliyundrive.com/s/mvQrTN1Sr87
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。 这个代码看格式是Python ? 挺好的,感谢楼主分享 谢谢分享,蛮好的 感谢分享 连接没有了可否在分享一下
链接挂了 求个鲸落
页:
[1]