File size: 1,851 Bytes
ec036c3
 
 
 
 
 
 
d0738fa
5be6373
 
cf771a4
a893d2d
 
d0738fa
 
a893d2d
ec036c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from pytube import YouTube

class YouTubeDownloader:
    @staticmethod
    def run():
        st.header("Video View and Download")
        
        option = st.selectbox('How would you like to be contacted for the download list?',('Email', 'Mobile phone'))
        st.write('You selected:', option)
        option2 = st.selectbox('Would you try one of these one minute AI generated science fiction short videos?',('https://youtu.be/v-qlGRxdm6c', 'https://youtu.be/5yToL7ymfNo'))

        url = st.text_input("YouTube Video URL:")
        if option2:
            url=option2
                  
        if url:
            YouTubeDownloader.validate_url(url)
            with st.expander("View"):
                st.video(url)
            if st.button("Download"):
                YouTubeDownloader.cleanup()
                file_ = YouTubeDownloader.download_video(url)
                st.video(file_)
                YouTubeDownloader.helper_message()

    @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("Video URL 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 click elipses ... and choose download")

if __name__ == "__main__":
    YouTubeDownloader.run()