π Create a Python Screen Recorder with Audio (Complete Code)
Demo :
Click Video πππ
π Folder Structure
π Python-Screen-Recorder
├── π screen_recorder.py (Main Python Script)
├── π΅ audio.wav (Recorded Audio)
├── π₯ screen_record.avi (Recorded Video)
Dependencies Install Karo:
cmd => pip install opencv-python numpy pyautogui pyaudio
Code :
import cv2import numpy as npimport pyautoguiimport pyaudioimport waveimport threadingimport tkinter as tkfrom tkinter import filedialog# Set screen sizescreen_width, screen_height = pyautogui.size()frame_rate = 20# Audio recording settingsaudio_format = pyaudio.paInt16channels = 2rate = 44100chunk = 1024audio_filename = "audio.wav"video_filename = "screen_record.avi"# Audio recording functiondef record_audio():audio = pyaudio.PyAudio()stream = audio.open(format=audio_format, channels=channels, rate=rate, input=True, frames_per_buffer=chunk)frames = []while recording:data = stream.read(chunk)frames.append(data)stream.stop_stream()stream.close()audio.terminate()# Save audio filewith wave.open(audio_filename, 'wb') as wf:wf.setnchannels(channels)wf.setsampwidth(audio.get_sample_size(audio_format))wf.setframerate(rate)wf.writeframes(b''.join(frames))# Video recording functiondef record_screen():global recordingrecording = Truefourcc = cv2.VideoWriter_fourcc(*"XVID")out = cv2.VideoWriter(video_filename, fourcc, frame_rate, (screen_width, screen_height))audio_thread = threading.Thread(target=record_audio)audio_thread.start()while recording:img = pyautogui.screenshot()frame = np.array(img)frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)out.write(frame)out.release()# Stop recording functiondef stop_recording():global recordingrecording = False# GUI Designdef start_gui():root = tk.Tk()root.title("Screen Recorder")root.geometry("300x200")start_btn = tk.Button(root, text="Start Recording", command=lambda: threading.Thread(target=record_screen).start(), fg="white", bg="green")start_btn.pack(pady=10)stop_btn = tk.Button(root, text="Stop Recording", command=stop_recording, fg="white", bg="red")stop_btn.pack(pady=10)root.mainloop()# Start GUIstart_gui()
Comments
Post a Comment