programmatic video rendering framework that is actually fast
fix(media): use the video track's DURATION tag before the container's
heojunfof1b82faf4d5c
diff --git a/fframes-media/src/video_decoder.rs b/fframes-media/src/video_decoder.rsindex d07ec32f..4ce5b892 100644--- a/fframes-media/src/video_decoder.rs+++ b/fframes-media/src/video_decoder.rs@@ -5,7 +5,7 @@ use ffmpeg_sys_fframes::SwsFlags::{SWS_BICUBIC, SWS_BILINEAR};use ffmpeg_sys_fframes::*;use std::cell::UnsafeCell;use std::collections::VecDeque;-use std::ffi::{CString, c_void};+use std::ffi::{CStr, CString, c_void};use std::mem::size_of;use std::path::Path;use std::ptr;@@ -13,17 +13,52 @@ use std::sync::Arc;use usvgr::PreloadedImageData;/// The video stream's duration in its own time base. Matroska and `WebM` leave the stream-/// duration unset (`AV_NOPTS_VALUE`) and only carry the container's, so fall back to that.-/// Either can be missing or 0; the caller treats a non-positive duration as unknown.+/// duration unset (`AV_NOPTS_VALUE`); their muxers write each track's length as a `DURATION`+/// tag instead, so use that. Only when the tag is missing too fall back to the container's+/// duration, which also covers any longer audio track. Any of these can be missing or 0; the+/// caller treats a non-positive duration as unknown.unsafe fn stream_duration(fmt_ctx: *mut AVFormatContext, stream: *mut AVStream) -> i64 {unsafe {- if (*stream).duration > 0 || (*fmt_ctx).duration <= 0 {+ if (*stream).duration > 0 {+ return (*stream).duration;+ }+ if let Some(micros) = duration_tag(stream) {+ return av_rescale_q(micros, AV_TIME_BASE_Q, (*stream).time_base);+ }+ if (*fmt_ctx).duration <= 0 {return (*stream).duration;}av_rescale_q((*fmt_ctx).duration, AV_TIME_BASE_Q, (*stream).time_base)}}+/// The stream's `DURATION` tag (`HH:MM:SS.nnnnnnnnn`) in `AV_TIME_BASE` units.+unsafe fn duration_tag(stream: *mut AVStream) -> Option<i64> {+ let entry = unsafe { av_dict_get((*stream).metadata, c"DURATION".as_ptr(), ptr::null(), 0) };+ if entry.is_null() {+ return None;+ }+ let value = unsafe { CStr::from_ptr((*entry).value) }.to_str().ok()?;+ let mut parts = value.trim().splitn(3, ':');+ let hours: i64 = parts.next()?.parse().ok()?;+ let minutes: i64 = parts.next()?.parse().ok()?;+ let seconds = parts.next()?;+ let (seconds, fraction) = seconds.split_once('.').unwrap_or((seconds, ""));+ let seconds: i64 = seconds.parse().ok()?;+ // Keep the first 6 fractional digits (microseconds), right-padded with zeros.+ let micros = fraction+ .bytes()+ .chain(std::iter::repeat(b'0'))+ .take(6)+ .try_fold(0i64, |acc, digit| {+ digit+ .is_ascii_digit()+ .then(|| acc * 10 + i64::from(digit - b'0'))+ })?;+ let total = ((hours * 60 + minutes) * 60 + seconds) * 1_000_000 + micros;+ (total > 0).then_some(total)+}+const FFRAMES_VIDEO_PATH_TAG: &str = "___fframes_internal_video_frame_pts___";// just a little bit faster than the format! macropub fn encode_video_resource(resource: &str, pts: i64) -> String {