Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
|
| 4 |
+
hide_streamlit_style = """
|
| 5 |
+
<style>
|
| 6 |
+
#MainMenu {visibility: hidden;}
|
| 7 |
+
footer {visibility: hidden;}
|
| 8 |
+
.stApp{
|
| 9 |
+
background-image: linear-gradient(115deg,#FFAF00,#FFC500,#FFD600,#FCED00,#F9F380,#F6F7CD);
|
| 10 |
+
animation: rotate 7s linear infinite;
|
| 11 |
+
}
|
| 12 |
+
@keyframes rotate {
|
| 13 |
+
100%{
|
| 14 |
+
filter: hue-rotate(-360deg)
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
</style>
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# JavaScript code for confetti animation
|
| 21 |
+
confetti_animation_script = """
|
| 22 |
+
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.2/dist/confetti.browser.min.js"></script>
|
| 23 |
+
<script>
|
| 24 |
+
function triggerConfetti() {
|
| 25 |
+
confetti({
|
| 26 |
+
angle: randomInRange(55, 125),
|
| 27 |
+
particleCount: randomInRange(50, 100),
|
| 28 |
+
origin: { y: 0.6 },
|
| 29 |
+
spread: randomInRange(50, 70),
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
function randomInRange(min, max) {
|
| 34 |
+
return Math.random() * (max - min) + min;
|
| 35 |
+
}
|
| 36 |
+
</script>
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
class YouTubeDownloader:
|
| 42 |
+
@staticmethod
|
| 43 |
+
def run():
|
| 44 |
+
st.header("")
|
| 45 |
+
url = st.text_input("Enter Here")
|
| 46 |
+
start_button = st.button("Start")
|
| 47 |
+
|
| 48 |
+
if start_button:
|
| 49 |
+
if url:
|
| 50 |
+
YouTubeDownloader.validate_url(url)
|
| 51 |
+
with st.expander("preview video"):
|
| 52 |
+
st.video(url)
|
| 53 |
+
YouTubeDownloader.cleanup()
|
| 54 |
+
file_ = YouTubeDownloader.download_video(url)
|
| 55 |
+
st.video(file_)
|
| 56 |
+
YouTubeDownloader.helper_message()
|
| 57 |
+
st.markdown(confetti_animation_script, unsafe_allow_html=True) # Include confetti script
|
| 58 |
+
|
| 59 |
+
st.markdown("")
|
| 60 |
+
|
| 61 |
+
@staticmethod
|
| 62 |
+
def download_video(url):
|
| 63 |
+
with st.spinner("Downloading..."):
|
| 64 |
+
local_file = (
|
| 65 |
+
YouTube(url)
|
| 66 |
+
.streams.filter(progressive=True, file_extension="mp4")
|
| 67 |
+
.first()
|
| 68 |
+
.download()
|
| 69 |
+
)
|
| 70 |
+
st.success("Downloaded")
|
| 71 |
+
return local_file
|
| 72 |
+
|
| 73 |
+
@staticmethod
|
| 74 |
+
def validate_url(url):
|
| 75 |
+
import validators
|
| 76 |
+
|
| 77 |
+
if not validators.url(url):
|
| 78 |
+
st.error("Hi there ð URL seems invalid ð½")
|
| 79 |
+
st.stop()
|
| 80 |
+
|
| 81 |
+
@classmethod
|
| 82 |
+
def cleanup(cls):
|
| 83 |
+
import pathlib
|
| 84 |
+
import glob
|
| 85 |
+
|
| 86 |
+
junks = glob.glob("*.mp4")
|
| 87 |
+
for junk in junks:
|
| 88 |
+
pathlib.Path(junk).unlink()
|
| 89 |
+
|
| 90 |
+
@classmethod
|
| 91 |
+
def helper_message(cls):
|
| 92 |
+
st.write(
|
| 93 |
+
"> To save to the local computer, "
|
| 94 |
+
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
YouTubeDownloader.run()
|