|
import streamlit as st |
|
from pytube import YouTube |
|
|
|
|
|
def set_bg_hack_url(): |
|
''' |
|
A function to unpack an image from url and set as bg. |
|
Returns |
|
------- |
|
The background. |
|
''' |
|
|
|
st.markdown( |
|
f""" |
|
<style> |
|
.stApp {{ |
|
background: url("https://cdn.pixabay.com/photo/2020/06/19/22/33/wormhole-5319067_960_720.jpg"); |
|
background-size: cover |
|
}} |
|
</style> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
class YouTubeDownloader: |
|
@staticmethod |
|
def run(): |
|
st.header("YouTube Video Downloader") |
|
url = st.text_input("Enter YouTube URL to download:") |
|
start_button = st.button("Start Download") |
|
|
|
if start_button: |
|
if url: |
|
YouTubeDownloader.validate_url(url) |
|
with st.expander("preview video"): |
|
st.video(url) |
|
YouTubeDownloader.cleanup() |
|
file_ = YouTubeDownloader.download_video(url) |
|
st.video(file_) |
|
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() |
|
) |
|
st.success("Downloaded") |
|
return local_file |
|
|
|
@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 the 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() |
|
|