Python Video Player using OpenCV and PyQT

Posted by


In this tutorial, we will create a video player using Python and OpenCV with a graphical user interface (GUI) built using PyQT. This video player will allow users to open and play video files on their computer.

Step 1: Install Required Libraries

Before we get started, make sure you have the following libraries installed:

  • OpenCV: This library is used for reading and displaying video files.
  • PyQT: This library is used for creating the graphical user interface.

You can install these libraries using pip:

pip install opencv-python
pip install PyQt5

Step 2: Create the GUI

First, we need to create the graphical user interface for our video player. Create a new Python file and import the necessary libraries:

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QFileDialog, QSlider
from PyQt5.QtGui import QPixmap
import sys

Next, create a class called VideoPlayer that inherits from QWidget. In the init method, we will create the UI elements such as buttons, labels, and sliders:

class VideoPlayer(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Video Player')
        self.setGeometry(100, 100, 800, 600)

        self.video_label = QLabel()
        self.play_button = QPushButton('Play')
        self.stop_button = QPushButton('Stop')

        self.slider = QSlider()
        self.slider.setOrientation(1) # set orientation to vertical

        layout = QVBoxLayout()
        layout.addWidget(self.video_label)
        layout.addWidget(self.play_button)
        layout.addWidget(self.stop_button)
        layout.addWidget(self.slider)

        self.setLayout(layout)

Step 3: Add Functionality to the Buttons

Next, we need to add functionality to the buttons. We will create methods called play_video and stop_video that will start and stop the video playback:

def play_video(self):
    pass

def stop_video(self):
    pass

Inside the play_video method, we will open a video file using the QFileDialog and start playing it using OpenCV:

def play_video(self):
    filename, _ = QFileDialog.getOpenFileName(self, 'Open Video File')
    cap = cv2.VideoCapture(filename)

    while(cap.isOpened()):
        ret, frame = cap.read()

        if ret:
            # Convert frame to RGB
            rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # Convert frame to QImage
            h, w, c = rgb_frame.shape
            qimg = QImage(rgb_frame, w, h, 3 * w, QImage.Format_RGB888)

            # Set the image to the QLabel
            self.video_label.setPixmap(QPixmap.fromImage(qimg))

        else:
            break

        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

Step 4: Add Functionality to the Slider

To allow users to seek through the video, we will add functionality to the slider. We will create a method called seek_video that will update the video playback based on the slider position:

def seek_video(self):
    pass

Inside the seek_video method, we will calculate the desired frame based on the slider position and set the video playback to that frame:

def seek_video(self):
    pass

Step 5: Connect Buttons and Slider to Functions

Finally, we need to connect the buttons and slider to their respective functions. We will connect the play_button and stop_button to the play_video and stop_video methods, and connect the slider to the seek_video method:

self.play_button.clicked.connect(self.play_video)
self.stop_button.clicked.connect(self.stop_video)
self.slider.valueChanged.connect(self.seek_video)

Step 6: Run the Video Player

To run the video player, create an instance of the VideoPlayer class and show the GUI:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = VideoPlayer()
    player.show()
    sys.exit(app.exec_())

Congratulations! You have now created a video player using Python, OpenCV, and PyQT. You can further customize the GUI and add more functionality to the video player, such as adding play/pause buttons, a volume slider, and fullscreen mode. Experiment with different features and have fun exploring the possibilities of video playback with Python!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x