欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

opencv-python视频处理之录制视频

程序员文章站 2022-03-07 20:49:02
cv2.VideoWriter 分别传入的参数是路径 格式,帧率,视频尺寸...

录制一段视频保存

import cv2
# 创建一个视频窗口
cv2.namedWindow('Video')
# cv2.VideoCapture(0)打开摄像头
video_capture = cv2.VideoCapture(0)
video_writer = cv2.VideoWriter('test.avi',cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),video_capture.get(cv2.CAP_PROP_FPS),(int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))))
# cv2.VideoWriter 分别传入的参数是路径 格式,帧率,视频尺寸
success,frame = video_capture.read()
# 成功打开摄像头 直到按esc退出保存视频
while success and not cv2.waitKey(1) == 27:
    blur_frame = cv2.GaussianBlur(frame[:,::-1], (3,3),0)
    video_writer.write(blur_frame)
    cv2.imshow("Video", blur_frame)
    success, frame = video_capture.read()
cv2.destroyWindow('Video')
video_capture.release()

if __name__ == '__main__':
    pass

下面是录制带有声音的视频

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')
————————————————
版权声明:本文为CSDN博主「luke-skyworker」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaoyun_zzz/java/article/details/84341801

本文地址:https://blog.csdn.net/weixin_32759777/article/details/107295877