Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ exclude = [
]

[dependencies]
libc = "0.1"
delegate = "0.1"
chrono = "0.3"
libc = "0.2"
delegate = "0.13"
chrono = "0.4"

[build-dependencies.pkg-config]
version = "0.3.9"
version = "0.3"
9 changes: 6 additions & 3 deletions examples/convenience_api.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
extern crate mediainfo;

use mediainfo::MediaInfoWrapper;
use std::path::PathBuf;
use std::{env::current_dir, path::PathBuf};

fn main() {
let mut media_info = MediaInfoWrapper::new();
let sample_path = PathBuf::from("../samples");
let sample_path = PathBuf::from("samples");
let extnames = ["mp3", "m4a", "flac"];

for ext in extnames.iter() {
let filename = sample_path.join(format!("sample.{}", ext));

media_info.open(&filename).expect("It should open the file.");
println!("Filename: {}", filename.to_str().as_ref().unwrap());
println!("{}\n", media_info.codec().unwrap());
println!("{:?}\n", media_info.duration().unwrap());

let _ = media_info.option("output", "JSON");
println!("{}\n", media_info.inform().unwrap());

media_info.close();
}
Expand Down
14 changes: 11 additions & 3 deletions examples/inform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ use std::path::PathBuf;

fn main() {
let mut media_info = MediaInfo::new();
let sample_path = PathBuf::from("../samples");

let sample_path = PathBuf::from("samples");
let extnames = ["mp3", "m4a", "flac"];

for ext in extnames.iter() {
let filename = sample_path.join(format!("sample.{}", ext));

media_info.open(&filename).expect("It should open the file.");
println!("Filename: {}", filename.to_str().as_ref().unwrap());
media_info
.open(&filename)
.expect("It should open the file.");

let result = media_info.option("output", "JSON");
println!("option result: {:?}", result);

println!("{}\n", media_info.inform().unwrap());

// println!("Filename: {}", filename.to_str().as_ref().unwrap());

media_info.close();
}
}
87 changes: 56 additions & 31 deletions src/convenience_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use ffi::{MediaInfo, MediaInfoResult, MediaInfoStream};
use streams::{GeneralStream, VideoStream, AudioStream, ImageStream, MenuStream, OtherStream, TextStream};
use streams::{
AudioStream, GeneralStream, ImageStream, MenuStream, OtherStream, TextStream, VideoStream,
};

use chrono::{UTC, DateTime};
use std::rc::Rc;
use std::cell::RefCell;
use chrono::{DateTime, Utc};
use std::path::Path;
use std::time::Duration;
use std::rc::Rc;
use std::{cell::RefCell, time::Duration};

pub struct MediaInfoWrapper {
general_stream: GeneralStream,
Expand Down Expand Up @@ -48,16 +49,20 @@ impl MediaInfoWrapper {
Ok(r) => {
self.wrap_streams();
Ok(r)
},
}
Err(r) => Err(r),
}
}

pub fn open_data(&mut self, data: &[u8]) -> Result<(), String>{
pub fn open_data(&mut self, data: &[u8]) -> Result<(), String> {
let data_len = data.len();
if data_len == 0 { return Err("Data length is 0".to_string()); }
if data_len == 0 {
return Err("Data length is 0".to_string());
}

self.handle.borrow_mut().open_buffer_init(data_len as u64, 0);
self.handle
.borrow_mut()
.open_buffer_init(data_len as u64, 0);
let continue_result = self.handle.borrow_mut().open_buffer_continue(data);
let finalize_result = self.handle.borrow_mut().open_buffer_finalize();

Expand All @@ -70,6 +75,14 @@ impl MediaInfoWrapper {
Ok(())
}

pub fn option(&mut self, parameter: &str, value: &str) -> MediaInfoResult<String> {
self.handle.borrow_mut().option(parameter, value)
}

pub fn inform(&mut self) -> MediaInfoResult<String> {
self.handle.borrow_mut().inform()
}

pub fn close(&mut self) {
self.general_stream.handler = None;
self.video_streams = None;
Expand All @@ -94,9 +107,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.video_streams = Some(streams);
},
}
MediaInfoStream::Audio => {
let mut streams = Vec::new();
for i in 0..self.handle.borrow_mut().count_get(stype) {
Expand All @@ -105,9 +118,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.audio_streams = Some(streams);
},
}
MediaInfoStream::Text => {
let mut streams = Vec::new();
for i in 0..self.handle.borrow_mut().count_get(stype) {
Expand All @@ -116,9 +129,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.text_streams = Some(streams);
},
}
MediaInfoStream::Other => {
let mut streams = Vec::new();
for i in 0..self.handle.borrow_mut().count_get(stype) {
Expand All @@ -127,9 +140,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.other_streams = Some(streams);
},
}
MediaInfoStream::Image => {
let mut streams = Vec::new();
for i in 0..self.handle.borrow_mut().count_get(stype) {
Expand All @@ -138,9 +151,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.image_streams = Some(streams);
},
}
MediaInfoStream::Menu => {
let mut streams = Vec::new();
for i in 0..self.handle.borrow_mut().count_get(stype) {
Expand All @@ -149,9 +162,9 @@ impl MediaInfoWrapper {
index: i,
handler: Rc::clone(&self.handle),
});
};
}
self.menu_streams = Some(streams);
},
}
_ => continue,
}
}
Expand Down Expand Up @@ -182,7 +195,7 @@ impl MediaInfoWrapper {
}

