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
+11
-1
@@ -1028,11 +1028,21 @@ pub fn demo_review(app: &mut App) {
|
||||
}
|
||||
screens::review::show(app, sample.remove(0));
|
||||
app.review.error = Some("Sample recording: no clip on disk.".to_string());
|
||||
// Snapshot hook: draw a real clip when one is handed in.
|
||||
// Snapshot hook: draw a real clip when one is handed in, with as many audio tracks as
|
||||
// `LR_DEMO_TRACKS` names.
|
||||
if let Ok(path) = std::env::var("LR_DEMO_VIDEO") {
|
||||
let player = rune::Player::file(&path);
|
||||
player.set_looping(false);
|
||||
app.review.player = Some(player);
|
||||
app.review.error = None;
|
||||
if let Ok(tracks) = std::env::var("LR_DEMO_TRACKS") {
|
||||
if let Some(game) = app.review.game.as_mut() {
|
||||
game.audio_tracks = tracks
|
||||
.split(',')
|
||||
.map(|name| name.trim().to_string())
|
||||
.collect();
|
||||
}
|
||||
app.review.open_sound(&path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,13 @@ use crate::ipc::Client;
|
||||
/// Set this to point the app and the daemon at a scratch directory, as scripts do.
|
||||
pub const HOME_ENV: &str = "RECORD_DAEMON_HOME";
|
||||
|
||||
/// Where the review screen keeps one audio file per track of a clip.
|
||||
pub fn audio_cache_dir() -> PathBuf {
|
||||
data_dir()
|
||||
.map(|p| p.join("audio-cache"))
|
||||
.unwrap_or_else(|| PathBuf::from("./audio-cache"))
|
||||
}
|
||||
|
||||
/// The daemon's data directory, shared with the daemon itself.
|
||||
pub fn data_dir() -> Option<PathBuf> {
|
||||
if let Some(home) = std::env::var_os(HOME_ENV).filter(|value| !value.is_empty()) {
|
||||
|
||||
+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| {
|
||||
|
||||
@@ -38,6 +38,10 @@ pub struct Timeline {
|
||||
pub raw_end_game_stats: Option<Value>,
|
||||
#[serde(default)]
|
||||
pub video_file: Option<String>,
|
||||
/// What each audio track of the video carries, in file order. Empty for recordings made
|
||||
/// before the recorder wrote one track per source.
|
||||
#[serde(default)]
|
||||
pub audio_tracks: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -581,6 +585,7 @@ pub fn sample() -> Vec<Timeline> {
|
||||
}
|
||||
})),
|
||||
video_file: None,
|
||||
audio_tracks: Vec::new(),
|
||||
}
|
||||
};
|
||||
vec![
|
||||
|
||||
+177
-28
@@ -18,11 +18,8 @@ pub struct Review {
|
||||
/// Set while that file is on its way to the screen.
|
||||
pub loading: bool,
|
||||
pub player: Option<Player>,
|
||||
/// The clip's soundtrack, played by its own player. Rune's video player asks Media Foundation
|
||||
/// for pictures and sound from one reader, and that reader stamps the sound from its own
|
||||
/// clock instead of the clip's, so the sound never lines up with the clock and plays silent.
|
||||
/// An audio-only player reads the same file and needs no video, so its sound starts at zero.
|
||||
pub sound: Option<Player>,
|
||||
/// The clip's audio tracks, one player each, so the channels are heard apart.
|
||||
pub sounds: Vec<Channel>,
|
||||
/// The picture time the soundtrack was last put back to, to spot a window that stopped drawing.
|
||||
steady_at: f64,
|
||||
pub time: f64,
|
||||
@@ -38,10 +35,12 @@ pub struct Review {
|
||||
pub volume: f32,
|
||||
pub muted: bool,
|
||||
pub error: Option<String>,
|
||||
/// Why the channels could not be opened, when the clip has more than one.
|
||||
pub channel_error: Option<String>,
|
||||
}
|
||||
|
||||
impl Review {
|
||||
/// The video player only draws; the soundtrack player carries the gain and the mute.
|
||||
/// The video player only draws; the audio players carry the gain and the mute.
|
||||
fn mute_video(player: &Player) {
|
||||
player.set_muted(true);
|
||||
}
|
||||
@@ -52,21 +51,86 @@ impl Review {
|
||||
if self.volume > 0.0 {
|
||||
self.muted = false;
|
||||
}
|
||||
if let Some(sound) = &self.sound {
|
||||
sound.set_volume(self.volume);
|
||||
sound.set_muted(self.muted);
|
||||
for channel in &self.sounds {
|
||||
channel.sound.set_volume(self.volume);
|
||||
channel.sound.set_muted(!channel.heard(self.muted));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_muted(&mut self) {
|
||||
self.muted = !self.muted;
|
||||
if let Some(sound) = &self.sound {
|
||||
sound.set_muted(self.muted);
|
||||
for channel in &self.sounds {
|
||||
channel.sound.set_muted(!channel.heard(self.muted));
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the soundtrack's player, at the place the viewer is watching.
|
||||
fn open_sound(&mut self, path: &str) {
|
||||
/// Turn one track on or off, and make the change audible now rather than at the next seek.
|
||||
pub fn toggle_channel(&mut self, index: usize) {
|
||||
let Some(channel) = self.sounds.get_mut(index) else {
|
||||
return;
|
||||
};
|
||||
channel.on = !channel.on;
|
||||
channel.sound.set_muted(!channel.heard(self.muted));
|
||||
}
|
||||
|
||||
/// The audio streams a clip or highlight keeps: the tracks the viewer left on.
|
||||
pub fn kept_tracks(&self) -> Vec<usize> {
|
||||
self.sounds
|
||||
.iter()
|
||||
.filter(|channel| channel.on)
|
||||
.map(|channel| channel.stream)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Start the soundtrack's players, at the place the viewer is watching.
|
||||
pub fn open_sound(&mut self, path: &str) {
|
||||
self.sounds.clear();
|
||||
self.channel_error = None;
|
||||
let labels = self
|
||||
.game
|
||||
.as_ref()
|
||||
.map(|game| game.audio_tracks.clone())
|
||||
.unwrap_or_default();
|
||||
if labels.len() < 2 {
|
||||
// One track, or a recording made before the recorder split them: the file's own
|
||||
// sound is the whole mix, and an audio player reads it as it is.
|
||||
let label = labels
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "All audio".to_string());
|
||||
let channel = self.channel(0, label, path);
|
||||
self.sounds.push(channel);
|
||||
return;
|
||||
}
|
||||
let stem = self
|
||||
.game
|
||||
.as_ref()
|
||||
.and_then(|game| game.video_file.clone())
|
||||
.or_else(|| {
|
||||
std::path::Path::new(path)
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().to_string())
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let stem = stem.rsplit_once('.').map(|(name, _)| name).unwrap_or(&stem);
|
||||
let files = match crate::export::extract_tracks(path, stem, labels.len()) {
|
||||
Ok(files) => files,
|
||||
Err(e) => {
|
||||
self.channel_error = Some(e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
for (stream, file) in files.iter().enumerate() {
|
||||
// A track no source fed is silent in the file; it gets no toggle.
|
||||
let Some(label) = labels.get(stream).filter(|label| !label.trim().is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
let channel = self.channel(stream, label.clone(), &file.to_string_lossy());
|
||||
self.sounds.push(channel);
|
||||
}
|
||||
}
|
||||
|
||||
fn channel(&self, stream: usize, label: String, path: &str) -> Channel {
|
||||
let sound = Player::audio(path.to_string());
|
||||
sound.set_looping(false);
|
||||
sound.set_volume(self.volume);
|
||||
@@ -79,7 +143,12 @@ impl Review {
|
||||
if playing {
|
||||
sound.play();
|
||||
}
|
||||
self.sound = Some(sound);
|
||||
Channel {
|
||||
label,
|
||||
stream,
|
||||
sound,
|
||||
on: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn playback(&mut self) {
|
||||
@@ -91,15 +160,14 @@ impl Review {
|
||||
player.play();
|
||||
}
|
||||
}
|
||||
if let Some(sound) = &self.sound {
|
||||
for channel in &self.sounds {
|
||||
if playing {
|
||||
sound.pause();
|
||||
channel.sound.pause();
|
||||
} else {
|
||||
sound.play();
|
||||
channel.sound.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Clip button: start a selection at the viewer's place, or drop the one on screen.
|
||||
pub fn toggle_clip(&mut self) {
|
||||
self.clip = match self.clip {
|
||||
@@ -136,11 +204,33 @@ impl Review {
|
||||
player.seek(at);
|
||||
self.time = at;
|
||||
}
|
||||
if let Some(sound) = &self.sound {
|
||||
sound.seek(at);
|
||||
for channel in &self.sounds {
|
||||
channel.sound.seek(at);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// One audio track of the clip, copied to a file of its own and played on its own, so the
|
||||
/// channels are heard apart and can be kept apart.
|
||||
/// Rune's video player asks Media Foundation for pictures and sound from one reader, and that
|
||||
/// reader stamps the sound from its own clock instead of the clip's, so the sound never lines
|
||||
/// up with the clock and plays silent. Audio-only players read one track each and need no
|
||||
/// video, so their sound starts at zero.
|
||||
pub struct Channel {
|
||||
/// What the track carries, as the recorder named it.
|
||||
pub label: String,
|
||||
/// The stream this track is, in the file.
|
||||
pub stream: usize,
|
||||
pub sound: Player,
|
||||
/// Off means the viewer does not hear it, and a clip or highlight does not keep it.
|
||||
pub on: bool,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
/// Whether this track goes into the current mix, given the master mute.
|
||||
fn heard(&self, muted: bool) -> bool {
|
||||
self.on && !muted
|
||||
}
|
||||
}
|
||||
|
||||
/// A clip being chosen on the scrubber: where it starts and where it ends.
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -166,7 +256,7 @@ impl Default for Review {
|
||||
game: None,
|
||||
loading: false,
|
||||
player: None,
|
||||
sound: None,
|
||||
sounds: Vec::new(),
|
||||
steady_at: 0.0,
|
||||
time: 0.0,
|
||||
duration: 0.0,
|
||||
@@ -181,6 +271,7 @@ impl Default for Review {
|
||||
// headroom, and the transport bar can change it.
|
||||
volume: 0.8,
|
||||
muted: false,
|
||||
channel_error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +320,7 @@ pub fn show(app: &mut App, game: Timeline) {
|
||||
player.set_waker(waker.clone());
|
||||
}
|
||||
app.review.player = Some(player);
|
||||
app.review.sound = None;
|
||||
app.review.sounds.clear();
|
||||
app.review.steady_at = 0.0;
|
||||
Review::mute_video(app.review.player.as_ref().expect("just set"));
|
||||
}
|
||||
@@ -240,7 +331,7 @@ fn follow_sound(app: &mut App) {
|
||||
let Some(player) = &app.review.player else {
|
||||
return;
|
||||
};
|
||||
if app.review.sound.is_some() || player.has_sound() != Some(true) {
|
||||
if !app.review.sounds.is_empty() || player.has_sound() != Some(true) {
|
||||
return;
|
||||
}
|
||||
let Some(path) = app
|
||||
@@ -259,7 +350,7 @@ pub fn leave(app: &mut App) {
|
||||
app.review_rx = None;
|
||||
app.review.loading = false;
|
||||
app.review.player = None;
|
||||
app.review.sound = None;
|
||||
app.review.sounds.clear();
|
||||
app.review.game = None;
|
||||
app.review.highlights.clear();
|
||||
app.review.time = 0.0;
|
||||
@@ -272,7 +363,10 @@ pub fn leave(app: &mut App) {
|
||||
/// Sound from a second player drifts from the pictures by a few milliseconds a minute. Past a
|
||||
/// quarter second it is worth a seek, and short of that a seek would be audible.
|
||||
fn keep_in_step(app: &mut App) {
|
||||
let (Some(player), Some(sound)) = (&app.review.player, &app.review.sound) else {
|
||||
let (Some(player), Some(sound)) = (
|
||||
&app.review.player,
|
||||
app.review.sounds.first().map(|channel| &channel.sound),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
if !player.is_playing() {
|
||||
@@ -626,12 +720,64 @@ fn transport(app: &App, p: Paint) -> El {
|
||||
.w_full()
|
||||
.gap(space::S2)
|
||||
.child(bar);
|
||||
if let Some(line) = channels(app, p) {
|
||||
column = column.child(line);
|
||||
}
|
||||
if let Some(clip) = app.review.clip {
|
||||
column = column.child(clip_row(app, p, clip));
|
||||
}
|
||||
column
|
||||
}
|
||||
|
||||
/// One toggle per audio track, shown once the clip carries more than one. A track that is off
|
||||
/// is neither heard nor kept in an export.
|
||||
fn channels(app: &App, p: Paint) -> Option<El> {
|
||||
if app.review.sounds.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let mut line = row().key("channels").center_y().gap(space::S2).child(
|
||||
text("Audio")
|
||||
.key("channels-label")
|
||||
.font(body(11.5))
|
||||
.color(p.muted()),
|
||||
);
|
||||
for (index, channel) in app.review.sounds.iter().enumerate() {
|
||||
let label = channel.label.clone();
|
||||
let on = channel.on;
|
||||
line = line.child(
|
||||
el().key(format!("channel-{index}"))
|
||||
.role("switch")
|
||||
.label(label.clone())
|
||||
.checked(on)
|
||||
.focusable(true)
|
||||
.h(24.0)
|
||||
.px(10.0)
|
||||
.center()
|
||||
.radius(radius::MD)
|
||||
.fill(if on { p.accent } else { p.t.control_off })
|
||||
.hover(p.t.hover)
|
||||
.press(p.t.press)
|
||||
.on_press(move |app: &mut App| app.review.toggle_channel(index))
|
||||
.child(
|
||||
text(label)
|
||||
.key("label")
|
||||
.font(type_::LABEL)
|
||||
.color(if on { p.accent_ink } else { p.muted() })
|
||||
.a11y_hidden(),
|
||||
),
|
||||
);
|
||||
}
|
||||
if let Some(error) = &app.review.channel_error {
|
||||
line = line.child(
|
||||
text(error.clone())
|
||||
.key("channels-error")
|
||||
.font(body(11.5))
|
||||
.color(p.warning),
|
||||
);
|
||||
}
|
||||
Some(line)
|
||||
}
|
||||
|
||||
/// The row under the transport while a clip is being chosen: the two ends, its length, and the
|
||||
/// two things to do with it.
|
||||
fn clip_row(app: &App, p: Paint, clip: Clip) -> El {
|
||||
@@ -673,9 +819,10 @@ fn clip_row(app: &App, p: Paint, clip: Clip) -> El {
|
||||
Some(clip) => (clip.start, clip.end),
|
||||
None => return,
|
||||
};
|
||||
let audio = app.review.kept_tracks();
|
||||
std::thread::spawn(move || {
|
||||
let result =
|
||||
crate::export::export_clip(&video, start, end, &name, true, "high");
|
||||
crate::export::export_clip(&video, start, end, &name, true, "high", &audio);
|
||||
let _ = tx.send(result);
|
||||
});
|
||||
app.review.export = Some(rx);
|
||||
@@ -907,9 +1054,11 @@ fn highlights(app: &App, p: Paint, game: &Timeline) -> El {
|
||||
let (tx, rx) = channel();
|
||||
let Some(video) = video.clone() else { return };
|
||||
let name = name.clone();
|
||||
let audio = app.review.kept_tracks();
|
||||
std::thread::spawn(move || {
|
||||
let result =
|
||||
crate::export::export_clip(&video, start, end, &name, true, "high");
|
||||
let result = crate::export::export_clip(
|
||||
&video, start, end, &name, true, "high", &audio,
|
||||
);
|
||||
let _ = tx.send(result);
|
||||
});
|
||||
app.review.export = Some(rx);
|
||||
|
||||
Reference in new issue
Block a user