app: cut a recording's audio tracks off the UI thread
The first open of a multi-track recording re-encoded every track on the main thread, so the channels could be muted apart. On a 27 minute clip that is 12.8 s per track, about a minute of a window Windows marks as not responding, which is what made a game look ready to crash the first time it was opened. The files are cached, so only that first open paid it. The cut now runs on a thread, and `tick` opens the channel players when the files land, at the playhead's time and playing state. Each track is copied instead of re-encoded, which takes 0.4 s on the same clip, and the AAC encode stays as a fallback for a codec an m4a cannot hold. A mutex keeps two cuts from writing the same cache file when a row is reopened while its first cut is still running.
This commit is contained in:
1 parent
9f6a047867
commit
1d7f87f71d
2 files changed
+156
-53
No files matched your search
+53
-35
@@ -1,7 +1,8 @@
|
||||
//! Clip export with ffmpeg, ported from `export_clip` and `export_clip_precise` in
|
||||
//! `tauri-app/src-tauri/src/lib.rs`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use ffmpeg_sidecar::command::{FfmpegCommand, ffmpeg_is_installed};
|
||||
@@ -25,6 +26,10 @@ pub fn clips_dir() -> PathBuf {
|
||||
/// unpacks over the first.
|
||||
static FETCHING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// One cut at a time. Reopening a row while its tracks are still being cut would otherwise
|
||||
/// have two ffmpeg processes writing the same cache file.
|
||||
static CUTTING: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// What `fetch` reports while it runs.
|
||||
pub enum FetchEvent {
|
||||
/// Bytes of the download so far, of the total the server announced.
|
||||
@@ -214,6 +219,7 @@ pub fn export_clip(
|
||||
/// track. Files are kept between sessions; a track older than the clip is cut again.
|
||||
pub fn extract_tracks(video: &str, stem: &str, count: usize) -> Result<Vec<PathBuf>, String> {
|
||||
ensure_ffmpeg()?;
|
||||
let _cutting = CUTTING.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let dir = crate::daemon::audio_cache_dir().join(sanitize(stem));
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
||||
let clip_time = std::fs::metadata(video)
|
||||
@@ -228,46 +234,58 @@ pub fn extract_tracks(video: &str, stem: &str, count: usize) -> Result<Vec<PathB
|
||||
.map(|time| time >= clip_time)
|
||||
.unwrap_or(false);
|
||||
if !fresh {
|
||||
let mut command = FfmpegCommand::new();
|
||||
command
|
||||
.arg("-y")
|
||||
.arg("-i")
|
||||
.arg(video)
|
||||
.arg("-map")
|
||||
.arg(format!("0:a:{track}"))
|
||||
.arg("-vn")
|
||||
.arg("-c:a")
|
||||
.arg("aac")
|
||||
.arg("-b:a")
|
||||
.arg("160k")
|
||||
.arg(path.to_string_lossy().to_string());
|
||||
let mut errors = Vec::new();
|
||||
for event in command
|
||||
.spawn()
|
||||
.map_err(|e| format!("cannot start ffmpeg: {e}"))?
|
||||
.iter()
|
||||
.map_err(|e| format!("cannot read ffmpeg output: {e}"))?
|
||||
{
|
||||
match event {
|
||||
FfmpegEvent::Error(e) => errors.push(e),
|
||||
FfmpegEvent::Log(LogLevel::Error, msg) => errors.push(msg),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
|
||||
if size == 0 {
|
||||
return Err(if errors.is_empty() {
|
||||
format!("audio track {track} came out empty")
|
||||
} else {
|
||||
errors.join("; ")
|
||||
});
|
||||
}
|
||||
// The stream copy takes seconds where re-encoding takes minutes; a track whose
|
||||
// codec an m4a cannot hold is cut again as AAC.
|
||||
cut_track(video, track, &path, true)
|
||||
.or_else(|_| cut_track(video, track, &path, false))?;
|
||||
}
|
||||
paths.push(path);
|
||||
}
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
/// Cut `track` out of `video` into `path`, keeping the stream as it is (`copy`) or re-encoding
|
||||
/// it as AAC.
|
||||
fn cut_track(video: &str, track: usize, path: &Path, copy: bool) -> Result<(), String> {
|
||||
let mut command = FfmpegCommand::new();
|
||||
command
|
||||
.arg("-y")
|
||||
.arg("-i")
|
||||
.arg(video)
|
||||
.arg("-map")
|
||||
.arg(format!("0:a:{track}"))
|
||||
.arg("-vn");
|
||||
if copy {
|
||||
command.arg("-c:a").arg("copy");
|
||||
} else {
|
||||
command.arg("-c:a").arg("aac").arg("-b:a").arg("160k");
|
||||
}
|
||||
command.arg(path.to_string_lossy().to_string());
|
||||
|
||||
let mut errors = Vec::new();
|
||||
for event in command
|
||||
.spawn()
|
||||
.map_err(|e| format!("cannot start ffmpeg: {e}"))?
|
||||
.iter()
|
||||
.map_err(|e| format!("cannot read ffmpeg output: {e}"))?
|
||||
{
|
||||
match event {
|
||||
FfmpegEvent::Error(e) => errors.push(e),
|
||||
FfmpegEvent::Log(LogLevel::Error, msg) => errors.push(msg),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
|
||||
if size == 0 {
|
||||
return Err(if errors.is_empty() {
|
||||
format!("audio track {track} came out empty")
|
||||
} else {
|
||||
errors.join("; ")
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sanitize(name: &str) -> String {
|
||||
name.chars()
|
||||
.map(|c| {
|
||||
|
||||
Reference in new issue
Block a user