Hug0endob commited on
Commit
6d68124
·
verified ·
1 Parent(s): dd1c77a

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +46 -15
streamlit_app.py CHANGED
@@ -160,30 +160,61 @@ def _download_direct(url: str, dst: Path) -> Path:
160
 
161
 
162
  def _download_with_yt_dlp(url: str, dst: Path, password: str = "") -> Path:
163
- """Fallback downloader using yt‑dlp."""
164
  tmpl = str(dst / "%(id)s.%(ext)s")
165
- opts = {"outtmpl": tmpl, "format": "best"}
 
 
 
 
 
 
 
 
166
  if password:
167
  opts["videopassword"] = password
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  try:
170
  with yt_dlp.YoutubeDL(opts) as ydl:
171
  info = ydl.extract_info(url, download=True)
172
  except Exception as e:
173
  raise RuntimeError(f"yt‑dlp could not download the URL: {e}") from e
174
-
175
- # yt‑dlp may return a dict with the final filename
176
- if isinstance(info, dict) and "id" in info:
177
- candidate = dst / f"{info['id']}.{info.get('ext', 'mp4')}"
178
- if candidate.exists():
179
- return _convert_to_mp4(candidate)
180
-
181
- # Fallback: pick the newest file in the output folder
182
- files = list(dst.iterdir())
183
- if not files:
184
- raise RuntimeError("yt‑dlp did not produce any files.")
185
- newest = max(files, key=lambda p: p.stat().st_mtime)
186
- return _convert_to_mp4(newest)
 
 
 
 
 
 
187
 
188
 
189
  def download_video(url: str, dst: Path, password: str = "") -> Path:
 
160
 
161
 
162
  def _download_with_yt_dlp(url: str, dst: Path, password: str = "") -> Path:
163
+ """Download via yt‑dlp with Streamlit progress and MP4‑first format."""
164
  tmpl = str(dst / "%(id)s.%(ext)s")
165
+ # Prefer MP4, fall back to best if not available
166
+ fmt = "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio"
167
+ opts = {
168
+ "outtmpl": tmpl,
169
+ "format": fmt,
170
+ "quiet": True, # we handle progress ourselves
171
+ "noprogress": True,
172
+ "nocheckcertificate": True,
173
+ }
174
  if password:
175
  opts["videopassword"] = password
176
 
177
+ progress_bar = st.empty()
178
+ status_text = st.empty()
179
+
180
+ def _progress_hook(d):
181
+ if d["status"] == "downloading":
182
+ total = d.get("total_bytes") or d.get("total_bytes_estimate")
183
+ downloaded = d.get("downloaded_bytes", 0)
184
+ if total:
185
+ pct = downloaded / total
186
+ progress_bar.progress(pct)
187
+ status_text.caption(f"Downloading… {pct:.0%}")
188
+ elif d["status"] == "finished":
189
+ progress_bar.progress(1.0)
190
+ status_text.caption("Download complete, processing…")
191
+
192
+ opts["progress_hooks"] = [_progress_hook]
193
+
194
  try:
195
  with yt_dlp.YoutubeDL(opts) as ydl:
196
  info = ydl.extract_info(url, download=True)
197
  except Exception as e:
198
  raise RuntimeError(f"yt‑dlp could not download the URL: {e}") from e
199
+ finally:
200
+ progress_bar.empty()
201
+ status_text.empty()
202
+
203
+ # yt‑dlp may have produced several files; pick the newest MP4
204
+ mp4_files = list(dst.glob("*.mp4"))
205
+ if not mp4_files:
206
+ raise RuntimeError("No MP4 file was created.")
207
+ newest = max(mp4_files, key=lambda p: p.stat().st_mtime)
208
+
209
+ # Optional cache: if a file with the same SHA‑256 already exists, reuse it
210
+ sha = _file_sha256(newest)
211
+ if sha:
212
+ for existing in dst.iterdir():
213
+ if existing != newest and _file_sha256(existing) == sha:
214
+ newest.unlink() # remove duplicate
215
+ return existing
216
+
217
+ return newest
218
 
219
 
220
  def download_video(url: str, dst: Path, password: str = "") -> Path: