pub struct Timestamp { /* private fields */ }Expand description
A timestamp representing a point in time within a media stream.
Timestamps are represented as a presentation timestamp (PTS) value combined with a time base that defines the unit of measurement.
§Time Base
The time base is a rational number that represents the duration of one timestamp unit. For example:
1/90000: Each PTS unit is 1/90000 of a second (MPEG-TS)1/1000: Each PTS unit is 1 millisecond1/48000: Each PTS unit is one audio sample at 48kHz
§Examples
use ff_format::{Rational, Timestamp};
use std::time::Duration;
// Create a timestamp at 1 second using 90kHz time base
let time_base = Rational::new(1, 90000);
let ts = Timestamp::new(90000, time_base);
assert!((ts.as_secs_f64() - 1.0).abs() < 0.0001);
assert_eq!(ts.as_millis(), 1000);
// Convert from Duration
let ts2 = Timestamp::from_duration(Duration::from_secs(1), time_base);
assert_eq!(ts2.pts(), 90000);Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub const fn new(pts: i64, time_base: Rational) -> Self
pub const fn new(pts: i64, time_base: Rational) -> Self
Creates a new timestamp with the given PTS value and time base.
§Arguments
pts- The presentation timestamp valuetime_base- The time base (duration of one PTS unit)
§Examples
use ff_format::{Rational, Timestamp};
let time_base = Rational::new(1, 1000); // milliseconds
let ts = Timestamp::new(500, time_base); // 500ms
assert_eq!(ts.as_millis(), 500);Sourcepub const fn zero(time_base: Rational) -> Self
pub const fn zero(time_base: Rational) -> Self
Creates a timestamp representing zero (0 PTS).
§Examples
use ff_format::{Rational, Timestamp};
let time_base = Rational::new(1, 90000);
let zero = Timestamp::zero(time_base);
assert_eq!(zero.pts(), 0);
assert_eq!(zero.as_secs_f64(), 0.0);Sourcepub fn from_duration(duration: Duration, time_base: Rational) -> Self
pub fn from_duration(duration: Duration, time_base: Rational) -> Self
Creates a timestamp from a Duration value.
§Arguments
duration- The duration to converttime_base- The target time base for the resulting timestamp
§Examples
use ff_format::{Rational, Timestamp};
use std::time::Duration;
let time_base = Rational::new(1, 90000);
let ts = Timestamp::from_duration(Duration::from_millis(1000), time_base);
assert_eq!(ts.pts(), 90000);Sourcepub fn from_secs_f64(secs: f64, time_base: Rational) -> Self
pub fn from_secs_f64(secs: f64, time_base: Rational) -> Self
Creates a timestamp from a seconds value.
§Examples
use ff_format::{Rational, Timestamp};
let time_base = Rational::new(1, 1000);
let ts = Timestamp::from_secs_f64(1.5, time_base);
assert_eq!(ts.pts(), 1500);Sourcepub fn from_millis(millis: i64, time_base: Rational) -> Self
pub fn from_millis(millis: i64, time_base: Rational) -> Self
Creates a timestamp from milliseconds.
§Examples
use ff_format::{Rational, Timestamp};
let time_base = Rational::new(1, 90000);
let ts = Timestamp::from_millis(1000, time_base);
assert_eq!(ts.pts(), 90000);Sourcepub const fn pts(&self) -> i64
pub const fn pts(&self) -> i64
Returns the presentation timestamp value.
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(12345, Rational::new(1, 90000));
assert_eq!(ts.pts(), 12345);Sourcepub const fn time_base(&self) -> Rational
pub const fn time_base(&self) -> Rational
Returns the time base.
§Examples
use ff_format::{Rational, Timestamp};
let time_base = Rational::new(1, 90000);
let ts = Timestamp::new(100, time_base);
assert_eq!(ts.time_base(), time_base);Sourcepub fn as_duration(&self) -> Duration
pub fn as_duration(&self) -> Duration
Converts the timestamp to a Duration.
Note: Negative timestamps will be clamped to zero Duration.
§Examples
use ff_format::{Rational, Timestamp};
use std::time::Duration;
let ts = Timestamp::new(90000, Rational::new(1, 90000));
let duration = ts.as_duration();
assert_eq!(duration, Duration::from_secs(1));Sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Converts the timestamp to seconds as a floating-point value.
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(45000, Rational::new(1, 90000));
assert!((ts.as_secs_f64() - 0.5).abs() < 0.0001);Sourcepub fn as_millis(&self) -> i64
pub fn as_millis(&self) -> i64
Converts the timestamp to milliseconds.
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(90000, Rational::new(1, 90000));
assert_eq!(ts.as_millis(), 1000);Sourcepub fn as_micros(&self) -> i64
pub fn as_micros(&self) -> i64
Converts the timestamp to microseconds.
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(90, Rational::new(1, 90000));
assert_eq!(ts.as_micros(), 1000); // 90/90000 = 0.001 sec = 1000 microsecondsSourcepub fn as_frame_number(&self, fps: f64) -> u64
pub fn as_frame_number(&self, fps: f64) -> u64
Converts the timestamp to a frame number at the given frame rate.
§Arguments
fps- The frame rate (frames per second)
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(90000, Rational::new(1, 90000)); // 1 second
assert_eq!(ts.as_frame_number(30.0), 30); // 30 fps
assert_eq!(ts.as_frame_number(60.0), 60); // 60 fpsSourcepub fn as_frame_number_rational(&self, fps: Rational) -> u64
pub fn as_frame_number_rational(&self, fps: Rational) -> u64
Converts the timestamp to a frame number using a rational frame rate.
§Arguments
fps- The frame rate as a rational number
§Examples
use ff_format::{Rational, Timestamp};
let ts = Timestamp::new(90000, Rational::new(1, 90000)); // 1 second
let fps = Rational::new(30000, 1001); // 29.97 fps
let frame = ts.as_frame_number_rational(fps);
assert!(frame == 29 || frame == 30); // Should be approximately 30Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this timestamp is zero.
§Examples
use ff_format::{Rational, Timestamp};
let zero = Timestamp::zero(Rational::new(1, 90000));
assert!(zero.is_zero());
let non_zero = Timestamp::new(100, Rational::new(1, 90000));
assert!(!non_zero.is_zero());Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true if this timestamp is negative.
§Examples
use ff_format::{Rational, Timestamp};
let negative = Timestamp::new(-100, Rational::new(1, 90000));
assert!(negative.is_negative());