πŸš€ 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 cv2
import numpy as np
import pyautogui
import pyaudio
import wave
import threading
import tkinter as tk
from tkinter import filedialog

# Set screen size
screen_width, screen_height = pyautogui.size()
frame_rate = 20

# Audio recording settings
audio_format = pyaudio.paInt16
channels = 2
rate = 44100
chunk = 1024
audio_filename = "audio.wav"
video_filename = "screen_record.avi"

# Audio recording function
def 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 file
    with 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 function
def record_screen():
    global recording
    recording = True

    fourcc = 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 function
def stop_recording():
    global recording
    recording = False

# GUI Design
def 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 GUI
start_gui()


Comments

Popular posts from this blog

πŸš€ Simple Login & Registration System in Python Tkinter πŸ“±

Python IP Tracker App with GUI | Track IP Location Real-Time! (Working Project)