Add function to get audio file duration
This commit is contained in:
parent
3338cc2662
commit
79ba191415
33
src/util/audio.rs
Normal file
33
src/util/audio.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use symphonia::core::formats::FormatOptions;
|
||||||
|
use symphonia::core::io::MediaSourceStream;
|
||||||
|
use symphonia::core::meta::MetadataOptions;
|
||||||
|
use symphonia::core::probe::Hint;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
/// Measure the duration (in seconds) of an audio file
|
||||||
|
pub fn measure_duration(file: File) -> Result<u64, Box<dyn std::error::Error>> {
|
||||||
|
let source_stream = MediaSourceStream::new(Box::new(file), Default::default());
|
||||||
|
|
||||||
|
let hint = Hint::new();
|
||||||
|
let format_opts = FormatOptions::default();
|
||||||
|
let metadata_opts = MetadataOptions::default();
|
||||||
|
|
||||||
|
let probe = symphonia::default::get_probe().format(&hint, source_stream, &format_opts, &metadata_opts)?;
|
||||||
|
let reader = probe.format;
|
||||||
|
|
||||||
|
if reader.tracks().len() != 1 {
|
||||||
|
return Err(format!("Expected 1 track, found {}", reader.tracks().len()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
let track = &reader.tracks()[0];
|
||||||
|
|
||||||
|
let time_base = track.codec_params.time_base.ok_or("Missing time base")?;
|
||||||
|
let duration = track.codec_params.n_frames
|
||||||
|
.map(|frames| track.codec_params.start_ts + frames)
|
||||||
|
.ok_or("Missing number of frames")?;
|
||||||
|
|
||||||
|
duration
|
||||||
|
.checked_mul(time_base.numer as u64)
|
||||||
|
.and_then(|v| v.checked_div(time_base.denom as u64))
|
||||||
|
.ok_or("Overflow while computing duration".into())
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
|
cfg_if! {
|
||||||
|
if #[cfg(feature = "ssr")] {
|
||||||
|
pub mod audio;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user