Add --ffmpeg-threads flag to control the ffmpeg thread number

This commit is contained in:
bytedream 2023-12-09 01:33:54 +01:00
parent 6c7ab04b99
commit b4057599a1
3 changed files with 38 additions and 4 deletions

View file

@ -90,6 +90,16 @@ pub struct Archive {
#[arg(long)]
#[arg(value_parser = FFmpegPreset::parse)]
pub(crate) ffmpeg_preset: Option<FFmpegPreset>,
#[arg(
help = "The number of threads used by ffmpeg to generate the output file. Does not work with every codec/preset"
)]
#[arg(
long_help = "The number of threads used by ffmpeg to generate the output file. \
Does not work with every codec/preset and is skipped entirely when specifying custom ffmpeg output arguments instead of a preset for `--ffmpeg-preset`. \
By default, ffmpeg chooses the thread count which works best for the output codec"
)]
#[arg(long)]
pub(crate) ffmpeg_threads: Option<usize>,
#[arg(
help = "Set which subtitle language should be set as default / auto shown when starting a video"
@ -182,6 +192,7 @@ impl Execute for Archive {
let download_builder = DownloadBuilder::new()
.default_subtitle(self.default_subtitle.clone())
.ffmpeg_preset(self.ffmpeg_preset.clone().unwrap_or_default())
.ffmpeg_threads(self.ffmpeg_threads)
.output_format(Some("matroska".to_string()))
.audio_sort(Some(self.audio.clone()))
.subtitle_sort(Some(self.subtitle.clone()))

View file

@ -74,6 +74,16 @@ pub struct Download {
#[arg(long)]
#[arg(value_parser = FFmpegPreset::parse)]
pub(crate) ffmpeg_preset: Option<FFmpegPreset>,
#[arg(
help = "The number of threads used by ffmpeg to generate the output file. Does not work with every codec/preset"
)]
#[arg(
long_help = "The number of threads used by ffmpeg to generate the output file. \
Does not work with every codec/preset and is skipped entirely when specifying custom ffmpeg output arguments instead of a preset for `--ffmpeg-preset`. \
By default, ffmpeg chooses the thread count which works best for the output codec"
)]
#[arg(long)]
pub(crate) ffmpeg_threads: Option<usize>,
#[arg(help = "Skip files which are already existing")]
#[arg(long, default_value_t = false)]
@ -203,6 +213,7 @@ impl Execute for Download {
None
})
.ffmpeg_preset(self.ffmpeg_preset.clone().unwrap_or_default())
.ffmpeg_threads(self.ffmpeg_threads)
.threads(self.threads);
for mut single_formats in single_format_collection.into_iter() {

View file

@ -51,6 +51,7 @@ pub struct DownloadBuilder {
subtitle_sort: Option<Vec<Locale>>,
force_hardsub: bool,
threads: usize,
ffmpeg_threads: Option<usize>,
}
impl DownloadBuilder {
@ -63,6 +64,7 @@ impl DownloadBuilder {
subtitle_sort: None,
force_hardsub: false,
threads: num_cpus::get(),
ffmpeg_threads: None,
}
}
@ -75,7 +77,9 @@ impl DownloadBuilder {
subtitle_sort: self.subtitle_sort,
force_hardsub: self.force_hardsub,
threads: self.threads,
download_threads: self.threads,
ffmpeg_threads: self.ffmpeg_threads,
formats: vec![],
}
@ -102,7 +106,9 @@ pub struct Downloader {
subtitle_sort: Option<Vec<Locale>>,
force_hardsub: bool,
threads: usize,
download_threads: usize,
ffmpeg_threads: Option<usize>,
formats: Vec<DownloadFormat>,
}
@ -343,6 +349,7 @@ impl Downloader {
}
}
let preset_custom = matches!(self.ffmpeg_preset, FFmpegPreset::Custom(_));
let (input_presets, mut output_presets) = self.ffmpeg_preset.into_input_output_args();
let fifo = temp_named_pipe()?;
@ -356,6 +363,11 @@ impl Downloader {
command_args.extend(input);
command_args.extend(maps);
command_args.extend(metadata);
if !preset_custom {
if let Some(ffmpeg_threads) = self.ffmpeg_threads {
command_args.extend(vec!["-threads".to_string(), ffmpeg_threads.to_string()])
}
}
// set default subtitle
if let Some(default_subtitle) = self.default_subtitle {
@ -618,7 +630,7 @@ impl Downloader {
None
};
let cpus = self.threads;
let cpus = self.download_threads;
let mut segs: Vec<Vec<VariantSegment>> = Vec::with_capacity(cpus);
for _ in 0..cpus {
segs.push(vec![])