Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import tempfile
|
|
| 5 |
import base64
|
| 6 |
import json
|
| 7 |
from io import BytesIO
|
| 8 |
-
from typing import List, Tuple, Optional, Set
|
| 9 |
import requests
|
| 10 |
from PIL import Image, ImageFile, UnidentifiedImageError
|
| 11 |
import gradio as gr
|
|
@@ -176,19 +176,21 @@ def fetch_bytes(src: str, stream_threshold: int = STREAM_THRESHOLD_BYTES, timeou
|
|
| 176 |
fd, p = tempfile.mkstemp(suffix=ext_from_src(src) or ".tmp")
|
| 177 |
os.close(fd)
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
progress
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
| 192 |
finally:
|
| 193 |
try: os.remove(p)
|
| 194 |
except Exception as e: print(f"Error during streaming temp file cleanup {p}: {e}")
|
|
@@ -243,13 +245,19 @@ def _ffprobe_streams(path: str) -> Optional[dict]:
|
|
| 243 |
"""Uses ffprobe to get stream information for a media file."""
|
| 244 |
if not FFMPEG_BIN:
|
| 245 |
return None
|
| 246 |
-
|
| 247 |
-
ffprobe_path =
|
| 248 |
-
if
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
if
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
cmd = [
|
| 255 |
ffprobe_path, "-v", "error", "-print_format", "json", "-show_streams", "-show_format", path
|
|
|
|
| 5 |
import base64
|
| 6 |
import json
|
| 7 |
from io import BytesIO
|
| 8 |
+
from typing import List, Tuple, Optional, Set
|
| 9 |
import requests
|
| 10 |
from PIL import Image, ImageFile, UnidentifiedImageError
|
| 11 |
import gradio as gr
|
|
|
|
| 176 |
fd, p = tempfile.mkstemp(suffix=ext_from_src(src) or ".tmp")
|
| 177 |
os.close(fd)
|
| 178 |
try:
|
| 179 |
+
# FIX: Open file for writing before the streaming loop
|
| 180 |
+
with open(p, "wb") as fh_write:
|
| 181 |
+
with requests.get(src, timeout=timeout, stream=True, headers=DEFAULT_HEADERS) as r:
|
| 182 |
+
r.raise_for_status()
|
| 183 |
+
total_size = int(r.headers.get("content-length", 0))
|
| 184 |
+
downloaded_size = 0
|
| 185 |
+
for chunk in r.iter_content(8192):
|
| 186 |
+
if chunk:
|
| 187 |
+
fh_write.write(chunk)
|
| 188 |
+
downloaded_size += len(chunk)
|
| 189 |
+
if progress is not None and total_size > 0:
|
| 190 |
+
# Scale progress from 0.1 to 0.25 for streaming phase
|
| 191 |
+
progress(0.1 + (downloaded_size / total_size) * 0.15)
|
| 192 |
+
with open(p, "rb") as fh_read: # Open again to read full content
|
| 193 |
+
return fh_read.read()
|
| 194 |
finally:
|
| 195 |
try: os.remove(p)
|
| 196 |
except Exception as e: print(f"Error during streaming temp file cleanup {p}: {e}")
|
|
|
|
| 245 |
"""Uses ffprobe to get stream information for a media file."""
|
| 246 |
if not FFMPEG_BIN:
|
| 247 |
return None
|
| 248 |
+
|
| 249 |
+
ffprobe_path = None
|
| 250 |
+
if FFMPEG_BIN:
|
| 251 |
+
ffmpeg_dir = os.path.dirname(FFMPEG_BIN)
|
| 252 |
+
potential_ffprobe_in_dir = os.path.join(ffmpeg_dir, "ffprobe")
|
| 253 |
+
if os.path.exists(potential_ffprobe_in_dir) and os.access(potential_ffprobe_in_dir, os.X_OK):
|
| 254 |
+
ffprobe_path = potential_ffprobe_in_dir
|
| 255 |
+
|
| 256 |
+
if not ffprobe_path: # Fallback to checking PATH if not found next to ffmpeg
|
| 257 |
+
ffprobe_path = shutil.which("ffprobe")
|
| 258 |
+
|
| 259 |
+
if not ffprobe_path:
|
| 260 |
+
return None # ffprobe is not available
|
| 261 |
|
| 262 |
cmd = [
|
| 263 |
ffprobe_path, "-v", "error", "-print_format", "json", "-show_streams", "-show_format", path
|