Update upload.rs
This commit is contained in:
1 parent
9f0681563e
commit
fe918433e8
1 file changed
+66
-33
+66
-33
@@ -259,47 +259,80 @@ enum FileType {
|
|||||||
|
|
||||||
// Function to extract duration from audio/video files
|
// Function to extract duration from audio/video files
|
||||||
async fn extract_duration(filepath: &str) -> Option<f64> {
|
async fn extract_duration(filepath: &str) -> Option<f64> {
|
||||||
|
use std::panic;
|
||||||
use symphonia::core::io::MediaSourceStream;
|
use symphonia::core::io::MediaSourceStream;
|
||||||
|
|
||||||
let file = match std::fs::File::open(filepath) {
|
// Wrap the Symphonia call in catch_unwind to prevent panics
|
||||||
Ok(file) => file,
|
let result = panic::catch_unwind(|| {
|
||||||
Err(_) => return None,
|
// Ensure the file is fully written before trying to read it
|
||||||
};
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||||
|
|
||||||
let mss = MediaSourceStream::new(Box::new(file), Default::default());
|
// Open the file using std::fs::File to avoid async file issues with Symphonia
|
||||||
|
let file = match std::fs::File::open(filepath) {
|
||||||
let mut hint = Hint::new();
|
Ok(file) => file,
|
||||||
if let Some(ext) = Path::new(filepath).extension().and_then(|ext| ext.to_str()) {
|
Err(e) => {
|
||||||
hint.with_extension(ext);
|
eprintln!("Error opening media file: {}", e);
|
||||||
}
|
return None;
|
||||||
|
}
|
||||||
let format_opts = FormatOptions::default();
|
|
||||||
let metadata_opts = MetadataOptions::default();
|
|
||||||
|
|
||||||
let probed =
|
|
||||||
match symphonia::default::get_probe().format(&hint, mss, &format_opts, &metadata_opts) {
|
|
||||||
Ok(probed) => probed,
|
|
||||||
Err(_) => return None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let track = match probed
|
// Create a MediaSourceStream with proper buffering
|
||||||
.format
|
let mss = MediaSourceStream::new(Box::new(file), Default::default());
|
||||||
.tracks()
|
|
||||||
.iter()
|
|
||||||
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)
|
|
||||||
{
|
|
||||||
Some(track) => track,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let duration = track.codec_params.time_base.and_then(|tb| {
|
let mut hint = Hint::new();
|
||||||
track
|
if let Some(ext) = Path::new(filepath).extension().and_then(|ext| ext.to_str()) {
|
||||||
.codec_params
|
hint.with_extension(ext);
|
||||||
.n_frames
|
}
|
||||||
.and_then(|frames| Some(tb.calc_time(frames).seconds as f64))
|
|
||||||
|
// Configure format options to be more permissive with EBML formats like WebM
|
||||||
|
let format_opts = FormatOptions {
|
||||||
|
enable_gapless: true,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let metadata_opts = MetadataOptions::default();
|
||||||
|
|
||||||
|
let probed = match symphonia::default::get_probe().format(&hint, mss, &format_opts, &metadata_opts) {
|
||||||
|
Ok(probed) => probed,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error probing media file: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Try to get the duration from the format reader's capabilities
|
||||||
|
// Note: Symphonia doesn't have a direct duration() method, so we'll skip this approach
|
||||||
|
// and rely on track-based duration calculation
|
||||||
|
|
||||||
|
// Fallback: try to get duration from track parameters
|
||||||
|
let track = match probed
|
||||||
|
.format
|
||||||
|
.tracks()
|
||||||
|
.iter()
|
||||||
|
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)
|
||||||
|
{
|
||||||
|
Some(track) => track,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Try to get duration from track's time base and frame count
|
||||||
|
let duration = track.codec_params.time_base.and_then(|tb| {
|
||||||
|
track
|
||||||
|
.codec_params
|
||||||
|
.n_frames
|
||||||
|
.and_then(|frames| Some(tb.calc_time(frames).seconds as f64))
|
||||||
|
});
|
||||||
|
|
||||||
|
duration
|
||||||
});
|
});
|
||||||
|
|
||||||
duration
|
match result {
|
||||||
|
Ok(duration) => duration,
|
||||||
|
Err(panic_info) => {
|
||||||
|
eprintln!("Symphonia panic caught: {:?}", panic_info);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn determine_file_type(content_type: &str, filename: &str) -> Option<FileType> {
|
fn determine_file_type(content_type: &str, filename: &str) -> Option<FileType> {
|
||||||
|
|||||||
Reference in new issue
Block a user