record: keep a track per audio source and let the review mute them
The recorder put every source on one mixer track, so the file had one audio stream and the review screen could not tell the game's sound from the microphone or from Discord. The daemon now gives each source a track: 1 for the game, 2 for the microphone, then one per application in the settings, up to OBS's six. Every track above the first gets an audio encoder and the output is told which tracks to write, which is what libobs's muxer reads to lay the streams out in track order. The timeline's new `audio_tracks` names what each stream holds, with empty names for the tracks no source fed. The app opens one player per stream, copied out of the clip into the audio cache, and the transport carries a toggle per channel. A channel that is off is neither heard nor kept: clip and highlight exports mix only the tracks left on. Recordings made before this change have one stream and play as they did.
This commit is contained in:
1 parent
4f2ee4256a
commit
eabc3780c9
13 files changed
+545
-46
No files matched your search
+93
-3
@@ -25,7 +25,8 @@ pub fn ensure_ffmpeg() -> Result<(), String> {
|
||||
}
|
||||
|
||||
/// Export `start..end` of `video`. Stream copy is fast and keyframe-aligned; `precise` re-encodes
|
||||
/// with x264 for exact cuts.
|
||||
/// with x264 for exact cuts. `audio` names the audio streams the clip keeps, empty for all of
|
||||
/// them as they are.
|
||||
pub fn export_clip(
|
||||
video: &str,
|
||||
start: f64,
|
||||
@@ -33,6 +34,7 @@ pub fn export_clip(
|
||||
output_name: &str,
|
||||
precise: bool,
|
||||
quality: &str,
|
||||
audio: &[usize],
|
||||
) -> Result<String, String> {
|
||||
ensure_ffmpeg()?;
|
||||
if !PathBuf::from(video).exists() {
|
||||
@@ -53,7 +55,38 @@ pub fn export_clip(
|
||||
.arg(video)
|
||||
.arg("-t")
|
||||
.arg(format!("{duration:.3}"));
|
||||
if precise {
|
||||
|
||||
// Which tracks the clip keeps. One track is mapped whole; several are mixed into a single
|
||||
// stereo track, because a clip is meant to be played, not to carry a track picker.
|
||||
let kept: Vec<String> = audio
|
||||
.iter()
|
||||
.map(|stream| format!("[0:a:{stream}]"))
|
||||
.collect();
|
||||
if kept.len() > 1 {
|
||||
command
|
||||
.arg("-filter_complex")
|
||||
.arg(format!(
|
||||
"{}amix=inputs={}:normalize=0[out]",
|
||||
kept.concat(),
|
||||
kept.len()
|
||||
))
|
||||
.arg("-map")
|
||||
.arg("0:v")
|
||||
.arg("-map")
|
||||
.arg("[out]")
|
||||
.arg("-c:a")
|
||||
.arg("aac")
|
||||
.arg("-b:a")
|
||||
.arg("160k");
|
||||
} else if let Some(stream) = audio.first() {
|
||||
command
|
||||
.arg("-map")
|
||||
.arg("0:v")
|
||||
.arg("-map")
|
||||
.arg(format!("0:a:{stream}"));
|
||||
}
|
||||
|
||||
if precise || kept.len() > 1 {
|
||||
let crf = match quality {
|
||||
"low" => "28",
|
||||
"high" => "18",
|
||||
@@ -77,7 +110,6 @@ pub fn export_clip(
|
||||
.arg("-avoid_negative_ts")
|
||||
.arg("make_zero");
|
||||
}
|
||||
|
||||
let iter = command
|
||||
.arg(output.to_string_lossy().to_string())
|
||||
.spawn()
|
||||
@@ -105,6 +137,64 @@ pub fn export_clip(
|
||||
Ok(output.display().to_string())
|
||||
}
|
||||
|
||||
/// Copy each audio track of `video` into a file of its own, so a player can be opened per
|
||||
/// 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 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)
|
||||
.and_then(|m| m.modified())
|
||||
.map_err(|e| format!("cannot read {video}: {e}"))?;
|
||||
|
||||
let mut paths = Vec::new();
|
||||
for track in 0..count {
|
||||
let path = dir.join(format!("track-{track}.m4a"));
|
||||
let fresh = std::fs::metadata(&path)
|
||||
.and_then(|m| m.modified())
|
||||
.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("; ")
|
||||
});
|
||||
}
|
||||
}
|
||||
paths.push(path);
|
||||
}
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
fn sanitize(name: &str) -> String {
|
||||
name.chars()
|
||||
.map(|c| {
|
||||
|
||||
Reference in new issue
Block a user