80 lines
1.9 KiB
Rust
80 lines
1.9 KiB
Rust
use std::error::Error;
|
|
use std::fs::OpenOptions;
|
|
use std::path::Path;
|
|
use std::io;
|
|
use std::fmt;
|
|
use std::ffi::OsStr;
|
|
use symphonia::default;
|
|
use symphonia::core::io::MediaSourceStream;
|
|
use symphonia::core::probe::{Hint, ProbeResult};
|
|
|
|
pub fn load_song(path: &Path) -> Result<ProbeResult, LoadError> {
|
|
let codecs = default::get_codecs();
|
|
let probe = default::get_probe();
|
|
|
|
let file = OpenOptions::new()
|
|
.read(true)
|
|
.write(false)
|
|
.open(path)
|
|
.map_err(LoadError::OpenError)?;
|
|
let media_source_stream = MediaSourceStream::new(Box::new(file), Default::default());
|
|
|
|
let ext_hint = path.extension()
|
|
.and_then(OsStr::to_str)
|
|
.map(|ext| {
|
|
let mut h = Hint::new();
|
|
h.with_extension(ext);
|
|
h
|
|
})
|
|
.unwrap_or_else(Hint::new);
|
|
|
|
probe.format(
|
|
&ext_hint,
|
|
media_source_stream,
|
|
&Default::default(),
|
|
&Default::default(),
|
|
).map_err(LoadError::SymphoniaError)
|
|
}
|
|
|
|
pub fn extract_cover(ProbeResult { format, metadata }: &mut ProbeResult) {
|
|
if let Some(metadata) = metadata.get() {
|
|
if let Some(current) = metadata.current() {
|
|
for tag in current.tags() {
|
|
println!("Probed Tag: {}", tag);
|
|
}
|
|
println!("{} Visuals in Probed Metadata", current.visuals().len());
|
|
} else {
|
|
println!("No current probed metadata");
|
|
}
|
|
} else {
|
|
println!("No probed metadata");
|
|
}
|
|
|
|
let normal_metadata = format.metadata();
|
|
if let Some(current) = normal_metadata.current() {
|
|
for tag in current.tags() {
|
|
println!("Probed Tag: {}", tag);
|
|
}
|
|
println!("{} Visuals in Normal Metadata", current.visuals().len());
|
|
} else {
|
|
println!("No current normal metadata");
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum LoadError {
|
|
OpenError(io::Error),
|
|
SymphoniaError(symphonia::core::errors::Error),
|
|
}
|
|
|
|
impl fmt::Display for LoadError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
match self {
|
|
Self::OpenError(e) => write!(f, "Problem opening song: {}", e),
|
|
Self::SymphoniaError(e) => write!(f, "Symphonia encountered a problem: {}", e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error for LoadError {}
|