Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add file size and manifest version to segment metadata

Richie McIlroy34b224f7654f
diff --git a/crates/enc-avfoundation/src/segmented.rs b/crates/enc-avfoundation/src/segmented.rs
index 222ea7586..c8a0506b5 100644
--- a/crates/enc-avfoundation/src/segmented.rs
+++ b/crates/enc-avfoundation/src/segmented.rs
@@ -54,6 +54,7 @@ pub struct SegmentInfo {
pub path: PathBuf,
pub index: u32,
pub duration: Duration,
+ pub file_size: Option<u64>,
}
#[derive(Serialize)]
@@ -62,10 +63,15 @@ struct FragmentEntry {
index: u32,
duration: f64,
is_complete: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ file_size: Option<u64>,
}
+const MANIFEST_VERSION: u32 = 2;
+
#[derive(Serialize)]
struct Manifest {
+ version: u32,
fragments: Vec<FragmentEntry>,
#[serde(skip_serializing_if = "Option::is_none")]
total_duration: Option<f64>,
@@ -147,10 +153,15 @@ impl SegmentedMP4Encoder {
sync_file(&completed_segment_path);
+ let file_size = std::fs::metadata(&completed_segment_path)
+ .ok()
+ .map(|m| m.len());
+
self.completed_segments.push(SegmentInfo {
path: completed_segment_path,
index: self.current_index,
duration: segment_duration,
+ file_size,
});
self.write_manifest();
@@ -182,6 +193,7 @@ impl SegmentedMP4Encoder {
fn write_manifest(&self) {
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -195,6 +207,7 @@ impl SegmentedMP4Encoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: None,
@@ -224,6 +237,7 @@ impl SegmentedMP4Encoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect();
@@ -237,9 +251,11 @@ impl SegmentedMP4Encoder {
index: self.current_index,
duration: 0.0,
is_complete: false,
+ file_size: None,
});
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments,
total_duration: None,
is_complete: false,
@@ -267,20 +283,25 @@ impl SegmentedMP4Encoder {
}
pub fn finish(&mut self, timestamp: Option<Duration>) -> Result<(), FinishError> {
- if let Some(segment_start) = self.segment_start_time {
- let final_duration = timestamp
- .unwrap_or(segment_start)
- .saturating_sub(segment_start);
-
- self.completed_segments.push(SegmentInfo {
- path: self.current_segment_path(),
- index: self.current_index,
- duration: final_duration,
- });
- }
+ let segment_path = self.current_segment_path();
+ let segment_start = self.segment_start_time;
if let Some(mut encoder) = self.current_encoder.take() {
encoder.finish(timestamp)?;
+
+ sync_file(&segment_path);
+
+ if let Some(start) = segment_start {
+ let final_duration = timestamp.unwrap_or(start).saturating_sub(start);
+ let file_size = std::fs::metadata(&segment_path).ok().map(|m| m.len());
+
+ self.completed_segments.push(SegmentInfo {
+ path: segment_path,
+ index: self.current_index,
+ duration: final_duration,
+ file_size,
+ });
+ }
}
self.finalize_manifest();
@@ -292,6 +313,7 @@ impl SegmentedMP4Encoder {
let total_duration: Duration = self.completed_segments.iter().map(|s| s.duration).sum();
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -305,6 +327,7 @@ impl SegmentedMP4Encoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: Some(total_duration.as_secs_f64()),
diff --git a/crates/enc-ffmpeg/src/mux/segmented_audio.rs b/crates/enc-ffmpeg/src/mux/segmented_audio.rs
index 76779cb9d..c8e6d65c4 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_audio.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_audio.rs
@@ -58,6 +58,7 @@ pub struct SegmentInfo {
pub path: PathBuf,
pub index: u32,
pub duration: Duration,
+ pub file_size: Option<u64>,
}
#[derive(Serialize)]
@@ -66,10 +67,15 @@ struct FragmentEntry {
index: u32,
duration: f64,
is_complete: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ file_size: Option<u64>,
}
+const MANIFEST_VERSION: u32 = 2;
+
#[derive(Serialize)]
struct Manifest {
+ version: u32,
fragments: Vec<FragmentEntry>,
#[serde(skip_serializing_if = "Option::is_none")]
total_duration: Option<f64>,
@@ -191,10 +197,15 @@ impl SegmentedAudioEncoder {
sync_file(&completed_segment_path);
+ let file_size = std::fs::metadata(&completed_segment_path)
+ .ok()
+ .map(|m| m.len());
+
self.completed_segments.push(SegmentInfo {
path: completed_segment_path,
index: self.current_index,
duration: segment_duration,
+ file_size,
});
self.write_manifest();
@@ -218,6 +229,7 @@ impl SegmentedAudioEncoder {
fn write_manifest(&self) {
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -231,6 +243,7 @@ impl SegmentedAudioEncoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: None,
@@ -260,6 +273,7 @@ impl SegmentedAudioEncoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect();
@@ -273,9 +287,11 @@ impl SegmentedAudioEncoder {
index: self.current_index,
duration: 0.0,
is_complete: false,
+ file_size: None,
});
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments,
total_duration: None,
is_complete: false,
@@ -291,21 +307,12 @@ impl SegmentedAudioEncoder {
}
pub fn finish(&mut self) -> Result<(), FinishError> {
+ let segment_path = self.current_segment_path();
+ let segment_start = self.segment_start_time;
+ let last_timestamp = self.last_frame_timestamp;
+
if let Some(mut encoder) = self.current_encoder.take() {
if encoder.has_frames {
- if let Some(segment_start) = self.segment_start_time {
- let final_duration = self
- .last_frame_timestamp
- .unwrap_or(segment_start)
- .saturating_sub(segment_start);
-
- self.completed_segments.push(SegmentInfo {
- path: self.current_segment_path(),
- index: self.current_index,
- duration: final_duration,
- });
- }
-
let flush_result = encoder.encoder.flush(&mut encoder.output);
let trailer_result = encoder.output.write_trailer();
@@ -315,9 +322,23 @@ impl SegmentedAudioEncoder {
if let Err(e) = &trailer_result {
tracing::warn!("Audio write_trailer warning: {e}");
}
+
+ sync_file(&segment_path);
+
+ if let Some(start) = segment_start {
+ let final_duration = last_timestamp.unwrap_or(start).saturating_sub(start);
+ let file_size = std::fs::metadata(&segment_path).ok().map(|m| m.len());
+
+ self.completed_segments.push(SegmentInfo {
+ path: segment_path,
+ index: self.current_index,
+ duration: final_duration,
+ file_size,
+ });
+ }
} else {
let _ = encoder.output.write_trailer();
- let _ = std::fs::remove_file(self.current_segment_path());
+ let _ = std::fs::remove_file(segment_path);
}
}
@@ -327,18 +348,11 @@ impl SegmentedAudioEncoder {
}
pub fn finish_with_timestamp(&mut self, timestamp: Duration) -> Result<(), FinishError> {
+ let segment_path = self.current_segment_path();
+ let segment_start = self.segment_start_time;
+
if let Some(mut encoder) = self.current_encoder.take() {
if encoder.has_frames {
- if let Some(segment_start) = self.segment_start_time {
- let final_duration = timestamp.saturating_sub(segment_start);
-
- self.completed_segments.push(SegmentInfo {
- path: self.current_segment_path(),
- index: self.current_index,
- duration: final_duration,
- });
- }
-
let flush_result = encoder.encoder.flush(&mut encoder.output);
let trailer_result = encoder.output.write_trailer();
@@ -348,9 +362,23 @@ impl SegmentedAudioEncoder {
if let Err(e) = &trailer_result {
tracing::warn!("Audio write_trailer warning: {e}");
}
+
+ sync_file(&segment_path);
+
+ if let Some(start) = segment_start {
+ let final_duration = timestamp.saturating_sub(start);
+ let file_size = std::fs::metadata(&segment_path).ok().map(|m| m.len());
+
+ self.completed_segments.push(SegmentInfo {
+ path: segment_path,
+ index: self.current_index,
+ duration: final_duration,
+ file_size,
+ });
+ }
} else {
let _ = encoder.output.write_trailer();
- let _ = std::fs::remove_file(self.current_segment_path());
+ let _ = std::fs::remove_file(segment_path);
}
}
@@ -363,6 +391,7 @@ impl SegmentedAudioEncoder {
let total_duration: Duration = self.completed_segments.iter().map(|s| s.duration).sum();
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -376,6 +405,7 @@ impl SegmentedAudioEncoder {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: Some(total_duration.as_secs_f64()),
diff --git a/crates/recording/src/fragmentation/manifest.rs b/crates/recording/src/fragmentation/manifest.rs
index 1a9cbedaf..70971d9ea 100644
--- a/crates/recording/src/fragmentation/manifest.rs
+++ b/crates/recording/src/fragmentation/manifest.rs
@@ -1,8 +1,12 @@
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, time::Duration};
+pub const CURRENT_MANIFEST_VERSION: u32 = 2;
+
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FragmentManifest {
+ #[serde(default = "default_manifest_version")]
+ pub version: u32,
pub fragments: Vec<FragmentInfo>,
#[serde(
with = "duration_serde",
@@ -13,6 +17,10 @@ pub struct FragmentManifest {
pub is_complete: bool,
}
+fn default_manifest_version() -> u32 {
+ 1
+}
+
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FragmentInfo {
#[serde(with = "path_serde")]
@@ -25,6 +33,8 @@ pub struct FragmentInfo {
)]
pub duration: Option<Duration>,
pub is_complete: bool,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub file_size: Option<u64>,
}
impl FragmentManifest {
diff --git a/crates/recording/src/fragmentation/mod.rs b/crates/recording/src/fragmentation/mod.rs
index ca9d398e0..225a338fc 100644
--- a/crates/recording/src/fragmentation/mod.rs
+++ b/crates/recording/src/fragmentation/mod.rs
@@ -65,12 +65,18 @@ impl FragmentManager {
.join(format!("fragment_{:03}.m4a", self.current_index))
}
- pub fn rotate(&mut self, duration: Option<Duration>, is_complete: bool) -> PathBuf {
+ pub fn rotate(
+ &mut self,
+ duration: Option<Duration>,
+ is_complete: bool,
+ file_size: Option<u64>,
+ ) -> PathBuf {
self.fragments.push(FragmentInfo {
path: self.current_fragment_path(),
index: self.current_index,
duration,
is_complete,
+ file_size,
});
self.current_index += 1;
@@ -89,17 +95,19 @@ impl FragmentManager {
&self.fragments
}
- pub fn mark_current_complete(&mut self, duration: Option<Duration>) {
+ pub fn mark_current_complete(&mut self, duration: Option<Duration>, file_size: Option<u64>) {
self.fragments.push(FragmentInfo {
path: self.current_fragment_path(),
index: self.current_index,
duration,
is_complete: true,
+ file_size,
});
}
pub fn write_manifest(&self) -> std::io::Result<()> {
let manifest = FragmentManifest {
+ version: CURRENT_MANIFEST_VERSION,
fragments: self.fragments.clone(),
total_duration: self.total_duration(),
is_complete: false,
@@ -111,6 +119,7 @@ impl FragmentManager {
pub fn finalize_manifest(&self) -> std::io::Result<()> {
let manifest = FragmentManifest {
+ version: CURRENT_MANIFEST_VERSION,
fragments: self.fragments.clone(),
total_duration: self.total_duration(),
is_complete: true,
diff --git a/crates/recording/src/output_pipeline/win_segmented.rs b/crates/recording/src/output_pipeline/win_segmented.rs
index 7f1acfbfa..e96e65b5f 100644
--- a/crates/recording/src/output_pipeline/win_segmented.rs
+++ b/crates/recording/src/output_pipeline/win_segmented.rs
@@ -24,6 +24,7 @@ pub struct SegmentInfo {
pub path: PathBuf,
pub index: u32,
pub duration: Duration,
+ pub file_size: Option<u64>,
}
#[derive(Serialize)]
@@ -32,10 +33,15 @@ struct FragmentEntry {
index: u32,
duration: f64,
is_complete: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ file_size: Option<u64>,
}
+const MANIFEST_VERSION: u32 = 2;
+
#[derive(Serialize)]
struct Manifest {
+ version: u32,
fragments: Vec<FragmentEntry>,
#[serde(skip_serializing_if = "Option::is_none")]
total_duration: Option<f64>,
@@ -172,15 +178,8 @@ impl Muxer for WindowsSegmentedMuxer {
}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<anyhow::Result<()>> {
- if let Some(segment_start) = self.segment_start_time {
- let final_duration = timestamp.saturating_sub(segment_start);
-
- self.completed_segments.push(SegmentInfo {
- path: self.current_segment_path(),
- index: self.current_index,
- duration: final_duration,
- });
- }
+ let segment_path = self.current_segment_path();
+ let segment_start = self.segment_start_time;
if let Some(mut state) = self.current_state.take() {
let _ = state.video_tx.send(None);
@@ -210,7 +209,19 @@ impl Muxer for WindowsSegmentedMuxer {
.map_err(|_| anyhow!("Failed to lock output"))?;
output.write_trailer()?;
- fragmentation::sync_file(&self.current_segment_path());
+ fragmentation::sync_file(&segment_path);
+
+ if let Some(start) = segment_start {
+ let final_duration = timestamp.saturating_sub(start);
+ let file_size = std::fs::metadata(&segment_path).ok().map(|m| m.len());
+
+ self.completed_segments.push(SegmentInfo {
+ path: segment_path,
+ index: self.current_index,
+ duration: final_duration,
+ file_size,
+ });
+ }
}
self.finalize_manifest();
@@ -227,6 +238,7 @@ impl WindowsSegmentedMuxer {
fn write_manifest(&self) {
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -240,6 +252,7 @@ impl WindowsSegmentedMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: None,
@@ -259,6 +272,7 @@ impl WindowsSegmentedMuxer {
let total_duration: Duration = self.completed_segments.iter().map(|s| s.duration).sum();
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -272,6 +286,7 @@ impl WindowsSegmentedMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: Some(total_duration.as_secs_f64()),
@@ -538,10 +553,15 @@ impl WindowsSegmentedMuxer {
fragmentation::sync_file(&completed_segment_path);
+ let file_size = std::fs::metadata(&completed_segment_path)
+ .ok()
+ .map(|m| m.len());
+
self.completed_segments.push(SegmentInfo {
path: completed_segment_path,
index: self.current_index,
duration: segment_duration,
+ file_size,
});
self.write_manifest();
@@ -575,6 +595,7 @@ impl WindowsSegmentedMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect();
@@ -588,9 +609,11 @@ impl WindowsSegmentedMuxer {
index: self.current_index,
duration: 0.0,
is_complete: false,
+ file_size: None,
});
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments,
total_duration: None,
is_complete: false,
diff --git a/crates/recording/src/output_pipeline/win_segmented_camera.rs b/crates/recording/src/output_pipeline/win_segmented_camera.rs
index 32e7fc880..cc5d6ae73 100644
--- a/crates/recording/src/output_pipeline/win_segmented_camera.rs
+++ b/crates/recording/src/output_pipeline/win_segmented_camera.rs
@@ -21,6 +21,7 @@ struct SegmentInfo {
path: PathBuf,
index: u32,
duration: Duration,
+ file_size: Option<u64>,
}
#[derive(Serialize)]
@@ -29,10 +30,15 @@ struct FragmentEntry {
index: u32,
duration: f64,
is_complete: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ file_size: Option<u64>,
}
+const MANIFEST_VERSION: u32 = 2;
+
#[derive(Serialize)]
struct Manifest {
+ version: u32,
fragments: Vec<FragmentEntry>,
#[serde(skip_serializing_if = "Option::is_none")]
total_duration: Option<f64>,
@@ -163,15 +169,8 @@ impl Muxer for WindowsSegmentedCameraMuxer {
}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<anyhow::Result<()>> {
- if let Some(segment_start) = self.segment_start_time {
- let final_duration = timestamp.saturating_sub(segment_start);
-
- self.completed_segments.push(SegmentInfo {
- path: self.current_segment_path(),
- index: self.current_index,
- duration: final_duration,
- });
- }
+ let segment_path = self.current_segment_path();
+ let segment_start = self.segment_start_time;
if let Some(mut state) = self.current_state.take() {
let _ = state.video_tx.send(None);
@@ -201,7 +200,19 @@ impl Muxer for WindowsSegmentedCameraMuxer {
.map_err(|_| anyhow!("Failed to lock output"))?;
output.write_trailer()?;
- fragmentation::sync_file(&self.current_segment_path());
+ fragmentation::sync_file(&segment_path);
+
+ if let Some(start) = segment_start {
+ let final_duration = timestamp.saturating_sub(start);
+ let file_size = std::fs::metadata(&segment_path).ok().map(|m| m.len());
+
+ self.completed_segments.push(SegmentInfo {
+ path: segment_path,
+ index: self.current_index,
+ duration: final_duration,
+ file_size,
+ });
+ }
}
self.finalize_manifest();
@@ -218,6 +229,7 @@ impl WindowsSegmentedCameraMuxer {
fn write_manifest(&self) {
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -231,6 +243,7 @@ impl WindowsSegmentedCameraMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: None,
@@ -250,6 +263,7 @@ impl WindowsSegmentedCameraMuxer {
let total_duration: Duration = self.completed_segments.iter().map(|s| s.duration).sum();
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments: self
.completed_segments
.iter()
@@ -263,6 +277,7 @@ impl WindowsSegmentedCameraMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect(),
total_duration: Some(total_duration.as_secs_f64()),
@@ -482,10 +497,15 @@ impl WindowsSegmentedCameraMuxer {
fragmentation::sync_file(&completed_segment_path);
+ let file_size = std::fs::metadata(&completed_segment_path)
+ .ok()
+ .map(|m| m.len());
+
self.completed_segments.push(SegmentInfo {
path: completed_segment_path,
index: self.current_index,
duration: segment_duration,
+ file_size,
});
self.write_manifest();
@@ -519,6 +539,7 @@ impl WindowsSegmentedCameraMuxer {
index: s.index,
duration: s.duration.as_secs_f64(),
is_complete: true,
+ file_size: s.file_size,
})
.collect();
@@ -532,9 +553,11 @@ impl WindowsSegmentedCameraMuxer {
index: self.current_index,
duration: 0.0,
is_complete: false,
+ file_size: None,
});
let manifest = Manifest {
+ version: MANIFEST_VERSION,
fragments,
total_duration: None,
is_complete: false,
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index da5560e4c..f66c571ab 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -201,6 +201,8 @@ impl RecoveryManager {
}
fn find_complete_fragments(dir: &Path) -> Vec<PathBuf> {
+ use crate::fragmentation::CURRENT_MANIFEST_VERSION;
+
let manifest_path = dir.join("manifest.json");
if manifest_path.exists()
@@ -208,6 +210,21 @@ impl RecoveryManager {
&& let Ok(manifest) = serde_json::from_str::<serde_json::Value>(&content)
&& let Some(fragments) = manifest.get("fragments").and_then(|f| f.as_array())
{
+ let manifest_version = manifest
+ .get("version")
+ .and_then(|v| v.as_u64())
+ .unwrap_or(1) as u32;
+ if manifest_version > CURRENT_MANIFEST_VERSION {
+ warn!(
+ "Manifest version {} is newer than supported {}",
+ manifest_version, CURRENT_MANIFEST_VERSION
+ );
+ }
+
+ let expected_file_size = |f: &serde_json::Value| -> Option<u64> {
+ f.get("file_size").and_then(|s| s.as_u64())
+ };
+
let result: Vec<PathBuf> = fragments
.iter()
.filter(|f| {
@@ -215,9 +232,46 @@ impl RecoveryManager {
.and_then(|c| c.as_bool())
.unwrap_or(false)
})
- .filter_map(|f| f.get("path").and_then(|p| p.as_str()))
- .map(|p| dir.join(p))
- .filter(|p| p.exists())
+ .filter_map(|f| {
+ let path_str = f.get("path").and_then(|p| p.as_str())?;
+ let path = dir.join(path_str);
+ if !path.exists() {
+ return None;
+ }
+
+ if let Some(expected_size) = expected_file_size(f) {
+ if let Ok(metadata) = std::fs::metadata(&path) {
+ if metadata.len() != expected_size {
+ warn!(
+ "Fragment {} size mismatch: expected {}, got {}",
+ path.display(),
+ expected_size,
+ metadata.len()
+ );
+ return None;
+ }
+ }
+ }
+
+ if Self::is_video_file(&path) {
+ match probe_video_can_decode(&path) {
+ Ok(true) => Some(path),
+ Ok(false) => {
+ warn!("Fragment {} has no decodable frames", path.display());
+ None
+ }
+ Err(e) => {
+ warn!("Fragment {} validation failed: {}", path.display(), e);
+ None
+ }
+ }
+ } else if probe_media_valid(&path) {
+ Some(path)
+ } else {
+ warn!("Fragment {} is not valid media", path.display());
+ None
+ }
+ })
.collect();
if !result.is_empty() {
@@ -228,6 +282,12 @@ impl RecoveryManager {
Self::probe_fragments_in_dir(dir)
}
+ fn is_video_file(path: &Path) -> bool {
+ path.extension()
+ .map(|e| e.eq_ignore_ascii_case("mp4"))
+ .unwrap_or(false)
+ }
+
fn probe_fragments_in_dir(dir: &Path) -> Vec<PathBuf> {
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
@@ -237,11 +297,26 @@ impl RecoveryManager {
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| {
- p.extension()
- .map(|e| e == "mp4" || e == "m4a" || e == "ogg")
- .unwrap_or(false)
+ let ext = p
+ .extension()
+ .and_then(|e| e.to_str())
+ .map(|e| e.to_lowercase());
+ match ext.as_deref() {
+ Some("mp4") => match probe_video_can_decode(p) {
+ Ok(true) => true,
+ Ok(false) => {
+ debug!("Skipping {} - no decodable frames", p.display());
+ false
+ }
+ Err(e) => {
+ debug!("Skipping {} - validation failed: {}", p.display(), e);
+ false
+ }
+ },
+ Some("m4a") | Some("ogg") => probe_media_valid(p),
+ _ => false,
+ }
})
- .filter(|p| probe_media_valid(p))
.collect();
fragments.sort();
@@ -249,7 +324,23 @@ impl RecoveryManager {
}
fn probe_single_file(path: &Path) -> Option<PathBuf> {
- if path.exists() && probe_media_valid(path) {
+ if !path.exists() {
+ return None;
+ }
+
+ if Self::is_video_file(path) {
+ match probe_video_can_decode(path) {
+ Ok(true) => Some(path.to_path_buf()),
+ Ok(false) => {
+ debug!("Single file {} has no decodable frames", path.display());
+ None
+ }
+ Err(e) => {
+ debug!("Single file {} validation failed: {}", path.display(), e);
+ None
+ }
+ }
+ } else if probe_media_valid(path) {
Some(path.to_path_buf())
} else {
None