MogensR commited on
Commit
2117d05
·
verified ·
1 Parent(s): dd0209f

Update pipeline/video_pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline/video_pipeline.py +64 -6
pipeline/video_pipeline.py CHANGED
@@ -6,6 +6,7 @@
6
  """
7
 
8
  import os
 
9
  import time
10
  import tempfile
11
  import shutil
@@ -162,6 +163,54 @@ def mux_audio(video_no_audio: str, source_with_audio: str, output: str) -> bool:
162
  TMP_DIR = Path("tmp")
163
  TMP_DIR.mkdir(parents=True, exist_ok=True)
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  # ==================================================================================
166
  # SAM2 Mask Generation
167
  # ==================================================================================
@@ -448,11 +497,20 @@ def update_progress(progress, message):
448
  # Process video
449
  with tempfile.TemporaryDirectory() as temp_dir:
450
  temp_dir = Path(temp_dir)
451
- input_path = str(temp_dir / "input.mp4")
452
-
453
- # Save input video
454
- with open(input_path, "wb") as f:
455
- f.write(input_file.getvalue())
 
 
 
 
 
 
 
 
 
456
 
457
  update_progress(0.2, "Generating first-frame segmentation mask...")
458
 
@@ -655,4 +713,4 @@ def update_progress(progress, message):
655
  except Exception as e:
656
  logger.error(f"Stage 2 error: {e}", exc_info=True)
657
  st.error(f"Stage 2 failed: {e}")
658
- return None
 
6
  """
7
 
8
  import os
9
+ import io
10
  import time
11
  import tempfile
12
  import shutil
 
163
  TMP_DIR = Path("tmp")
164
  TMP_DIR.mkdir(parents=True, exist_ok=True)
165
 
166
+ # ==================================================================================
167
+ # INPUT NORMALIZATION - Accept path | bytes | file-like
168
+ # ==================================================================================
169
+
170
+ def _normalize_video_input_to_path(inp, work_dir: Path) -> str:
171
+ """
172
+ Accept:
173
+ - str path (existing)
174
+ - bytes/bytearray
175
+ - file-like with .getvalue() or .read()
176
+ Return:
177
+ - A filesystem path to the video, writing to `work_dir` if needed.
178
+ """
179
+ # Already a path on disk?
180
+ if isinstance(inp, str):
181
+ if os.path.exists(inp):
182
+ logger.info(f"[Stage1] Input detected as existing path: {inp}")
183
+ return inp
184
+ raise FileNotFoundError(f"[Stage1] Input path not found: {inp}")
185
+
186
+ data = None
187
+ # Raw bytes
188
+ if isinstance(inp, (bytes, bytearray)):
189
+ data = bytes(inp)
190
+ logger.info(f"[Stage1] Input provided as raw bytes: {len(data)} bytes")
191
+ # File-like with .getvalue()
192
+ elif hasattr(inp, "getvalue"):
193
+ try:
194
+ data = inp.getvalue()
195
+ logger.info(f"[Stage1] Input provided as file-like (.getvalue): {len(data)} bytes")
196
+ except Exception as e:
197
+ raise TypeError(f"[Stage1] Could not read file-like via .getvalue(): {e}")
198
+ # File-like with .read()
199
+ elif hasattr(inp, "read"):
200
+ try:
201
+ data = inp.read()
202
+ logger.info(f"[Stage1] Input provided as file-like (.read): {len(data)} bytes")
203
+ except Exception as e:
204
+ raise TypeError(f"[Stage1] Could not read file-like via .read(): {e}")
205
+ else:
206
+ raise TypeError(f"[Stage1] Unsupported input type: {type(inp)}")
207
+
208
+ # Materialize to a stable local path under work_dir
209
+ target = work_dir / "input.mp4"
210
+ with open(target, "wb") as f:
211
+ f.write(data)
212
+ return str(target)
213
+
214
  # ==================================================================================
215
  # SAM2 Mask Generation
216
  # ==================================================================================
 
497
  # Process video
498
  with tempfile.TemporaryDirectory() as temp_dir:
499
  temp_dir = Path(temp_dir)
500
+
501
+ # --- NEW: normalize incoming input (path | bytes | file-like) to a usable path ---
502
+ try:
503
+ input_path = _normalize_video_input_to_path(input_file, temp_dir)
504
+ if os.path.exists(input_path):
505
+ size_mb = os.path.getsize(input_path) / 1e6
506
+ logger.info(f"[Stage1] Using input path: {input_path} ({size_mb:.2f} MB)")
507
+ else:
508
+ logger.warning(f"[Stage1] Input path reported but does not exist: {input_path}")
509
+ except Exception as e:
510
+ logger.error(f"[Stage1] Failed to materialize input: {e}", exc_info=True)
511
+ st.error(f"Stage 1 failed to read input video: {e}")
512
+ heartbeat_flag["running"] = False
513
+ return None
514
 
515
  update_progress(0.2, "Generating first-frame segmentation mask...")
516
 
 
713
  except Exception as e:
714
  logger.error(f"Stage 2 error: {e}", exc_info=True)
715
  st.error(f"Stage 2 failed: {e}")
716
+ return None