Matias Macias Gomez commited on
Commit
ba99ed5
·
1 Parent(s): 8cd0c04

Managed Memory

Browse files
Files changed (1) hide show
  1. streamlit_app.py +38 -28
streamlit_app.py CHANGED
@@ -5,50 +5,60 @@ import string
5
  import streamlit as st
6
 
7
 
8
- # Callbacks
9
  if "videos" not in st.session_state:
10
  st.session_state["videos"] = ""
11
 
12
  if "downloaded" not in st.session_state:
13
  st.session_state["downloaded"] = []
14
 
15
- def download_video(url:str, save_path:str , download:bool, **kwargs):
 
 
 
 
 
 
 
 
 
 
16
  video_id =url.split("/")[-1]
17
  ydl_opts = {
18
  'outtmpl': f'{save_path}/{video_id}.%(ext)s',
19
  }
20
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
21
- info_dict = ydl.extract_info(url, download=download)
22
- return video_id
23
 
 
 
 
 
24
 
25
- st.title("Download Any Video")
 
 
 
 
26
 
27
- st.text_input("Video URL", key="url", placeholder="Enter Video URL and Hit Enter")
 
 
 
28
 
29
 
30
  if st.session_state["url"]:
31
- download = False if st.session_state["url"] in st.session_state["downloaded"] else True
32
- video_path = download_video(st.session_state["url"], save_path=os.path.join(".", "data"), download=download)
33
- st.session_state["videos"] = video_path
34
- if not download:
35
- st.session_state["downloaded"].append(st.session_state["url"])
36
 
37
  if st.session_state["videos"]:
38
-
39
- videos = glob(os.path.join(".", "data", "*.mp4")) \
40
- + glob(os.path.join(".", "data", "*.webm")) \
41
- + glob(os.path.join(".", "data", "*.mov")) \
42
- + glob(os.path.join(".", "data", "*.m4a"))
43
- print("videos found:", videos)
44
- for video in videos:
45
- if st.session_state["videos"] in video:
46
- st.write("**Title**:", st.session_state["videos"])
47
- st.video(video)
48
- video_file = open(video, "rb")
49
- st.download_button("**Download Video 📁**",
50
- data=video_file,
51
- file_name=f"{st.session_state['videos'].lower().translate(str.maketrans('','', string.punctuation)).replace(' ', '_')}.mp4",
52
- mime="video/mp4",
53
- type="primary")
54
- break
 
5
  import streamlit as st
6
 
7
 
8
+ # Config
9
  if "videos" not in st.session_state:
10
  st.session_state["videos"] = ""
11
 
12
  if "downloaded" not in st.session_state:
13
  st.session_state["downloaded"] = []
14
 
15
+
16
+ # Callbacks
17
+ def _remove_video():
18
+ if st.session_state["videos"]:
19
+ try:
20
+ os.remove(st.session_state["videos"])
21
+ st.session_state["videos"] = ""
22
+ except FileNotFoundError:
23
+ print("Couldn't delete file")
24
+
25
+ def download_video(url:str, save_path:str , **kwargs):
26
  video_id =url.split("/")[-1]
27
  ydl_opts = {
28
  'outtmpl': f'{save_path}/{video_id}.%(ext)s',
29
  }
30
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
+ ydl.download(url)
 
32
 
33
+ videos = glob(os.path.join(".", "data", "*.mp4")) \
34
+ + glob(os.path.join(".", "data", "*.webm")) \
35
+ + glob(os.path.join(".", "data", "*.mov")) \
36
+ + glob(os.path.join(".", "data", "*.m4a"))
37
 
38
+ print("videos found:", videos)
39
+ for video in videos:
40
+ if video_id in video:
41
+ video_path = video
42
+ return video_path
43
 
44
+
45
+ st.title("Download Any* Video")
46
+
47
+ st.text_input("Video URL", key="url", placeholder="Enter Video URL and Hit Enter", on_change=_remove_video)
48
 
49
 
50
  if st.session_state["url"]:
51
+ download = st.button("Download")
52
+ if download:
53
+ video_path = download_video(st.session_state["url"], save_path=os.path.join(".", "data"))
54
+ st.session_state["videos"] = video_path
 
55
 
56
  if st.session_state["videos"]:
57
+ st.write("**Title**:", st.session_state["videos"])
58
+ st.video(st.session_state["videos"])
59
+ video_file = open(st.session_state["videos"], "rb")
60
+ st.download_button("**Download Video 📁**",
61
+ data=video_file,
62
+ file_name=f"{st.session_state['videos'].lower().translate(str.maketrans('','', string.punctuation)).replace(' ', '_')}.mp4",
63
+ mime="video/mp4",
64
+ type="primary")