Skip to main content

Timecode

Struct Timecode 

Source
pub struct Timecode { /* private fields */ }
Expand description

A position within one 24-hour timecode day at a FrameRate.

The position is a frame count from 00:00:00:00, so arithmetic is exact and drop-frame labels only matter when parsing and formatting. Arithmetic wraps at midnight in both directions.

use smpte_timecode::{FrameRate, Timecode};

let start = Timecode::parse("00:00:59;29", FrameRate::FPS_29_97_DF)?;
assert_eq!(start.frames(), 1799);
assert_eq!(start.wrapping_add_frames(1).to_string(), "00:01:00;02");

Implementations§

Source§

impl Timecode

Source

pub const fn from_frames( frames: u32, rate: FrameRate, ) -> Result<Self, TimecodeError>

The position frames frames after 00:00:00:00.

§Errors

TimecodeError::FrameOutOfRange when frames is not below FrameRate::frames_per_day.

Source

pub const fn from_fields( fields: Fields, rate: FrameRate, ) -> Result<Self, TimecodeError>

The position that fields label at rate.

§Errors

TimecodeError::FieldOutOfRange for a field outside its range and TimecodeError::DroppedLabel for a label drop-frame counting skips.

Source

pub const fn parse(text: &str, rate: FrameRate) -> Result<Self, TimecodeError>

Parses HH:MM:SS:FF. Drop-frame rates also accept HH:MM:SS;FF. The frame field has two or three digits. Surrounding whitespace is not accepted.

§Errors

TimecodeError::InvalidSyntax for any other shape, TimecodeError::DropFrameSeparator for ; at a non-drop-frame rate, and the errors of Timecode::from_fields.

Source

pub const fn from_seconds( seconds: Ratio, rate: FrameRate, rounding: Rounding, ) -> Result<Self, TimecodeError>

The position at seconds after midnight, rounded to a whole frame.

§Errors

TimecodeError::FrameOutOfRange when the rounded frame falls outside the day.

Source

pub const fn from_samples( samples: u64, sample_rate: Ratio, rate: FrameRate, rounding: Rounding, ) -> Result<Self, TimecodeError>

The position of sample samples at sample_rate samples per second, rounded to a whole frame. Pass a pulled sample rate from Pull::apply for pulled audio.

§Errors

TimecodeError::ZeroSampleRate for a zero sample_rate, TimecodeError::Overflow when the exact product does not fit in 128 bits, and TimecodeError::FrameOutOfRange when the rounded frame falls outside the day.

Source

pub const fn frames(self) -> u32

The frame count from 00:00:00:00.

Source

pub const fn rate(self) -> FrameRate

The frame rate.

Source

pub const fn fields(self) -> Fields

The label fields of this position.

Source

pub const fn wrapping_add_frames(self, delta: i64) -> Self

The position delta frames later, or earlier for a negative delta, wrapping at midnight.

Source

pub const fn to_seconds(self) -> Ratio

The exact time since midnight in seconds.

use smpte_timecode::{FrameRate, Ratio, Timecode};

let hour = Timecode::parse("01:00:00:00", FrameRate::FPS_23_976)?;
assert_eq!(hour.to_seconds(), Ratio::new(18_018, 5)?); // 3603.6 s
Source

pub const fn to_samples( self, sample_rate: Ratio, rounding: Rounding, ) -> Result<u64, TimecodeError>

The sample at this position for sample_rate samples per second, rounded to a whole sample. NTSC rates rarely land on a whole sample, so the rounding is part of the call.

use smpte_timecode::{FrameRate, Ratio, Rounding, Timecode};

let hour = Timecode::parse("01:00:00;00", FrameRate::FPS_29_97_DF)?;
let rate = Ratio::from_integer(48_000);
// Exactly 172 799 827.2 samples.
assert_eq!(hour.to_samples(rate, Rounding::Down)?, 172_799_827);
assert_eq!(hour.to_samples(rate, Rounding::Up)?, 172_799_828);
§Errors

TimecodeError::Overflow when the sample does not fit in u64.

Trait Implementations§

Source§

impl Clone for Timecode

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Timecode

Source§

impl Debug for Timecode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Timecode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Timecode

Source§

impl Hash for Timecode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Timecode

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Timecode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.