[Bugfix] Video loading: sample over presentable frames, not header sample count (MP4 edit-list trims) (#48608)
Signed-off-by: AmitMY <amitmoryossef@gmail.com> Signed-off-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
co-authored by
Claude Fable 5
Isotr0py
parent
11baa0ebd8
commit
9b2aef5124
@@ -103,6 +103,43 @@ def create_long_gop_video(
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def create_edit_list_trimmed_video(
|
||||
num_frames: int = 90,
|
||||
trim_start_frame: int = 60,
|
||||
fps: int = 30,
|
||||
) -> tuple[bytes, int]:
|
||||
"""Stream-copy-cut a long-GOP clip so an mp4 edit list hides the lead-in.
|
||||
|
||||
Remuxes the tail of a single-keyframe clip without re-encoding, rebasing
|
||||
timestamps at ``trim_start_frame``: the lead-in packets are still needed
|
||||
to decode, so they stay in the file at negative pts and the muxer records
|
||||
an edit list. The header sample count stays ``num_frames`` while only the
|
||||
trailing frames are presentable — the shape produced by lossless trims
|
||||
(e.g. ``ffmpeg -ss ... -c copy``). Returns (video_bytes, visible_frames).
|
||||
"""
|
||||
import io
|
||||
|
||||
import av
|
||||
|
||||
src = create_long_gop_video(num_frames=num_frames, fps=fps)
|
||||
buf = io.BytesIO()
|
||||
with (
|
||||
av.open(io.BytesIO(src)) as source,
|
||||
av.open(buf, mode="w", format="mp4") as out,
|
||||
):
|
||||
in_stream = source.streams.video[0]
|
||||
out_stream = out.add_stream_from_template(in_stream)
|
||||
start_pts = round(trim_start_frame / fps / in_stream.time_base)
|
||||
for packet in source.demux(in_stream):
|
||||
if packet.pts is None or packet.dts is None:
|
||||
continue
|
||||
packet.pts -= start_pts
|
||||
packet.dts -= start_pts
|
||||
packet.stream = out_stream
|
||||
out.mux(packet)
|
||||
return buf.getvalue(), num_frames - trim_start_frame
|
||||
|
||||
|
||||
def cosine_similarity(A: npt.NDArray, B: npt.NDArray, axis: int = -1) -> npt.NDArray:
|
||||
"""Compute cosine similarity between two vectors."""
|
||||
return np.sum(A * B, axis=axis) / (
|
||||
|
||||
Reference in New Issue
Block a user