|
import streamlit as st |
|
from pytube import YouTube |
|
|
|
class YouTubeDownloader: |
|
@staticmethod |
|
def run(): |
|
st.header("YouTube Video Downloader") |
|
url = st.text_input("Enter YouTube URL to download:") |
|
if url: |
|
YouTubeDownloader.validate_url(url) |
|
with st.expander("Preview Video"): |
|
st.video(url) |
|
if st.button("Download Video"): |
|
YouTubeDownloader.cleanup() |
|
file_path = YouTubeDownloader.download_video(url) |
|
st.success("Downloaded") |
|
st.download_button( |
|
label="Download Here", |
|
key="download_button", |
|
on_click=YouTubeDownloader.download_button_callback, |
|
args=(file_path,), |
|
) |
|
YouTubeDownloader.helper_message() |
|
st.markdown("YouTube Video Download help") |
|
|
|
@staticmethod |
|
def download_video(url): |
|
with st.spinner("Downloading..."): |
|
local_file = ( |
|
YouTube(url) |
|
.streams.filter(progressive=True, file_extension="mp4") |
|
.first() |
|
.download() |
|
) |
|
return local_file |
|
|
|
@staticmethod |
|
def download_button_callback(file_path): |
|
with open(file_path, "rb") as file: |
|
file_data = file.read() |
|
st.download_button( |
|
label="Click to Download", |
|
data=file_data, |
|
key="file_download", |
|
file_name="downloaded_video.mp4", |
|
mime="video/mp4", |
|
) |
|
|
|
@staticmethod |
|
def validate_url(url): |
|
import validators |
|
|
|
if not validators.url(url): |
|
st.error("Hi there π URL seems invalid π½") |
|
st.stop() |
|
|
|
@classmethod |
|
def cleanup(cls): |
|
import pathlib |
|
import glob |
|
|
|
junks = glob.glob("*.mp4") |
|
for junk in junks: |
|
pathlib.Path(junk).unlink() |
|
|
|
@classmethod |
|
def helper_message(cls): |
|
st.write( |
|
"> To save the video to local computer, " |
|
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
YouTubeDownloader.run() |
|
|