本想使用進程池,同步兩個一起記錄合並,結果截屏這個函數裡延時有問題,總不能等待後執行。
將視頻幀數調整後雖然可以拉近時間,但是合並後的視頻與音頻不同步,無法正常使用。
import uuid,socket,time
import pyautogui
import cv2
import numpy as np
import pyttsx3 #語音播放
import sounddevice as sd
from scipy.io.wavfile import write
from multiprocessing import Pool #進程池
import os
#音頻文件合成
def ffmpeg_hb(file1,file2,result):
# 將audio1.mp4 與 video.mp4合並 輸出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’
#調用系統錄音系統生成文件
def get_wav_write(file_name,seconds):
fs = 16000 # Sample rate
seconds = 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’
#獲取電腦設備號
def get_mac():
#mac
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# 獲取主機名
hostname = socket.gethostname()
# 獲取IP
ip = socket.gethostbyname(hostname)
# 結果數據導出
pc_mac = {‘mac’:mac,‘hostname’:hostname,‘ip’:ip}
return pc_mac
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) #語音速度
engine.say(text) #朗讀內容
engine.runAndWait()
def get_win_gui(mac,time):
print(‘get_win_gui1’)
# 錄屏准備
fourcc = cv2.VideoWriter_fourcc(‘X’, ‘V’, ‘I’, ‘D’)
fps = 16
output = cv2.VideoWriter(mac +‘.avi’, fourcc, fps, (1366, 768))
frame_num = time * fps
# 循環執行
while True and frame_num > 0:
print(‘frame_num’,frame_num)
in_time = get_current_time()
# print(‘in_time’,in_time)
# 分別代表:左上角坐標,寬高
img = pyautogui.screenshot(region=[0, 0, 1366, 768])
# 對獲取的圖片轉換成二維矩陣形式,後再將RGB轉成BGR
# 因為imshow,默認通道順序是BGR,而pyautogui默認是RGB所以要轉換一下,不然會有點問題
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('倒數開始截屏')
for i in range(3):
pyttsx_yybf('%d 秒' % (3-i))
time.sleep(0.5)
pyttsx_yybf('截屏開始')
# 獲取系統數據
mac_id = get_mac()
# 展現數據
print('計算機參數展示', mac_id)
# 獲取內容
mac, hostname, ip = mac_id['mac'], mac_id['hostname'], mac_id['ip']
time_num = 10
# 錄屏准備
p.apply_async(get_win_gui, args=(mac, time_num))
# 錄音准備
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('截屏結束')
try:
os.remove('jieguo.avi')
except:
pass
time.sleep(0.5)
ffmpeg_hb(mac + '.avi', mac + '.wav', 'jieguo.avi')
pyttsx_yybf('轉換結束')
os.startfile('jieguo.avi')