您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Python+opencv+pyaudio如何實現帶聲音屏幕錄制,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
基于個人的愛好和現實的需求,決定用Python做一個屏幕錄制的腳本。因為要看一些加密的視頻,每次都要登錄,特別麻煩,遂決定用自己寫的腳本,將加密視頻的播放過程全程錄制下來,這樣以后看自己的錄播就好了。結合近期自己學習的內容,正好用Python來練練手,鞏固自己的學習效果。
經過多番搜索,決定采用Python+opencv+pyaudio來實現屏幕錄制。網上搜索到的錄屏,基本都是不帶聲音的,而我要實現的是帶聲音的屏幕錄制。下面就開始一步一步的實現吧。
聲音錄制
import pyaudio import wave import sys CHUNK = 1024 if len(sys.argv) < 2: print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0]) sys.exit(-1) wf = wave.open(sys.argv[1], 'rb') p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) data = wf.readframes(CHUNK) while data != '': stream.write(data) data = wf.readframes(CHUNK) stream.stop_stream() stream.close() p.terminate()
簡潔回調函數版音頻錄制
import pyaudio import wave import time import sys CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = 10 WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) time_count = 0 def callback(in_data, frame_count, time_info, status): wf.writeframes(in_data) if(time_count < 10): return (in_data, pyaudio.paContinue) else: return (in_data, pyaudio.paComplete) stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), input=True, stream_callback=callback) stream.start_stream() print("* recording") while stream.is_active(): time.sleep(1) time_count += 1 stream.stop_stream() stream.close() wf.close() p.terminate() print("* recording done!")
視頻錄制(無聲音)
from PIL import ImageGrab import numpy as np import cv2 image = ImageGrab.grab()#獲得當前屏幕 width = image.size[0] height = image.size[1] print("width:", width, "height:", height) print("image mode:",image.mode) k=np.zeros((width,height),np.uint8) fourcc = cv2.VideoWriter_fourcc(*'XVID')#編碼格式 video = cv2.VideoWriter('test.avi', fourcc, 25, (width, height)) #輸出文件命名為test.mp4,幀率為16,可以自己設置 while True: img_rgb = ImageGrab.grab() img_bgr=cv2.cvtColor(np.array(img_rgb), cv2.COLOR_RGB2BGR)#轉為opencv的BGR格式 video.write(img_bgr) cv2.imshow('imm', img_bgr) if cv2.waitKey(1) & 0xFF == ord('q'): break video.release() cv2.destroyAllWindows()
錄制的音頻與視頻合成為帶聲音的視頻
錄制200幀,帶音頻的MP4視頻,單線程
import wave from pyaudio import PyAudio,paInt16 from PIL import ImageGrab import numpy as np import cv2 from moviepy.editor import * from moviepy.audio.fx import all import time CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) audio_record_flag = True def callback(in_data, frame_count, time_info, status): wf.writeframes(in_data) if audio_record_flag: return (in_data, pyaudio.paContinue) else: return (in_data, pyaudio.paComplete) stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), input=True, stream_callback=callback) image = ImageGrab.grab()#獲得當前屏幕 width = image.size[0] height = image.size[1] print("width:", width, "height:", height) print("image mode:",image.mode) k=np.zeros((width,height),np.uint8) fourcc = cv2.VideoWriter_fourcc(*'XVID')#編碼格式 video = cv2.VideoWriter('test.mp4', fourcc, 9.5, (width, height)) #經實際測試,單線程下最高幀率為10幀/秒,且會變動,因此選擇9.5幀/秒 #若設置幀率與實際幀率不一致,會導致視頻時間與音頻時間不一致 print("video recording!!!!!") stream.start_stream() print("audio recording!!!!!") record_count = 0 while True: img_rgb = ImageGrab.grab() img_bgr=cv2.cvtColor(np.array(img_rgb), cv2.COLOR_RGB2BGR)#轉為opencv的BGR格式 video.write(img_bgr) record_count += 1 if(record_count > 200): break print(record_count, time.time()) audio_record_flag = False while stream.is_active(): time.sleep(1) stream.stop_stream() stream.close() wf.close() p.terminate() print("audio recording done!!!!!") video.release() cv2.destroyAllWindows() print("video recording done!!!!!") print("video audio merge!!!!!") audioclip = AudioFileClip("output.wav") videoclip = VideoFileClip("test.mp4") videoclip2 = videoclip.set_audio(audioclip) video = CompositeVideoClip([videoclip2]) video.write_videofile("test2.mp4",codec='mpeg4')
看來要提高幀率必須使用隊列加多線程了,這一步等到以后來添加吧。不過總是覺得用OpenCV來實現視頻錄制,有點怪異,畢竟opencv是用來做圖像與視頻分析的,還是走正道認真搗鼓opencv該做的事情吧。
關于“Python+opencv+pyaudio如何實現帶聲音屏幕錄制”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。