Creating a YouTube video downloader can be a fun and educational project to enhance your Python programming skills. In this blog, I’ll walk you through building a simple yet effective YouTube video downloader using Python. We will use the pytube
library to handle video downloading and streamlit
for creating a web interface. Let’s dive in!
Prerequisites
Before we begin, make sure you have Python installed on your system. Additionally, you need to install the required libraries. You can do this using pip:
pip install pytube streamlit
Setting Up the Project
First, create a new Python file, say youtube_downloader.py
, and import the necessary libraries:
import streamlit as st
from pytube import YouTube
import os
Defining the Download Function
Next, define a function to handle the video downloading process. This function will take the URL of the YouTube video and the path where the video should be saved.
def download_video(url, output_path='.'):
try:
# Creating YouTube object
yt = YouTube(url)
# Display video details
st.write(f"*Title:* {yt.title}")
st.write(f"*Views:* {yt.views}")
st.write(f"*Duration:* {yt.length} seconds")
# Get the highest resolution stream
ys = yt.streams.get_highest_resolution()
# Print the resolution of the stream being downloaded
st.write(f"*Downloading:* {ys.resolution}")
# Download the video
ys.download(output_path)
st.success("Download completed!")
except Exception as e:
st.error(f"An error occurred: {e}")
Creating the Streamlit App
Streamlit allows you to create an interactive web interface with minimal code. In this section, we will create the user interface for our downloader.
if __name__ == "__main__":
st.title("YouTube Video Downloader")
# User input for the URL
video_url = st.text_input("Enter the URL of the YouTube video:")
# User input for the save path
save_path = st.text_input("Enter the path where you want to save the video (leave blank for current directory):", '.')
# Download button
if st.button("Download"):
if video_url:
download_video(video_url, save_path)
else:
st.error("Please enter a valid YouTube URL.")
Running the App
To run your Streamlit app, open a terminal in the directory where your youtube_downloader.py
file is located and run the following command:
streamlit run youtube_downloader.py
This will start a local web server and open the Streamlit app in your default web browser. You should see a simple interface where you can enter the URL of the YouTube video and specify the path where you want to save the downloaded video.
Testing the Downloader
Enter a valid YouTube URL in the input box and click the “Download” button. The app will display the video details and download the video to the specified location. If any errors occur, they will be displayed on the screen.
Complete Code
Here’s the complete code for reference:
import streamlit as st
from pytube import YouTube
import os
def download_video(url, output_path='.'):
try:
# Creating YouTube object
yt = YouTube(url)
# Display video details
st.write(f"*Title:* {yt.title}")
st.write(f"*Views:* {yt.views}")
st.write(f"*Duration:* {yt.length} seconds")
# Get the highest resolution stream
ys = yt.streams.get_highest_resolution()
# Print the resolution of the stream being downloaded
st.write(f"*Downloading:* {ys.resolution}")
# Download the video
ys.download(output_path)
st.success("Download completed!")
except Exception as e:
st.error(f"An error occurred: {e}")
if __name__ == "__main__":
st.title("YouTube Video Downloader")
# User input for the URL
video_url = st.text_input("Enter the URL of the YouTube video:")
# User input for the save path
save_path = st.text_input("Enter the path where you want to save the video (leave blank for current directory):", '.')
# Download button
if st.button("Download"):
if video_url:
download_video(video_url, save_path)
else:
st.error("Please enter a valid YouTube URL.")
This code provides a basic yet functional YouTube video downloader. You can use it as a foundation for more complex projects or simply as a useful tool for personal use.
Conclusion:
Congratulations! You’ve just created a YouTube video downloader using Python and Streamlit. This project demonstrates the power of Python for web development and automation tasks. You can further enhance this downloader by adding more features such as selecting video quality, downloading audio only, or creating a more sophisticated user interface.
If you have any queries related to this article, then you can ask in the comment section, we will contact you soon, and Thank you for reading this article.
Follow me to receive more useful content:
Instagram | Twitter | Linkedin | Youtube
Thank you