0
点赞
收藏
分享

微信扫一扫

python实现录屏录音小工具


在Windows系统上,可以使用Python的pyautogui和cv2等库来实现屏幕录制功能。同时,也可以使用sounddevice或pyaudio等库来实现音频录制功能。下面是一个使用这些库实现的简单录屏工具示例:

import cv2
import numpy as np
import pyautogui
import sounddevice as sd
import threading

Set up video capture properties

screen_size = (1920, 1080)
fps = 30
fourcc = cv2.VideoWriter_fourcc(*"mp4v")

Set up audio capture properties

sample_rate = 44100
duration = 10
channels = 2

# Set up file names
video_file_name = "recorded_video.mp4"
audio_file_name = "recorded_audio.wav"

def record_video():
    # Create video writer object
    out = cv2.VideoWriter(video_file_name, fourcc, fps, screen_size)

    while True:
        # Capture screenshot
        img = pyautogui.screenshot()

        # Convert screenshot to numpy array and resize it
        frame = np.array(img)
        frame = cv2.resize(frame, screen_size)

        # Write the frame to the video file
        out.write(frame)

        # Check if user wants to stop recording
        if cv2.waitKey(1) == ord("q"):
            break

    # Release the video writer and close all windows
    out.release()
    cv2.destroyAllWindows()

def record_audio():
    # Start recording audio
    recorded_audio = sd.rec(duration * sample_rate, samplerate=sample_rate, channels=channels)

    # Wait for recording to finish
    sd.wait()

    # Save recorded audio to a file
    sd.write(audio_file_name, sample_rate, recorded_audio)

Start recording video and audio in separate threads

video_thread = threading.Thread(target=record_video)
audio_thread = threading.Thread(target=record_audio)

video_thread.start()
audio_thread.start()

Wait for both threads to finish

video_thread.join()
audio_thread.join()

在这个程序中,我们使用pyautogui库来捕获桌面截图,并使用cv2库将截图写入视频文件。我们还使用sounddevice库来录制音频,并将其写入音频文件。为了同时进行屏幕录制和音频录制,我们将它们放在不同的线程中运行。

注意:在运行此代码之前,需要先通过pip安装必要的库(如pyautogui、cv2和sounddevice)。

当程序执行完毕后,将会生成名为“recorded_video.mp4”和“recorded_audio.wav”的两个文件,其中“recorded_video.mp4”是包含录制内容的视频文件,而“recorded_audio.wav”是录制的音频文件。


举报

相关推荐

0 条评论