Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
use a 'close enough' method to audio auto merge (#286) (#320)
Browse files Browse the repository at this point in the history
* use a 'close enough' method to audio merge

* change default, rename flag, and use more gooder words
  • Loading branch information
kralverde authored Jan 29, 2024
1 parent 7cf7a8e commit a4abb14
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions crunchy-cli-core/src/archive/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use chrono::Duration;
use crunchyroll_rs::media::{Resolution, Subtitle};
use crunchyroll_rs::Locale;
use log::{debug, warn};
use std::collections::HashMap;
use std::ops::Sub;
use std::path::PathBuf;

#[derive(Clone, Debug, clap::Parser)]
Expand Down Expand Up @@ -86,6 +86,12 @@ pub struct Archive {
#[arg(value_parser = MergeBehavior::parse)]
pub(crate) merge: MergeBehavior,

#[arg(
help = "If the merge behavior is 'auto', only download multiple video tracks if their length difference is higher than the given milliseconds"
)]
#[arg(long, default_value_t = 200)]
pub(crate) merge_auto_tolerance: u32,

#[arg(help = format!("Presets for converting the video to a specific coding format. \
Available presets: \n {}", FFmpegPreset::available_matches_human_readable().join("\n ")))]
#[arg(long_help = format!("Presets for converting the video to a specific coding format. \
Expand Down Expand Up @@ -356,26 +362,46 @@ async fn get_format(
.collect(),
}),
MergeBehavior::Auto => {
let mut d_formats: HashMap<Duration, DownloadFormat> = HashMap::new();

let mut d_formats: Vec<(Duration, DownloadFormat)> = vec![];
for (single_format, video, audio, subtitles) in format_pairs {
if let Some(d_format) = d_formats.get_mut(&single_format.duration) {
d_format.audios.push((audio, single_format.audio.clone()));
d_format.subtitles.extend(subtitles)
} else {
d_formats.insert(
single_format.duration,
DownloadFormat {
video: (video, single_format.audio.clone()),
audios: vec![(audio, single_format.audio.clone())],
subtitles,
},
);
}
let closest_format = d_formats.iter_mut().min_by(|(x, _), (y, _)| {
x.sub(single_format.duration)
.abs()
.cmp(&y.sub(single_format.duration).abs())
});

match closest_format {
Some(closest_format)
if closest_format
.0
.sub(single_format.duration)
.abs()
.num_milliseconds()
< archive.merge_auto_tolerance.into() =>
{
// If less than `audio_error` apart, use same audio.
closest_format
.1
.audios
.push((audio, single_format.audio.clone()));
closest_format.1.subtitles.extend(subtitles);
}
_ => {
d_formats.push((
single_format.duration,
DownloadFormat {
video: (video, single_format.audio.clone()),
audios: vec![(audio, single_format.audio.clone())],
subtitles,
},
));
}
};
}

for d_format in d_formats.into_values() {
download_formats.push(d_format)
for (_, d_format) in d_formats.into_iter() {
download_formats.push(d_format);
}
}
}
Expand Down

0 comments on commit a4abb14

Please sign in to comment.