delegate! {
target self.general_stream {
to self.general_stream {
pub fn codec_id(&self) -> MediaInfoResult<String>;
pub fn duration(&self) -> MediaInfoResult<Duration>;
pub fn format(&self) -> MediaInfoResult<String>;
Expand All @@ -195,10 +208,10 @@ impl MediaInfoWrapper {
pub fn datasize(&self) -> MediaInfoResult<i64>;
pub fn footersize(&self) -> MediaInfoResult<i64>;
pub fn encoded_library(&self) -> MediaInfoResult<String>;
pub fn mastered_date(&self) -> MediaInfoResult<DateTime<UTC>>;
pub fn tagged_date(&self) -> MediaInfoResult<DateTime<UTC>>;
pub fn encoded_date(&self) -> MediaInfoResult<DateTime<UTC>>;
pub fn last_modification_date(&self) -> MediaInfoResult<DateTime<UTC>>;
pub fn mastered_date(&self) -> MediaInfoResult<DateTime<Utc>>;
pub fn tagged_date(&self) -> MediaInfoResult<DateTime<Utc>>;
pub fn encoded_date(&self) -> MediaInfoResult<DateTime<Utc>>;
pub fn last_modification_date(&self) -> MediaInfoResult<DateTime<Utc>>;
pub fn artist(&self) -> MediaInfoResult<String>;
pub fn performer(&self) -> MediaInfoResult<String>;
pub fn title(&self) -> MediaInfoResult<String>;
Expand All @@ -213,9 +226,9 @@ impl MediaInfoWrapper {
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use chrono::NaiveDate;
use std::fs;
use std::path::PathBuf;

#[test]
fn can_retrieve_general_information() {
Expand All @@ -229,13 +242,24 @@ mod tests {
assert_eq!(Duration::from_millis(5568), mw.duration().unwrap());
assert_eq!("MPEG-4", mw.format().unwrap());
assert_eq!("Base Media / Version 2", mw.format_profile().unwrap());
assert_eq!("MPEG-4", mw.codec().unwrap());
assert!(mw.codec().is_err(), "Codec should be empty");
assert_eq!(551194, mw.overall_bit_rate().unwrap());
assert_eq!("HandBrake 0.9.4 2009112300", mw.writing_application().unwrap());
assert_eq!(
"HandBrake 0.9.4 2009112300",
mw.writing_application().unwrap()
);
assert_eq!(160, mw.headersize().unwrap());
assert_eq!(379880, mw.datasize().unwrap());
assert_eq!(3591, mw.footersize().unwrap());
assert_eq!(DateTime::<UTC>::from_utc(NaiveDate::from_ymd(2010, 3, 20).and_hms(21, 29, 12), UTC), mw.tagged_date().unwrap());
assert_eq!(
DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDate::from_ymd_opt(2010, 3, 20)
.and_then(|d| d.and_hms_opt(21, 29, 12))
.unwrap(),
Utc
),
mw.tagged_date().unwrap()
);
mw.close();
}

Expand All @@ -245,7 +269,8 @@ mod tests {
let filename = sample_path.join("sample.mp4");
let mut mw = MediaInfoWrapper::new();
let contents = fs::read(filename).expect("File not found.");
mw.open_data(contents.as_slice()).expect("Could not read from buffer.");
mw.open_data(contents.as_slice())
.expect("Could not read from buffer.");

assert_eq!("mp42", mw.codec_id().unwrap());
}
Expand Down
18 changes: 8 additions & 10 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use ::c_w_string::CWcharString;
use std::ffi::CString;
use std::path::Path;

type uint64 = libc::uint64_t;
type uint8 = libc::uint8_t;
type uint64 = u64;
type uint8 = u8;
type size_t = libc::size_t;
type wchar = libc::wchar_t;
type c_char = libc::c_char;
Expand Down Expand Up @@ -114,12 +114,11 @@ impl MediaInfo {
if param_w_string.is_err(){ return Err(MediaInfoError::RustToCStringError); }
if value_w_string.is_err(){ return Err(MediaInfoError::RustToCStringError); }

let param_ptr = param_w_string.unwrap().as_raw();
let value_ptr = value_w_string.unwrap().as_raw();

// TODO(erick): Do we need to free this memory? I could not
// find this information on the documentation.
let result_ptr = MediaInfo_Option(self.handle, param_ptr, value_ptr);
let result_ptr = MediaInfo_Option(self.handle,
param_w_string.unwrap().as_raw(),
value_w_string.unwrap().as_raw());
let result_c_string = CWcharString::from_raw_to_c_string(result_ptr);
if result_c_string.is_err() { return Err(MediaInfoError::CToRustError); }

Expand Down Expand Up @@ -165,12 +164,11 @@ impl MediaInfo {
let param_w_string = CWcharString::from_str(parameter);
if param_w_string.is_err(){ return Err(MediaInfoError::RustToCStringError); }

let param_ptr = param_w_string.unwrap().as_raw();

// TODO(erick): Do we need to free this memory? I could not
// find this information on the documentation.
let result_ptr = MediaInfo_Get(self.handle, info_stream.c_compatible(),
stream_number as size_t, param_ptr,
stream_number as size_t,
param_w_string.unwrap().as_raw(),
info_kind.c_compatible(),
search_kind.c_compatible());
let result_c_string = CWcharString::from_raw_to_c_string(result_ptr);
Expand Down Expand Up @@ -199,7 +197,7 @@ impl MediaInfo {
let bytes_ptr = &data[0] as *const uint8;
let result = MediaInfo_Open_Buffer_Continue(self.handle,
bytes_ptr,
data.len() as uint64);
data.len() as size_t);
result as usize
}
}
Expand Down
Loading