Spaces:
Paused
Wideo bez metadanych o dlugosci bylo probkowane tylko z poczatku
Browse filesGalaz awaryjna w sample_frames brala co trzydziesta klatke i przerywala po
osmiu. Przy 25 klatkach na sekunde to jest dziesiec pierwszych sekund
nagrania, podawane modelowi jako przeglad calosci. Nie mialo tez zadnej
deduplikacji, wiec statyczne nagranie ekranu placilo pelnym kosztem tokenow
za osiem kopii tego samego obrazu.
Teraz, gdy metadane nie podaja dlugosci, mierzymy ja demuxujac strumien bez
dekodowania - pakiety niosa wlasne znaczniki, a pominiecie dekodera czyni to
tanim - i wracamy na normalna sciezke rownomiernego rozlozenia. Sonda idzie
na WLASNYM uchwycie: demuxowanie przewija strumien do konca, a surowy
strumien elementarny nie umie sie cofnac, wiec badanie przez ten sam
kontener, z ktorego zaraz dekodujemy, zostawialo go na koncu pliku i sampler
zwracal pusta liste.
Gdy nie da sie zmierzyc nawet tak, probka rozklada sie na caly strumien
zamiast na jego poczatek, z deduplikacja. Surowy strumien nie ma znacznikow
czasu w ogole, wiec czas szacujemy z numeru klatki i nominalnej liczby klatek
na sekunde: wymaganie prawdziwego pts zwracalo pusta liste i nagranie
raportowalo sie jako nieczytelne.
Zmierzone na minutowym nagraniu z szescioma scenami, przekodowanym do trzech
postaci:
- mp4 z metadanymi: 8 klatek, rozpietosc 46,7 s (bez zmian)
- mpegts: 5 klatek, rozpietosc 40,0 s
- surowy h264: przed zmiana 0 klatek, po zmianie 7 klatek, rozpietosc 50,4 s,
a model odczytal z nich wszystkie szesc scen z poprawnymi znacznikami.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
57 unmodified lines58596061626327 unmodified lines91929394959697989910010110210310410557 unmodified linesif not duration and container.duration:duration = float(container.duration) / 1_000_000.0 # AV_TIME_BASEif duration and duration > 0:# Evenly spaced timestamps; seek to each and grab the next decoded frame.times = [duration * (i + 1) / (max_frames + 1) for i in range(max_frames)]27 unmodified linesexcept Exception:continueelse:# No duration metadata: decode sequentially and keep every Nth frame.step, idx = 30, 0for frame in container.decode(vstream):if idx % step == 0:out.append((round(float(frame.pts * tb) if frame.pts else 0.0, 1),_to_data_url(frame.to_image())))if len(out) >= max_frames:breakidx += 1container.close()except Exception as e:print(f"[LUNA] frames: sampling failed for {path} ({type(e).__name__}: {e}).")57 unmodified lines58596061626364656667686970717273747576777879808182838485868788899027 unmodified lines11811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415557 unmodified linesif not duration and container.duration:duration = float(container.duration) / 1_000_000.0 # AV_TIME_BASEif not duration or duration <= 0:# No duration in the metadata (fragmented MP4, some WebM, a few recorders).# Rather than fall back to sampling the opening and calling it the video,# demux the stream WITHOUT decoding to find the last timestamp. Packets# carry their pts and skipping the decode makes this cheap, and it puts us# back on the evenly-spaced path below.# On its OWN handle. Demuxing runs the stream to the end, and a raw# elementary stream cannot seek back, so probing through the container we# are about to decode from left it at EOF and the sampler returned nothing.try:probe = av.open(path)try:pv = probe.streams.video[0]ptb = pv.time_baselast = 0.0for packet in probe.demux(pv):if packet.pts is not None:last = max(last, float(packet.pts * ptb))if last > 0:duration = lastprint(f"[LUNA] frames: no duration in metadata, measured "f"{last:.1f}s by demuxing.")finally:probe.close()except Exception:passif duration and duration > 0:# Evenly spaced timestamps; seek to each and grab the next decoded frame.times = [duration * (i + 1) / (max_frames + 1) for i in range(max_frames)]27 unmodified linesexcept Exception:continueelse:# Still no idea how long it is, so spread the sample over the whole stream# by keeping the LAST max_frames of evenly thinned frames: a fixed "every# 30th frame, stop at eight" covered the first ten seconds of a 25fps clip# and reported it as a view of the video. Decoding runs to the end either# way; only which frames are kept changes.# A raw elementary stream carries no timestamps at all, so pts is None on# every frame. Requiring one here returned an empty list and the video was# reported unreadable; the frame index over the stream's nominal rate is a# perfectly good estimate, and better than no video at all.try:fps = float(vstream.average_rate) if vstream.average_rate else 0.0except Exception:fps = 0.0step, idx = 15, 0picked_all: list = []for frame in container.decode(vstream):if idx % step == 0:if frame.pts is not None:ts = float(frame.pts * tb)else:ts = idx / fps if fps > 0 else 0.0picked_all.append((ts, frame.to_image()))idx += 1if picked_all:stride = max(1, len(picked_all) // max_frames)for ts, img in picked_all[::stride][:max_frames]:sig = _signature(img)if sig in sigs:skipped_same += 1continuesigs.add(sig)out.append((round(ts, 1), _to_data_url(img)))container.close()except Exception as e:print(f"[LUNA] frames: sampling failed for {path} ({type(e).__name__}: {e}).")