Skip to main content

MediaParser

Struct MediaParser 

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

A MediaParser can parse media info from a MediaSource.

MediaParser manages inner parse buffers that can be shared between multiple parsing tasks, thus avoiding frequent memory allocations.

Therefore:

  • Try to reuse a MediaParser instead of creating a new one every time you need it.

  • MediaSource should be created directly from Read, not from BufRead.

§Example

use nom_exif::*;
use chrono::DateTime;

let mut parser = MediaParser::new();

// ------------------- Parse Exif Info
let ms = MediaSource::open("./testdata/exif.heic").unwrap();
assert_eq!(ms.kind(), MediaKind::Image);
let mut iter = parser.parse_exif(ms).unwrap();

let entry = iter.next().unwrap();
assert!(matches!(entry.tag(), nom_exif::TagOrCode::Tag(ExifTag::Make)));
assert_eq!(entry.value().unwrap().as_str().unwrap(), "Apple");

// Convert `ExifIter` into an `Exif`. Clone it before converting, so that
// we can start the iteration from the beginning.
let exif: Exif = iter.clone().into();
assert_eq!(exif.get(ExifTag::Make).unwrap().as_str().unwrap(), "Apple");

// ------------------- Parse Track Info
let ms = MediaSource::open("./testdata/meta.mov").unwrap();
assert_eq!(ms.kind(), MediaKind::Track);
let info = parser.parse_track(ms).unwrap();

assert_eq!(info.get(TrackInfoTag::Make), Some(&"Apple".into()));
assert_eq!(info.get(TrackInfoTag::Model), Some(&"iPhone X".into()));
assert_eq!(info.get(TrackInfoTag::GpsIso6709), Some(&"+27.1281+100.2508+000.000/".into()));
assert_eq!(info.gps_info().unwrap().latitude_ref, LatRef::North);
assert_eq!(
    info.gps_info().unwrap().latitude,
    LatLng::new(URational::new(27, 1), URational::new(7, 1), URational::new(4116, 100)),
);

Implementations§

Source§

impl MediaParser

Source

pub async fn parse_exif_async<R: AsyncRead + Unpin + Send>( &mut self, ms: AsyncMediaSource<R>, ) -> Result<ExifIter>

Parse Exif metadata from an async image source. Returns Error::ExifNotFound if the source is a Track.

Source

pub async fn parse_track_async<R: AsyncRead + Unpin + Send>( &mut self, ms: AsyncMediaSource<R>, ) -> Result<TrackInfo>

Parse track info from an async video/audio source. Returns Error::TrackNotFound if the source is an Image.

Source§

impl MediaParser

Source

pub fn new() -> Self

Source

pub fn parse_exif<R: Read>(&mut self, ms: MediaSource<R>) -> Result<ExifIter>

Parse Exif metadata from an image source. Returns Error::ExifNotFound if the source is a Track (use Self::parse_track instead).

MediaParser reuses its internal parse buffer across calls, so prefer reusing a single MediaParser over creating a new one per file. Drop the returned ExifIter (or convert it into crate::Exif) before the next parse_* call so the buffer can be reclaimed.

Source

pub fn parse_track<R: Read>(&mut self, ms: MediaSource<R>) -> Result<TrackInfo>

Parse track info from a video/audio source.

In v3.1, this also accepts JPEG images that carry an embedded Pixel/Google Motion Photo trailer: when ExifIter::has_embedded_track returned true for such a JPEG, calling parse_track on the same source extracts the embedded MP4’s metadata. Other image formats without an embedded track return crate::Error::TrackNotFound.

Source

pub fn parse_exif_from_bytes(&mut self, ms: MediaSource<()>) -> Result<ExifIter>

Parse Exif metadata from an in-memory byte payload built via MediaSource::<()>::from_bytes. Returns Error::ExifNotFound if the payload is a Track (use Self::parse_track_from_bytes instead).

Memory-mode parsing is zero-copy: the underlying Bytes is shared with the returned ExifIter (and its sub-IFDs / CR3 CMT blocks) via reference counting. No Vec<u8> is allocated for the parse buffer.

Source

pub fn parse_track_from_bytes( &mut self, ms: MediaSource<()>, ) -> Result<TrackInfo>

Parse track info from an in-memory video/audio byte payload built via MediaSource::<()>::from_bytes. Returns Error::TrackNotFound if the payload is an Image (use Self::parse_exif_from_bytes instead).

Like Self::parse_exif_from_bytes, the parse is zero-copy with respect to the user-supplied Bytes.

Trait Implementations§

Source§

impl Debug for MediaParser

Source§

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

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

impl Default for MediaParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

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

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more