Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
|
| 4 |
+
class YouTubeDownloader:
|
| 5 |
+
@staticmethod
|
| 6 |
+
def run():
|
| 7 |
+
st.header("Video View and Download")
|
| 8 |
+
url = st.text_input("YouTube Video URL:")
|
| 9 |
+
if url:
|
| 10 |
+
YouTubeDownloader.validate_url(url)
|
| 11 |
+
with st.expander("View"):
|
| 12 |
+
st.video(url)
|
| 13 |
+
if st.button("Download"):
|
| 14 |
+
YouTubeDownloader.cleanup()
|
| 15 |
+
file_ = YouTubeDownloader.download_video(url)
|
| 16 |
+
st.video(file_)
|
| 17 |
+
YouTubeDownloader.helper_message()
|
| 18 |
+
|
| 19 |
+
@staticmethod
|
| 20 |
+
def download_video(url):
|
| 21 |
+
with st.spinner("Downloading..."):
|
| 22 |
+
local_file = (
|
| 23 |
+
YouTube(url)
|
| 24 |
+
.streams.filter(progressive=True, file_extension="mp4")
|
| 25 |
+
.first()
|
| 26 |
+
.download()
|
| 27 |
+
)
|
| 28 |
+
st.success("Downloaded")
|
| 29 |
+
return local_file
|
| 30 |
+
|
| 31 |
+
@staticmethod
|
| 32 |
+
def validate_url(url):
|
| 33 |
+
import validators
|
| 34 |
+
if not validators.url(url):
|
| 35 |
+
st.error("Video URL invalid")
|
| 36 |
+
st.stop()
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def cleanup(cls):
|
| 40 |
+
import pathlib
|
| 41 |
+
import glob
|
| 42 |
+
|
| 43 |
+
junks = glob.glob("*.mp4")
|
| 44 |
+
for junk in junks:
|
| 45 |
+
pathlib.Path(junk).unlink()
|
| 46 |
+
|
| 47 |
+
@classmethod
|
| 48 |
+
def helper_message(cls):
|
| 49 |
+
st.write("To save click elipses ... and choose download")
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
YouTubeDownloader.run()
|