How to display a video using streamlit

In this recipe, we will learn how to display a video using Streamlit. We will also take a look at a simple Streamlit application consisting of a video.

Recipe Objective: How to display a video in streamlit?

"st.video()" is used to display video in Streamlit.

  Syntax: st.video(data, format='video/mp4', start_time=0)
  Parameters:
   data -> It could be raw video data, filename, or a URL to the file to load. Must be opened using io.open(). Streamlit also includes support for YouTube URLs.
   format -> It is the The mime type for the video file. If nothing is specified it defaults to 'video/mp4'.
   start_time -> The time entered as integer from which this element should start playing.

Note: Since the MP4V codec (which is an export option in OpenCV) is not generally supported by browsers, some videos may not display. Your video will be able to be displayed in Streamlit after being converted to H.264.

Code:

#importing streamlit library
import streamlit as st

#displaying a local video file
video_file = open('FILENAME', 'rb') #enter the filename with filepath
video_bytes = video_file.read() #reading the file
st.video(video_bytes) #displaying the video

#displaying a video by simply passing a Youtube link
st.video("https://youtu.be/yVV_t_Tewvs")

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.