Update app.py
Browse files1078 unmodified lines1079108010811082108310841085108684 unmodified lines1171117211731174117511761177117811791180118111827 unmodified lines11901191119211931194119511963 unmodified lines1200120112021203120412051206120712081209121015 unmodified lines1226122712281229123012311078 unmodified linesnew_chunk_state = Noneif chunk_enabled:prior_paths = chunk_state["paths"] if chunk_advance else []chunk_paths = prior_paths + [path]path = _concat_chunks(chunk_paths) if len(chunk_paths) > 1 else pathnew_chunk_state = {"paths": chunk_paths}return path, report, new_stage_state, new_chunk_state84 unmodified linesdef _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`. Stridedback down to 24 fps if the saved chunk was FILM-interpolated to a multiple of it: feeding the video VAEframes at anything other than its own encoding rate would encode the motion at the wrong speed — areal correctness point, not a cosmetic one, since FILM's own multiplier is read fresh from the UI on everypress. Runs on CPU; no GPU time."""if not video_path or seconds <= 0:return None7 unmodified linesactual_fps = cap.get(cv2.CAP_PROP_FPS) or FPSstride = max(1, round(actual_fps / FPS))total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))start_frame = max(0, total_frames - round(seconds * actual_fps))cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)frames = []3 unmodified linesbreakif index % stride == 0:frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))cap.release()if not frames:return Nonearray = np.stack(frames).astype(np.float32) / 255.0return torch.from_numpy(array).permute(0, 3, 1, 2).contiguous()def _concat_chunks(paths: list[str]) -> str:15 unmodified lines)return out_pathload_models()1078 unmodified lines10791080108110821083108410851086108710881089109084 unmodified lines11751176117711781179118011811182118311841185118611877 unmodified lines119511961197119811991200120112023 unmodified lines120612071208120912101211121212131214121512161217121815 unmodified lines12341235123612371238123912401241124212431244124512461247124812491250125112521253125412551078 unmodified linesnew_chunk_state = Noneif 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 pathchunk_paths = prior_paths + [chunk_output]path = _concat_chunks(chunk_paths) if len(chunk_paths) > 1 else chunk_outputnew_chunk_state = {"paths": chunk_paths}return path, report, new_stage_state, new_chunk_state84 unmodified linesdef _trailing_frames(video_path: str, seconds: float):"""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 itsown `/255` and ImageNet normalization internally; pre-dividing here would double it). The frame count issnapped to the same `17 * n + 5` the video VAE's temporal chunking requires for a multi-frame encode, perthat function's own docstring — the same alignment `snap_frames` already gives a full request. Strided backto 24 fps first if the saved chunk was FILM-interpolated to a multiple of it: encoding frames at the wrongrate would encode the motion at the wrong speed. Runs on CPU; no GPU time."""if not video_path or seconds <= 0:return None7 unmodified linesactual_fps = cap.get(cv2.CAP_PROP_FPS) or FPSstride = 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 - target_frames * stride)cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)frames = []3 unmodified linesbreakif index % stride == 0:frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))if len(frames) == target_frames:breakcap.release()if len(frames) < target_frames:return None # not enough source frames for a full, correctly-aligned encodearray = 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_pathdef _trim_head(video_path: str, seconds: float) -> str:"""`video_path` with its first `seconds` cut off — the momentum-imposed opening a continuation chunkregenerates from the previous chunk's own tail, which would otherwise be duplicated once the chunks areconcatenated. Re-encodes rather than stream-copying: an arbitrary, non-keyframe-aligned cut point can'talways be trimmed losslessly with `-c copy`."""import subprocessdirectory = 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_pathload_models()
app.py+37-13