程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python practice_ Practice with screen capture and recording_ draw a blank

編輯:Python

I wanted to use the process pool , Synchronize the merging of two records together , As a result, there is a delay problem in the screenshot function , You can't wait to execute .
Adjusting the number of video frames can shorten the time , But the merged video and audio are not synchronized , Not working properly .

import uuid,socket,time
import pyautogui
import cv2
import numpy as np
import pyttsx3 # Voice playback
import sounddevice as sd
from scipy.io.wavfile import write
from multiprocessing import Pool # The process of pool
import os

# Audio file synthesis
def ffmpeg_hb(file1,file2,result):
# take audio1.mp4 And video.mp4 Merge Output output.mp4
#ffmpeg.exe -i audio1.mp4 -i video.mp4 -acodec copy -vcodec copy output.mp4
os.system(f"ffmpeg.exe -i {file1} -i {file2} -acodec copy -vcodec copy {result}")
return ‘ffmpeg_ok’

# Call the system recording system to generate files
def get_wav_write(file_name,seconds):
fs = 16000 # Sample rate
seconds = seconds # Duration seconds
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
sd.wait()
write(file_name + ‘.wav’, fs, myrecording) # Save as WAV file
return file_name + ‘.wav’

# Get the computer device number
def get_mac():
#mac
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# Get the hostname
hostname = socket.gethostname()
# obtain IP
ip = socket.gethostbyname(hostname)
# Result data export
pc_mac = {‘mac’:mac,‘hostname’:hostname,‘ip’:ip}
return pc_mac

Time calculation

def get_current_time():
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime(“%Y%m%d%H%M%S”, local_time)
data_secs = abs(ct - round(ct)) * 1000
time_stamp = “%s%03d” % (data_head, data_secs)
return time_stamp

def pyttsx_yybf(text):
engine = pyttsx3.init()
engine.setProperty(‘rate’, 160) # Speech speed
engine.say(text) # Reading content
engine.runAndWait()

def get_win_gui(mac,time):
print(‘get_win_gui1’)
# Screen recording preparation
fourcc = cv2.VideoWriter_fourcc(‘X’, ‘V’, ‘I’, ‘D’)
fps = 16
output = cv2.VideoWriter(mac +‘.avi’, fourcc, fps, (1366, 768))
frame_num = time * fps
# Loop execution
while True and frame_num > 0:
print(‘frame_num’,frame_num)
in_time = get_current_time()
# print(‘in_time’,in_time)
# Represent the : Top left coordinates , Wide and high
img = pyautogui.screenshot(region=[0, 0, 1366, 768])
# Convert the acquired image into two-dimensional matrix form , And then I will RGB Turn into BGR
# because imshow, The default channel order is BGR, and pyautogui The default is RGB So we need to switch , Otherwise there will be some problems
img_cv = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
output.write(img_cv)
frame_num -= 1
#time.sleep(0.0003)
print(‘get_win_gui2’)
return mac +‘.avi’

if name == ‘main’:

p = Pool(3)
pyttsx_yybf(' Countdown starts screenshot ')
for i in range(3):
pyttsx_yybf('%d second ' % (3-i))
time.sleep(0.5)
pyttsx_yybf(' Screenshot start ')
# Get system data
mac_id = get_mac()
# Display data
print(' Computer parameter display ', mac_id)
# Get content
mac, hostname, ip = mac_id['mac'], mac_id['hostname'], mac_id['ip']
time_num = 10
# Screen recording preparation
p.apply_async(get_win_gui, args=(mac, time_num))
# Recording preparation
p.apply_async(get_wav_write, args=(mac, time_num))
p.apply_async(time.sleep, args=(time_num + 0,))
p.close()
p.join()
pyttsx_yybf(' The screenshot ends ')
try:
os.remove('jieguo.avi')
except:
pass
time.sleep(0.5)
ffmpeg_hb(mac + '.avi', mac + '.wav', 'jieguo.avi')
pyttsx_yybf(' End of conversion ')
os.startfile('jieguo.avi')

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved