Update app.py
Browse files1078 unmodified lines1079108010811082108310841085108210831086108710881089109084 unmodified lines11751176117711741175117611771178117911781179118011811182118311841185118611877 unmodified lines1195119611971198119311991200120112023 unmodified lines1206120712081209121012111204120512061207121212131214121512161217121815 unmodified lines12341235123612371238123912401241124212431244124512461247124812491250125112521253125412551078 unmodified lines new_chunk_state = None if chunk_enabled: prior_paths = chunk_state["paths"] if chunk_advance else [] # The momentum-imposed opening is a *regeneration* of the previous chunk's own tail, not new content — # trimmed here so concatenation doesn't duplicate it. Only continuation chunks that actually had momentum # applied carry anything to trim; chunk one, and any chunk that fell back to a plain keyframe carry, don't. chunk_output = _trim_head(path, float(momentum)) if (chunk_advance and given_video is not None) else path chunk_paths = prior_paths + [path] path = _concat_chunks(chunk_paths) if len(chunk_paths) > 1 else path chunk_paths = prior_paths + [chunk_output] path = _concat_chunks(chunk_paths) if len(chunk_paths) > 1 else chunk_output new_chunk_state = {"paths": chunk_paths}
return path, report, new_stage_state, new_chunk_state84 unmodified lines
def _trailing_frames(video_path: str, seconds: float): """The last `seconds` of `video_path`'s pixel frames, at MiniMax-H3's own native 24 fps, as `(num_frames, 3, H, W)` float in `[0, 1]` — the format `MiniMaxH3MomentumConditionStep` expects for `given_video`. Strided back down to 24 fps if the saved chunk was FILM-interpolated to a multiple of it: feeding the video VAE frames at anything other than its own encoding rate would encode the motion at the wrong speed — a real correctness point, not a cosmetic one, since FILM's own multiplier is read fresh from the UI on every press. Runs on CPU; no GPU time. """The last `snap_frames(seconds)` of `video_path`'s pixel frames, at MiniMax-H3's own native 24 fps, as `(num_frames, 3, H, W)` **uint8** — `encode_vae_condition`'s own documented input convention (it does its own `/255` and ImageNet normalization internally; pre-dividing here would double it). The frame count is snapped to the same `17 * n + 5` the video VAE's temporal chunking requires for a multi-frame encode, per that function's own docstring — the same alignment `snap_frames` already gives a full request. Strided back to 24 fps first if the saved chunk was FILM-interpolated to a multiple of it: encoding frames at the wrong rate would encode the motion at the wrong speed. Runs on CPU; no GPU time. """ if not video_path or seconds <= 0: return None7 unmodified lines actual_fps = cap.get(cv2.CAP_PROP_FPS) or FPS stride = max(1, round(actual_fps / FPS)) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) target_frames = snap_frames(seconds) start_frame = max(0, total_frames - round(seconds * actual_fps)) start_frame = max(0, total_frames - target_frames * stride)
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame) frames = []3 unmodified lines break if index % stride == 0: frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) if len(frames) == target_frames: break cap.release() if not frames: return None array = np.stack(frames).astype(np.float32) / 255.0 return torch.from_numpy(array).permute(0, 3, 1, 2).contiguous() if len(frames) < target_frames: return None # not enough source frames for a full, correctly-aligned encode array = np.stack(frames) return torch.from_numpy(array).permute(0, 3, 1, 2).contiguous() # uint8, (num_frames, 3, H, W)
def _concat_chunks(paths: list[str]) -> str:15 unmodified lines ) return out_path
def _trim_head(video_path: str, seconds: float) -> str: """`video_path` with its first `seconds` cut off — the momentum-imposed opening a continuation chunk regenerates from the previous chunk's own tail, which would otherwise be duplicated once the chunks are concatenated. Re-encodes rather than stream-copying: an arbitrary, non-keyframe-aligned cut point can't always be trimmed losslessly with `-c copy`. """ import subprocess
directory = os.path.join(tempfile.gettempdir(), "pk-h3-chunks") os.makedirs(directory, exist_ok=True) out_path = os.path.join(directory, f"trimmed-{int(time.time() * 1000)}.mp4") subprocess.run( ["ffmpeg", "-y", "-ss", str(seconds), "-i", video_path, out_path], check=True, capture_output=True, ) return out_path
load_models()
app.py+37-13