Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M9.15: import .vob/.mod/.tod/.m2t/.m2v/.m1v/.mpv video and standalone .mp2 audio

Brandon Thomas37008cab5ee2
diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs
index 3cf7f400f..7954473d1 100644
--- a/crates/codecs/src/audio.rs
+++ b/crates/codecs/src/audio.rs
@@ -598,7 +598,7 @@ impl MediaSource for AudioFileSource {
pub fn opener(name: &str, bytes: Arc<[u8]>) -> Option<std::result::Result<SharedSource, MediaError>> {
let ext = name.rsplit('.').next().unwrap_or("").to_ascii_lowercase();
- if !matches!(ext.as_str(), "mp3" | "flac" | "ogg" | "oga" | "aif" | "aiff" | "aifc") {
+ if !matches!(ext.as_str(), "mp3" | "mp2" | "flac" | "ogg" | "oga" | "aif" | "aiff" | "aifc") {
return None;
}
Some(AudioFileSource::decode(name, bytes).map(|s| Arc::new(s) as SharedSource).map_err(Into::into))
diff --git a/crates/codecs/tests/mpeg_oracle.rs b/crates/codecs/tests/mpeg_oracle.rs
index 5cdc7b92c..5fdbc0bfc 100644
--- a/crates/codecs/tests/mpeg_oracle.rs
+++ b/crates/codecs/tests/mpeg_oracle.rs
@@ -141,6 +141,7 @@ fn spec(name: &str) -> Vec<String> {
"mkv_mpeg2.mkv" => {
cat(&[&["-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25"], NOISE, &["-t", "0.6", "-c:v", "mpeg2video", "-bf", "2", "-g", "6", "-c:a", "mp2"]])
}
+ "audio.mp2" => cat(&[NOISE, &["-t", "1", "-c:a", "mp2", "-b:a", "224k", "-f", "mp2"]]),
"es_mpeg2.m2v" => {
cat(&[&["-f", "lavfi", "-i", "testsrc2=size=352x288:rate=25"], &["-t", "1", "-c:v", "mpeg2video", "-bf", "2", "-g", "9", "-f", "mpeg2video"]])
}
@@ -162,6 +163,7 @@ pub const ALL: &[&str] = &[
"mov_xdcam_hd422.mov",
"mp4_mpeg2.mp4",
"mkv_mpeg2.mkv",
+ "audio.mp2",
"es_mpeg2.m2v",
];
@@ -450,6 +452,20 @@ fn mpeg2_in_quicktime_mp4_and_matroska() {
}
}
+#[test]
+fn standalone_mp2_file() {
+ let ff = filmcraft_testkit::require_ffmpeg!();
+ let f = make(&ff, "audio.mp2");
+ let src = filmcraft_codecs::open_bytes("audio.mp2", bytes(&f)).unwrap();
+ let a = src.info().audio.clone().unwrap();
+ assert_eq!((a.sample_rate, a.channels), (48_000, 2));
+ let want = ffmpeg_audio_f32(&ff, &f, &[]);
+ let n = want.len() / 2;
+ let buf = src.audio(0, n, 48_000).unwrap();
+ let worst = (0..n).map(|i| (buf.channels[0][i] - want[2 * i]).abs().max((buf.channels[1][i] - want[2 * i + 1]).abs())).fold(0f32, f32::max);
+ assert!(worst <= 2e-4, "{worst}");
+}
+
#[test]
fn truncated_and_corrupt_files_never_panic() {
let ff = filmcraft_testkit::require_ffmpeg!();
diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs
index 7a2e68efb..427e47b33 100644
--- a/crates/media/src/lib.rs
+++ b/crates/media/src/lib.rs
@@ -133,8 +133,9 @@ pub type SharedSource = Arc<dyn MediaSource>;
/// File extensions we recognise at import (the media browser filters on these).
pub const STILL_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp"];
-pub const AUDIO_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "mp3", "m4a", "aac", "flac", "ogg", "opus"];
-pub const VIDEO_EXTENSIONS: &[&str] = &["mp4", "m4v", "mov", "mkv", "webm", "avi", "mxf", "mts", "m2ts", "ts", "mpg", "mpeg", "3gp", "y4m"];
+pub const AUDIO_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "mp3", "mp2", "m4a", "aac", "flac", "ogg", "opus"];
+pub const VIDEO_EXTENSIONS: &[&str] =
+ &["mp4", "m4v", "mov", "mkv", "webm", "avi", "mxf", "mts", "m2ts", "m2t", "ts", "mpg", "mpeg", "vob", "mod", "tod", "m2v", "m1v", "mpv", "3gp", "y4m"];
pub fn is_importable(path: &Path) -> bool {
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();