Matias Macias Gomez commited on
Commit
630dd9d
ยท
1 Parent(s): dfbc210

chore: Add ffmpeg-python to requirements.txt and update video cleanup logic

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. streamlit_app.py +65 -26
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  streamlit==1.27.2
2
- yt-dlp==2024.4.9
 
 
1
  streamlit==1.27.2
2
+ yt-dlp==2024.4.9
3
+ ffmpeg-python==0.2.0
streamlit_app.py CHANGED
@@ -1,7 +1,9 @@
1
  import os
2
- from glob import glob
3
  import yt_dlp
4
  import string
 
 
5
  import streamlit as st
6
 
7
  # Config
@@ -23,10 +25,13 @@ def _remove_video():
23
 
24
 
25
  def _remove_all_videos():
26
- videos = glob(os.path.join(".", "data", "*.mp4")) \
27
- + glob(os.path.join(".", "data", "*.webm")) \
28
- + glob(os.path.join(".", "data", "*.mov")) \
29
- + glob(os.path.join(".", "data", "*.m4a"))
 
 
 
30
  for video in videos:
31
  try:
32
  os.remove(video)
@@ -34,7 +39,7 @@ def _remove_all_videos():
34
  print("Couldn't delete file:", video)
35
 
36
 
37
- # Main function
38
  def download_video(url: str, save_path: str, **kwargs):
39
  video_id = url.split("/")[-1]
40
 
@@ -42,26 +47,37 @@ def download_video(url: str, save_path: str, **kwargs):
42
  video_id = url.split("/")[-2]
43
 
44
  ydl_opts = {
45
- 'outtmpl': f'{save_path}/{video_id}.%(ext)s',
46
  }
47
 
48
  if st.session_state["ig_cookie"]:
49
- ydl_opts['cookies'] = st.session_state["ig_cookie"]
50
 
51
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
52
  ydl.download(url)
53
 
54
- videos = glob(os.path.join(".", "data", "*.mp4")) \
55
- + glob(os.path.join(".", "data", "*.webm")) \
56
- + glob(os.path.join(".", "data", "*.mov")) \
57
- + glob(os.path.join(".", "data", "*.m4a"))
58
 
59
  print("videos found:", videos)
60
  for video in videos:
61
  if video_id in video:
62
  video_path = video
63
- return video_path
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
 
65
 
66
  st.title("Download Any* Video")
67
 
@@ -69,20 +85,41 @@ st.title("Download Any* Video")
69
  with st.sidebar:
70
  st.subheader("Cookies! :cookie:")
71
  st.write(
72
- "For some websites that have a more strict bot control it's necessary to upload a cookie file to download content from them")
73
- st.file_uploader(label="**Upload Instagram Cookie** :cookie:", type=["txt", "json"],
74
- accept_multiple_files=False, key="ig_cookie")
75
- st.file_uploader(label="**Upload TikTok Cookie** :cookie:", type=["txt", "json"],
76
- accept_multiple_files=False, key="tt_cookie")
77
- st.button("Clear Videos", on_click=_remove_all_videos, type="secondary", help="Clear all downloaded videos")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Main content
80
- st.text_input("Video URL", key="url", placeholder="Enter Video URL and Hit Enter", on_change=_remove_video)
 
 
 
 
 
81
 
82
  if st.session_state["url"]:
83
  download = st.button("Load Video")
84
  if download:
85
- video_path = download_video(st.session_state["url"], save_path=os.path.join(".", "data"))
 
 
86
  st.session_state["videos"] = video_path
87
 
88
  if st.session_state["videos"]:
@@ -95,10 +132,12 @@ if st.session_state["videos"]:
95
 
96
  try:
97
  video_file = open(st.session_state["videos"], "rb")
98
- st.download_button("**Download Video ๐Ÿ“**",
99
- data=video_file,
100
- file_name=f"{st.session_state['videos'].lower().translate(str.maketrans('', '', string.punctuation)).replace(' ', '_')}.mp4",
101
- mime="video/mp4",
102
- type="primary")
 
 
103
  except Exception as e:
104
  st.error("Failed downloading the video", icon="๐Ÿ˜”")
 
1
  import os
