Video Player using Python and Streamlit

Introduction:

This project demonstrates how to create a simple video player web application using Python and Streamlit. Embed and play video files directly within your Streamlit apps.

Project Prerequisites:

  • Python: Ensure Python is installed on your system. Download from the official website: Python Official Website
  • Streamlit: Install the Streamlit library:
pip install streamlit
  • Video File: Place a video file (e.g., MP4) in a folder named video in the same directory as your Python script.

Getting Started:
Step 1: Set Up Your Video File
Create a folder named video in the same directory where you'll save your Python script. Place your video file (e.g., sample_video.mp4) inside this folder.

Step 2: Create a New Python Script
Open your text editor or IDE and create a new Python file (e.g., video_player.py).

Step 3: Write the Code
Copy and paste the following code into your script:

import streamlit as st
st.title("Video Player")
# Video file path (replace with your own video file)
video_file = open("./video/sample_video.mp4", "rb")
video_bytes = video_file.read()
# Display the video
st.video(video_bytes)

Step 4: Run the Application
Save the script. In your terminal, navigate to where you saved the file and run:

streamlit run video_player.py

Step 5: Use the Video Player
The video player app will open in your browser. The video from your video folder will be embedded and ready to play.

Guide:

  • st.title(): Sets the title of your app.
  • video_file = open(...): Opens the video file in binary read mode ("rb").
  • video_bytes = video_file.read(): Reads the video file data into a bytes object.
  • st.video(video_bytes): Embeds and plays the video in the Streamlit app.

Conclusion:
This video player app provides a basic but functional way to embed videos in your Streamlit applications. You can customize it further by:

  • Supporting different video formats: Streamlit supports various video formats (MP4, MOV, AVI, etc.). Experiment with different formats to see which ones work best.
  • Adding video controls: While Streamlit provides basic controls (play, pause, volume), you can explore more advanced controls using libraries like OpenCV.
  • Creating a video playlist: Allow users to select and play multiple videos.
  • Building a video streaming app: Explore libraries and techniques for streaming video content from URLs or other sources.

Advertisement