patocolher commited on
Commit
caab4df
Β·
verified Β·
1 Parent(s): 6b60c63

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -194,8 +194,10 @@ def reencode_video(
194
  yield None, "❌ Nenhum vΓ­deo enviado!"
195
  return
196
 
197
- # gr.File pode retornar objeto com .name em algumas versΓ΅es do Gradio
198
- if hasattr(video_file, "name"):
 
 
199
  video_file = video_file.name
200
 
201
  ext = os.path.splitext(video_file)[1].lower() or ".mp4"
@@ -214,36 +216,44 @@ def reencode_video(
214
  probe_extra = ["-analyzeduration", "100M", "-probesize", "100M"] if precisa_probe_extra else []
215
  fmt_extra = ["-f", "mpeg"] if ext == ".vob" else []
216
 
 
 
 
 
 
 
 
 
 
 
217
  # ── Detecta dimensΓ£o / orientaΓ§Γ£o ───────────────────────────
218
- probe = subprocess.run(
219
- ["ffprobe", "-v", "error"] + probe_extra + fmt_extra + [
220
- "-select_streams", "v:0",
221
- "-show_entries", "stream=width,height",
222
- "-of", "json", input_path
223
- ], capture_output=True, text=True)
224
- probe_data = json.loads(probe.stdout)
225
- stream = probe_data.get("streams", [{}])[0]
 
226
  vid_w = int(stream.get("width", 1920))
227
  vid_h = int(stream.get("height", 1080))
228
  is_vertical = vid_h > vid_w
229
 
230
  # ── DuraΓ§Γ£o ─────────────────────────────────────────────────
231
- probe_dur = subprocess.run(
232
- ["ffprobe", "-v", "error"] + probe_extra + fmt_extra + [
233
- "-show_entries", "format=duration",
234
- "-of", "json", input_path
235
- ], capture_output=True, text=True)
236
- dur_data = json.loads(probe_dur.stdout)
237
- total_duration = float(dur_data.get("format", {}).get("duration", 0))
238
 
239
  # ── Detecta Γ‘udio ───────────────────────────────────────────
240
- probe_audio = subprocess.run(
241
- ["ffprobe", "-v", "error"] + probe_extra + fmt_extra + [
242
- "-select_streams", "a",
243
- "-show_entries", "stream=codec_type",
244
- "-of", "json", input_path
245
- ], capture_output=True, text=True)
246
- has_audio = len(json.loads(probe_audio.stdout).get("streams", [])) > 0
247
 
248
  # ── GPU / logo ──────────────────────────────────────────────
249
  quer_gpu = "GPU" in modo
 
194
  yield None, "❌ Nenhum vΓ­deo enviado!"
195
  return
196
 
197
+ # Gradio 6 pode retornar dict {"path": "..."}, objeto com .name, ou string
198
+ if isinstance(video_file, dict):
199
+ video_file = video_file.get("path") or video_file.get("name", "")
200
+ elif hasattr(video_file, "name"):
201
  video_file = video_file.name
202
 
203
  ext = os.path.splitext(video_file)[1].lower() or ".mp4"
 
216
  probe_extra = ["-analyzeduration", "100M", "-probesize", "100M"] if precisa_probe_extra else []
217
  fmt_extra = ["-f", "mpeg"] if ext == ".vob" else []
218
 
219
+ def _run_ffprobe(args):
220
+ """Roda ffprobe e retorna dict parsed; em caso de falha loga e retorna {}."""
221
+ r = subprocess.run(args, capture_output=True, text=True)
222
+ if r.returncode != 0 or not r.stdout.strip():
223
+ return None, r.stderr.strip()
224
+ try:
225
+ return json.loads(r.stdout), None
226
+ except json.JSONDecodeError:
227
+ return None, r.stdout[:300]
228
+
229
  # ── Detecta dimensΓ£o / orientaΓ§Γ£o ───────────────────────────
230
+ data, err = _run_ffprobe(
231
+ ["ffprobe", "-v", "error"] + probe_extra + fmt_extra +
232
+ ["-select_streams", "v:0", "-show_entries", "stream=width,height",
233
+ "-of", "json", input_path]
234
+ )
235
+ if data is None:
236
+ yield None, f"❌ ffprobe falhou ao ler o vídeo:\n{err}"
237
+ return
238
+ stream = data.get("streams", [{}])[0]
239
  vid_w = int(stream.get("width", 1920))
240
  vid_h = int(stream.get("height", 1080))
241
  is_vertical = vid_h > vid_w
242
 
243
  # ── DuraΓ§Γ£o ─────────────────────────────────────────────────
244
+ data_dur, _ = _run_ffprobe(
245
+ ["ffprobe", "-v", "error"] + probe_extra + fmt_extra +
246
+ ["-show_entries", "format=duration", "-of", "json", input_path]
247
+ )
248
+ total_duration = float((data_dur or {}).get("format", {}).get("duration", 0))
 
 
249
 
250
  # ── Detecta Γ‘udio ───────────────────────────────────────────
251
+ data_audio, _ = _run_ffprobe(
252
+ ["ffprobe", "-v", "error"] + probe_extra + fmt_extra +
253
+ ["-select_streams", "a", "-show_entries", "stream=codec_type",
254
+ "-of", "json", input_path]
255
+ )
256
+ has_audio = len((data_audio or {}).get("streams", [])) > 0
 
257
 
258
  # ── GPU / logo ──────────────────────────────────────────────
259
  quer_gpu = "GPU" in modo