Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Python 3 Tkinter Project to Record Webcam & Save it as MP4 Video File Using OpenCV Library GUI Desktop App

Posted on May 18, 2023

 

 

Welcome folks today in this blog post we will be building a webcam video recorder in tkinter framework and save the video as mp4 file using opencv library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below libraries using the pip command as shown below

 

 

pip install opencv-python

 

 

pip install uuid

 

 

After that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tkinter as tk
from tkinter import filedialog
import cv2
import datetime
import uuid
 
class VideoRecorderGUI:
    def __init__(self, master):
        self.master = master
        master.title("Video Recorder")
 
        self.output_path = ""
 
        # Create the GUI elements
        self.label = tk.Label(master, text="Select destination folder:")
        self.label.pack()
 
        self.destination_button = tk.Button(master, text="Select Folder", command=self.select_destination)
        self.destination_button.pack()
 
        self.record_button = tk.Button(master, text="Record", command=self.start_recording)
        self.record_button.pack()
 
        self.stop_button = tk.Button(master, text="Stop", command=self.stop_recording, state=tk.DISABLED)
        self.stop_button.pack()
 
    def select_destination(self):
        self.output_path = filedialog.askdirectory()
 
    def start_recording(self):
        # Generate a unique filename
        unique_id = str(uuid.uuid4())
        current_datetime = datetime.datetime.now()
        filename = f"{current_datetime.strftime('%Y%m%d%H%M%S')}_{unique_id}.mp4"
 
        # Start recording using OpenCV
        self.cap = cv2.VideoCapture(0)
        self.out = cv2.VideoWriter(self.output_path + "/" + filename, cv2.VideoWriter_fourcc(*"mp4v"), 25, (640, 480))
 
        self.record_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
 
        self.record()
 
    def record(self):
        # Record frames until stop button is pressed
        ret, frame = self.cap.read()
        if ret:
            self.out.write(frame)
            self.master.after(1, self.record)
 
    def stop_recording(self):
        # Stop recording
        self.cap.release()
        self.out.release()
 
        self.record_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)
 
if __name__ == '__main__':
    root = tk.Tk()
    app = VideoRecorderGUI(root)
    root.mainloop()

 

 

As you can see we are importing all the required libraries at the top including opencv. And after that we have written the python class of VideoRecorder in which we have declared all the methods to record the webcam and stop the recording and also select the output directory where all the files will be stored. And lastly we are creating a new object of that class and starting the application.

 

 

 

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme