|
import streamlit as st |
|
from pytube import YouTube |
|
from pytube.exceptions import VideoUnavailable |
|
|
|
class YouTubeDownloader: |
|
@staticmethod |
|
def run(): |
|
st.header("μ νλΈ(Youtube) μμ URLλ‘ λ€μ΄λ‘λ") |
|
url = st.text_input("Enter YouTube URL to download:") |
|
if url: |
|
YouTubeDownloader.validate_url(url) |
|
try: |
|
with st.expander("preview video"): |
|
st.video(url) |
|
if st.button("Download"): |
|
YouTubeDownloader.cleanup() |
|
file_ = YouTubeDownloader.download_video(url) |
|
if file_: |
|
st.video(file_) |
|
YouTubeDownloader.helper_message() |
|
except Exception as e: |
|
st.error(f"Error: {str(e)}") |
|
st.markdown("μ) https://www.youtube.com/watch?v=hXpNj1ChxRI ") |
|
|
|
@staticmethod |
|
def download_video(url): |
|
try: |
|
with st.spinner("Downloading..."): |
|
yt = YouTube(url) |
|
stream = yt.streams.filter(progressive=True, file_extension="mp4").first() |
|
if stream: |
|
local_file = stream.download() |
|
st.success("Downloaded") |
|
return local_file |
|
else: |
|
st.error("No suitable stream found for this video") |
|
return None |
|
except VideoUnavailable: |
|
st.error("This video is unavailable") |
|
return None |
|
except Exception as e: |
|
st.error(f"An error occurred: {str(e)}") |
|
return None |
|
|
|
@staticmethod |
|
def validate_url(url): |
|
import validators |
|
|
|
if not validators.url(url): |
|
st.error("URLμ΄ μ μμ μ΄μ§ μμ΅λλ€.") |
|
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( |
|
"μμ νλ©΄ μ°μΈ‘ νλ¨ ... λ²νΌμ ν΄λ¦νλ©΄ λ€μ΄λ‘λ ν μ μμ΅λλ€." |
|
) |
|
|
|
if __name__ == "__main__": |
|
YouTubeDownloader.run() |