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) -> Timestamp
pub const fn new(pts: i64, time_base: Rational) -> Timestamp
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) -> Timestamp
pub const fn zero(time_base: Rational) -> Timestamp
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) -> Timestamp
pub fn from_duration(duration: Duration, time_base: Rational) -> Timestamp
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) -> Timestamp
pub fn from_secs_f64(secs: f64, time_base: Rational) -> Timestamp
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) -> Timestamp
pub fn from_millis(millis: i64, time_base: Rational) -> Timestamp
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());Sourcepub const fn is_valid(&self) -> bool
pub const fn is_valid(&self) -> bool
Returns true if this timestamp represents a real PTS value.
Returns false when the timestamp was constructed via invalid,
which corresponds to FFmpeg’s AV_NOPTS_VALUE.
§Examples
use ff_format::{Timestamp, Rational};
let valid = Timestamp::new(1000, Rational::new(1, 48000));
assert!(valid.is_valid());
let invalid = Timestamp::invalid();
assert!(!invalid.is_valid());Trait Implementations§
impl Copy for Timestamp
impl Eq for Timestamp
Source§impl Ord for Timestamp
impl Ord for Timestamp
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Timestamp
impl PartialOrd for Timestamp
Auto Trait Implementations§
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnsafeUnpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.