Hug0endob commited on
Commit
0e0bad7
·
verified ·
1 Parent(s): a59fa2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
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 # Import 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
- with requests.get(src, timeout=timeout, stream=True, headers=DEFAULT_HEADERS) as r:
180
- r.raise_for_status()
181
- total_size = int(r.headers.get("content-length", 0))
182
- downloaded_size = 0
183
- for chunk in r.iter_content(8192):
184
- if chunk:
185
- fh.write(chunk)
186
- downloaded_size += len(chunk)
187
- if progress is not None and total_size > 0:
188
- # Scale progress from 0.1 to 0.25 for streaming phase
189
- progress(0.1 + (downloaded_size / total_size) * 0.15)
190
- with open(p, "rb") as fh:
191
- return fh.read()
 
 
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
- # Use FFMPEG_BIN for ffprobe if it's explicitly provided and ends with ffmpeg
247
- ffprobe_path = FFMPEG_BIN.replace("ffmpeg", "ffprobe") if "ffmpeg" in os.path.basename(FFMPEG_BIN) else "ffprobe"
248
- if not shutil.which(ffprobe_path):
249
- # Fallback to just 'ffprobe' if the path replacement didn't find it
250
- ffprobe_path = "ffprobe"
251
- if not shutil.which(ffprobe_path):
252
- return None # ffprobe is not available
 
 
 
 
 
 
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