使用PYaudio錄制音頻和視頻(自己)


參考:https://blog.csdn.net/zhaoyun_zzz/article/details/84341801

音頻錄制:簡潔版

import pyaudio
import wave
import time
import sys

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5000
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()

音頻與視頻結婚:(一定記住此中的MOVIEPY==1.0.0)

 

import wave
# import PyAudio as pyaudio
import pyaudio

# 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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM