使用背景:需要对一款线上课程进行自动化实时录制,而现有的Python开源代码会出现如 视频声音不同步、动画快放等各种各样的问题
设计目标:利用Python中的opencv和pyaudio库,实现屏幕的实时录制和声音合成脚本。
设计思路:为避免声音画面不同步或快放等问题,强制设置每秒的读取帧和写入帧,并每捕获一帧便丢入线程队列中。
同时pyaudio对扬声器的声音进行采集,存储为.audio文件。
通过input输入任意键,便可以停止录制并开始视频声音合成
可能存在的问题:对机器的性能有较高的要求,低配机器可能会出现视频模糊,闪光等情况。
直接上代码:
import cv2, _thread, pyaudio,wave,queue,time
from PIL import ImageGrab
import numpy as np
from moviepy.editor import *
class Recorder():
# TODO 屏幕录制+系统扬声器声音
fristImage = ImageGrab.grab() # 获得当前屏幕
def __init__(self, chunk=1024, channels=2, rate=44100):
self._running = True
self.queue = queue.Queue()
"""视频相关"""
self.width = self.fristImage.size[0]
self.height = self.fristImage.size[1]
self.frame = 13 # 读取帧和录制帧,必须一致,否则会导致播放速度不一致
self.aviFileName = "test.avi"
"""声音相关"""
self.CHUNK = chunk
self.FORMAT = pyaudio.paInt16
self.CHANNELS = channels
self.RATE = rate
self._frames = []
self.audioFileName = "audio.wav"
self.audio = pyaudio.PyAudio()
self.audio_size = self.audio.get_sample_size(self.FORMAT)
"""合成后视频相关"""
self.mergeFileName = "1-1.avi"
def Produce(self):
startTime = time.time()
sum = 0
while self._running:
if time.time() -startTime >= 1:
startTime = time.time()
sum = 0
if sum < self.frame:
img_rgb = ImageGrab.grab()
img_bgr = cv2.cvtColor(np.array(img_rgb), cv2.COLOR_RGB2BGR) # 转为opencv的BGR格式
cv2.imshow('capturing', np.zeros((1, 255), np.uint8))
cv2.moveWindow('capturing', self.height - 100, self.width - 100)
self.queue.put(img_bgr)
sum += 1
cv2.destroyAllWindows()
def Consume(self):
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 编码格式
video = cv2.VideoWriter(self.aviFileName, fourcc, self.frame, (self.width, self.height))
while self._running:
img_bgr = self.queue.get()
video.write(img_bgr)
video.release()
def Audio(self):
# TODO 音频录制
# 获取内录设备序号,在windows操作系统上测试通过,hostAPI = 0 表明是MME设备
def findInternalRecordingDevice(p):
# 要找查的设备名称中的关键字
target = '立体声混音'
for i in range(p.get_device_count()):
devInfo = p.get_device_info_by_index(i)
if devInfo['name'].find(target) >= 0 and devInfo['hostApi'] == 0:
print('已找到系统内录设备,序号是 ', i)
return i
return -1
dev_idx = findInternalRecordingDevice(self.audio)
if dev_idx < 0:
return
# 在打开输入流时指定输入设备
stream = self.audio.open(input_device_index=dev_idx,format=self.FORMAT,channels=self.CHANNELS,
rate=self.RATE,input=True,frames_per_buffer=self.CHUNK)
# 循环读取输入流
while self._running:
data = stream.read(self.CHUNK)
self._frames.append(data)
# 停止读取输入流
stream.stop_stream()
# 关闭输入流
stream.close()
self.save(self.audioFileName)
self.audio.terminate()
def stop(self):
self._running = False
# 保存到文件
def save(self, fileName):
# 打开用于保存数据的文件
wf = wave.open(fileName, 'wb')
# 设置音频参数
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(self.audio_size)
wf.setframerate(self.RATE)
# 写入数据
wf.writeframes(b''.join(self._frames))
wf.close()
def Merge(self):
audioclip = AudioFileClip(self.audioFileName)
videoclip = VideoFileClip(self.aviFileName)
videoclip2 = videoclip.set_audio(audioclip)
video = CompositeVideoClip([videoclip2])
video.write_videofile(self.mergeFileName, codec='mpeg4')
if __name__ == '__main__':
recorder = Recorder()
_thread.start_new_thread(recorder.Produce, ()) # TODO 获取画面
_thread.start_new_thread(recorder.Consume, ()) # TODO 存储画面
_thread.start_new_thread(recorder.Audio, ()) # TODO 录制声音
input("请输入任意键开始视频合成:")
recorder.stop()
recorder.Merge()