python 录音录屏幕功能实现

import wave
import threading
from os import remove,mkdir,listdir
from os.path import exists,splitext,basename,join
from datetime import datetime
from time import sleep
from shutil import rmtree
import pyaudio
from PIL import ImageGrab
from numpy import array
import cv2
from moviepy.editor import *

CHUNK_sIZE = 1024
CHANNELS = 2
FORMAT = pyaudio.paInt16
RATE = 48000
allowRecording = True

def record_audio():
    p= pyaudio.PyAudio()
    # event.wait()
    sleep(3)
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    input_device_index=4,#立体混音,具体选哪个根据需要选择
                    frames_per_buffer = CHUNK_sIZE)
    wf = wave.open(audio_filename,'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    while allowRecording:
        # 从录音设备读取数据,直接写入wav文件
        data = stream.read(CHUNK_sIZE)
        wf.writeframes(data)
    wf.close()
    stream.stop_stream()
    stream.close()
    p.terminate()

def record_screen():
    # 录制屏幕
    im = ImageGrab.grab()
    video =cv2.VideoWriter(screen_video_filename,
                           cv2.VideoWriter_fourcc(*'XVID'),
                           25,im.size) #帧速和视频宽度、高度
    while allowRecording:
        im = ImageGrab.grab()
        im = cv2.cvtColor(array(im),cv2.COLOR_RGB2BGR)
        video.write(im)
    video.release()



now = str(datetime.now())[:19].replace(':','_')
audio_filename = "%s.mp3"%now
webcam_video_filename = "t%s.avi"%now
screen_video_filename = "tt%s.avi"%now
video_filename = "%s.avi"%now

#创建两个线程,分别录音和录屏
t1 = threading.Thread(target=record_audio)
t2 = threading.Thread(target=record_screen)

event = threading.Event()
event.clear()
for t in (t1,t2):
    t.start()
# 等待摄像头准保好,提示用户三秒钟以后开始录制
# event.wait()
print('3秒后开始录制,按q键结束录制')
while True:
    if input() =='q':
        break
allowRecording = False
for i in (t1,t2):
    t.join()

#把录制的视频和音频合成视频文件
audio = AudioFileClip(audio_filename)
video1 = VideoFileClip(screen_video_filename)
ratio1 = audio.duration / video1.duration
video1 = (video1.fl_time(lambda t: t/ratio1,apply_to=['video'])\
            .set_end(audio.duration))
            
video = CompositeVideoClip([video1]).set_audio(audio)
video.write_videofile(video_filename,codec= 'libx264',fps = 25)

remove(audio_filename)
remove(screen_video_filename)

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Listest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值