pub struct MediaParser { /* private fields */ }
Expand description
A MediaParser
/AsyncMediaParser
can parse media info from a
MediaSource
.
MediaParser
/AsyncMediaParser
manages inner parse buffers that can be
shared between multiple parsing tasks, thus avoiding frequent memory
allocations.
Therefore:
-
Try to reuse a
MediaParser
/AsyncMediaParser
instead of creating a new one every time you need it. -
MediaSource
should be created directly fromRead
, not fromBufRead
.
§Example
use nom_exif::*;
use chrono::DateTime;
let mut parser = MediaParser::new();
// ------------------- Parse Exif Info
let ms = MediaSource::file_path("./testdata/exif.heic").unwrap();
assert!(ms.has_exif());
let mut iter: ExifIter = parser.parse(ms).unwrap();
let entry = iter.next().unwrap();
assert_eq!(entry.tag().unwrap(), ExifTag::Make);
assert_eq!(entry.get_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::file_path("./testdata/meta.mov").unwrap();
assert!(ms.has_track());
let info: TrackInfo = parser.parse(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.get_gps_info().unwrap().latitude_ref, 'N');
assert_eq!(
info.get_gps_info().unwrap().latitude,
[(27, 1), (7, 1), (68, 100)].into(),
);
Implementations§
Source§impl MediaParser
impl MediaParser
pub fn new() -> Self
Sourcepub fn parse<R: Read, S, O: ParseOutput<R, S>>(
&mut self,
ms: MediaSource<R, S>,
) -> Result<O>
pub fn parse<R: Read, S, O: ParseOutput<R, S>>( &mut self, ms: MediaSource<R, S>, ) -> Result<O>
MediaParser
/AsyncMediaParser
comes with its own buffer management,
so that buffers can be reused during multiple parsing processes to
avoid frequent memory allocations. Therefore, try to reuse a
MediaParser
instead of creating a new one every time you need it.
Note:
-
For
ExifIter
as parse output, Please avoid holding theExifIter
object all the time and drop it immediately after use. Otherwise, the parsing buffer referenced by theExifIter
object will not be reused byMediaParser
, resulting in repeated memory allocation in the subsequent parsing process.If you really need to retain some data, please take out the required Entry values and save them, or convert the
ExifIter
into ancrate::Exif
object to retain all Entry values. -
For
TrackInfo
as parse output, you don’t need to worry about this, becauseTrackInfo
dosn’t reference the parsing buffer.