Struct vtc::Framerate

source ·
pub struct Framerate { /* private fields */ }
Expand description

The rate at which a video file frames are played back.

Framerate is measured in frames-per-second (24/1 = 24 frames-per-second).

Implementations§

source§

impl Framerate

source

pub fn playback(&self) -> Rational64

The rational representation of the real-world playback speed as a fraction in frames-per-second.

Examples
use vtc::{Framerate, Ntsc};
use num::Rational64;
let rate = Framerate::with_timebase("24/1", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback())
source

pub fn timebase(&self) -> Rational64

The rational representation of the timecode timebase speed as a fraction in frames-per-second.

Examples
use vtc::{Framerate, Ntsc};
use num::Rational64;
let rate = Framerate::with_playback("24000/1001", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24, 1), rate.timebase())
source

pub fn ntsc(&self) -> Ntsc

Whether this is an NTSC-style time base (aka 23.98, 24000/1001, etc). Returns an enum detailing if it is not NTSC or what type of NTSC flavor it is.

Examples
use vtc::{Framerate, Ntsc};
let rate = Framerate::with_playback("24000/1001", Ntsc::NonDropFrame).unwrap();
assert_eq!(Ntsc::NonDropFrame, rate.ntsc())
source

pub fn with_playback<T: FramerateSource>( rate: T, ntsc: Ntsc ) -> FramerateParseResult

Creates a new Framerate with a given real-world media playback value measured in frames-per-second.

Arguments
  • rate - A value that represents playback frames-per-second.

  • ntsc - The ntsc standard this value should be parsed as.

Examples

We can generate any NTSC framerates from f32 or f64 values easily.

use vtc::{Framerate, FramerateSource, Ntsc};
use num::Rational64;
let rate = Framerate::with_playback(23.98, Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

Floats are automatically rounded to the nearest valid NTSC playback speed:

let rate = Framerate::with_playback(23.5, Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

&str and String can be parsed if they are a float or rational format:

let rate = Framerate::with_playback("23.98", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());
let rate = Framerate::with_playback("24000/1001", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

Non-valid NTSC playback rates will result in an error if we are parsing NTSC drop or non-drop values:

let err = Framerate::with_playback("24/1", Ntsc::NonDropFrame);
assert_eq!(FramerateParseError::Ntsc("ntsc framerates must be n/1001".to_string()), err.err().unwrap());

This means that integers will always result in an error if ntsc != Ntsc::None:

let err = Framerate::with_playback(24, Ntsc::NonDropFrame);
assert!(err.is_err());

If we switch our NTSC settings to Ntsc::None, we can parse integers and integer strings, as well as other arbirary playback speed values:

let rate = Framerate::with_playback(24, Ntsc::None).unwrap();
assert_eq!(Rational64::new(24, 1), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::None, rate.ntsc());
let rate = Framerate::with_playback("24", Ntsc::None).unwrap();
assert_eq!(Rational64::new(24, 1), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::None, rate.ntsc());
let rate = Framerate::with_playback("3/1", Ntsc::None).unwrap();
assert_eq!(Rational64::new(3, 1), rate.playback());
assert_eq!(Rational64::new(3, 1), rate.timebase());
assert_eq!(Ntsc::None, rate.ntsc());

If we try to parse a non-drop-frame NTSC value with the wrong timbebase we will get an error:

let err = Framerate::with_playback(23.98, Ntsc::DropFrame);
assert_eq!(FramerateParseError::DropFrame("dropframe must have playback divisible by 30000/1001 (multiple of 29.97)".to_string()), err.err().unwrap());

For more information on why drop-frame timebases must be a multiple of 30000/1001, see this blogpost.

note

Using a float with Ntsc::None will result in an error. Floats are not precise, and without the ntsc flag, vtc cannot know exactly what framerate you want. A Rational64 value must be used.

source

pub fn with_timebase<T: FramerateSource>( base: T, ntsc: Ntsc ) -> FramerateParseResult

Creates a new Framerate with a given timecode timebase playback value measured in frames-per-second. For NTSC framerates, the timebase will differ from the playback.

Arguments
  • base - A value that represents timebase frames-per-second.

  • ntsc - The ntsc standard this value should be parsed as.

Examples

We can generate any NTSC framerates from any non 128-bit integer type easily:

use vtc::{Framerate, FramerateSource, Ntsc};
use num::Rational64;
let rate = Framerate::with_timebase(24, Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

Floats are automatically rounded to the nearest valid NTSC timebase speed:

let rate = Framerate::with_timebase(24.0, Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

&str and String can be parsed if they are an int, float or rational format:

let rate = Framerate::with_timebase("24", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());
let rate = Framerate::with_timebase("24.0", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());
let rate = Framerate::with_timebase("24/1", Ntsc::NonDropFrame).unwrap();
assert_eq!(Rational64::new(24000, 1001), rate.playback());
assert_eq!(Rational64::new(24, 1), rate.timebase());
assert_eq!(Ntsc::NonDropFrame, rate.ntsc());

Non-valid NTSC timebase will result in an error if we are parsing NTSC drop or non-drop values:

let err = Framerate::with_timebase("24000/1001", Ntsc::NonDropFrame);
assert_eq!(
    FramerateParseError::Ntsc("ntsc timebases must be whole numbers".to_string()),
    err.err().unwrap(),
);

If we switch our NTSC settings, we can parse arbirary Framerate values:

let rate = Framerate::with_timebase("3/1", Ntsc::None).unwrap();
assert_eq!(Rational64::new(3, 1), rate.playback());
assert_eq!(Rational64::new(3, 1), rate.timebase());
assert_eq!(Ntsc::None, rate.ntsc());

If we try to parse a drop-frame value with the wrong timbebase we will get an error:

let err = Framerate::with_timebase("24", Ntsc::DropFrame);
assert_eq!(
    FramerateParseError::DropFrame("dropframe must have timebase divisible by 30 (multiple of 29.97)".to_string()),
    err.err().unwrap(),
);

For more information on why drop-frame timebases must be a multiple of 30, see this blogpost.

Trait Implementations§

source§

impl Clone for Framerate

source§

fn clone(&self) -> Framerate

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Framerate

source§

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

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

impl Display for Framerate

source§

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

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

impl PartialEq<Framerate> for Framerate

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Framerate

source§

impl Eq for Framerate

source§

impl StructuralEq for Framerate

source§

impl StructuralPartialEq for Framerate

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.