2
+ import uuid
3
  import yt_dlp
4
  import string
5
+ import ffmpeg
6
+ from glob import glob
7
  import streamlit as st
8
 
9
  # Config
 
25
 
26
 
27
  def _remove_all_videos():
28
+ videos = (
29
+ glob(os.path.join(".", "data", "*.mp4"))
30
+ + glob(os.path.join(".", "data", "*.webm"))
31
+ + glob(os.path.join(".", "data", "*.mov"))
32
+ + glob(os.path.join(".", "data", "*.m4a"))
33
+ + glob(os.path.join(".", "data", "*.mkv"))
34
+ )
35
  for video in videos:
36
  try:
37
  os.remove(video)
 
39
  print("Couldn't delete file:", video)
40
 
41
 
42
+ # --- Main Functions ---
43
  def download_video(url: str, save_path: str, **kwargs):
44
  video_id = url.split("/")[-1]
45
 
 
47
  video_id = url.split("/")[-2]
48
 
49
  ydl_opts = {
50
+ "outtmpl": f"{save_path}/{video_id}.%(ext)s",
51
  }
52
 
53
  if st.session_state["ig_cookie"]:
54
+ ydl_opts["cookies"] = st.session_state["ig_cookie"]
55
 
56
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
57
  ydl.download(url)
58
 
59
+ videos = glob(os.path.join(".", "data", f"{video_id}.*"))
 
 
 
60
 
61
  print("videos found:", videos)
62
  for video in videos:
63
  if video_id in video:
64
  video_path = video
65
+ converted_video_path = convert_video_to_mp4(video_path)
66
+ return converted_video_path
67
+
68
+
69
+ def convert_video_to_mp4(video_path):
70
+ target_path = f"{os.path.splitext(video_path)[0]}.mp4"
71
+ if not os.path.exists(target_path): # Convert only if target doesn't exist
72
+ (ffmpeg.input(video_path).output(target_path).run(overwrite_output=True))
73
+ try:
74
+ os.remove(video_path)
75
+ except FileNotFoundError:
76
+ print("Couldn't delete file:", video_path)
77
+ return target_path
78
+
79
 
80
+ # --- Streamlit App ---
81
 
82
  st.title("Download Any* Video")
83
 
 
85
  with st.sidebar:
86
  st.subheader("Cookies! :cookie:")
87
  st.write(
88
+ "For some websites that have a more strict bot control it's necessary to upload a cookie file to download content from them"
89
+ )
90
+ st.file_uploader(
91
+ label="**Upload Instagram Cookie** :cookie:",
92
+ type=["txt", "json"],
93
+ accept_multiple_files=False,
94
+ key="ig_cookie",
95
+ )
96
+ st.file_uploader(
97
+ label="**Upload TikTok Cookie** :cookie:",
98
+ type=["txt", "json"],
99
+ accept_multiple_files=False,
100
+ key="tt_cookie",
101
+ )
102
+ st.button(
103
+ "Clear Videos",
104
+ on_click=_remove_all_videos,
105
+ type="secondary",
106
+ help="Clear all downloaded videos",
107
+ )
108
 
109
  # Main content
110
+ st.text_input(
111
+ "Video URL",
112
+ key="url",
113
+ placeholder="Enter Video URL and Hit Enter",
114
+ on_change=_remove_video,
115
+ )
116
 
117
  if st.session_state["url"]:
118
  download = st.button("Load Video")
119
  if download:
120
+ video_path = download_video(
121
+ st.session_state["url"], save_path=os.path.join(".", "data")
122
+ )
123
  st.session_state["videos"] = video_path
124
 
125
  if st.session_state["videos"]:
 
132
 
133
  try:
134
  video_file = open(st.session_state["videos"], "rb")
135
+ st.download_button(
136
+ "**Download Video ๐Ÿ“**",
137
+ data=video_file,
138
+ file_name=f"{st.session_state['videos'].lower().translate(str.maketrans('', '', string.punctuation)).replace(' ', '_')}.mp4",
139
+ mime="video/mp4",
140
+ type="primary",
141
+ )
142
  except Exception as e:
143
  st.error("Failed downloading the video", icon="๐Ÿ˜”")