Python 屏幕和声音录制工具 - 简单美观的 GUI
以下是一个使用 Python 的 tkinter 库创建一个简单的 GUI 来录制屏幕和声音的示例代码:
import tkinter as tk
import pyautogui
import sounddevice as sd
from scipy.io.wavfile import write
import threading
import datetime
class ScreenRecorder:
def __init__(self):
self.is_recording = False
self.frames = []
self.audio_frames = []
def start_recording(self):
self.is_recording = True
self.frames = []
self.audio_frames = []
# 开始录制屏幕
threading.Thread(target=self._record_screen).start()
# 开始录制声音
threading.Thread(target=self._record_audio).start()
def stop_recording(self):
self.is_recording = False
def save_video(self):
if self.frames:
file_name = f'screen_record_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.gif'
pyautogui.mimsave(file_name, self.frames, duration=0.1)
print(f'屏幕录制已保存为 {file_name}')
def save_audio(self):
if self.audio_frames:
file_name = f'audio_record_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.wav'
write(file_name, 44100, self.audio_frames)
print(f'声音录制已保存为 {file_name}')
def _record_screen(self):
while self.is_recording:
self.frames.append(pyautogui.screenshot())
def _record_audio(self):
duration = 10 # 录制声音的持续时间(秒)
sample_rate = 44100
recording = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=2)
sd.wait()
self.audio_frames = recording.tolist()
def start_recording():
recorder.start_recording()
def stop_recording():
recorder.stop_recording()
recorder.save_video()
recorder.save_audio()
root = tk.Tk()
root.title('屏幕和声音录制器')
recorder = ScreenRecorder()
start_button = tk.Button(root, text='开始录制', command=start_recording)
start_button.pack()
stop_button = tk.Button(root, text='停止录制', command=stop_recording)
stop_button.pack()
root.mainloop()
这个示例使用了 pyautogui 库来录制屏幕,sounddevice 库来录制声音。录制的屏幕和声音都将保存为 .gif 和 .wav 文件。你可以根据需要对代码进行调整和修改。
原文地址: https://www.cveoy.top/t/topic/Sxu 著作权归作者所有。请勿转载和采集!