use crate::assembling::youtube;
use crate::analyzer;
use std::process;
#[derive(Debug, Clone)]
pub struct DownloadConfig {
url: String,
output_path: String,
include_indexes: bool,
chosen_format: youtube::VideoQualityAndFormatPreferences,
media_selected: youtube::MediaSelection,
pub download_target: analyzer::DownloadOption,
}
impl DownloadConfig {
pub(crate) fn new_playlist (
url: &str,
output_path: String,
include_indexes: bool,
chosen_format: youtube::VideoQualityAndFormatPreferences,
media_selected: youtube::MediaSelection,
)
-> DownloadConfig
{
DownloadConfig { url: url.to_string(), output_path, include_indexes, chosen_format, media_selected,
download_target: analyzer::DownloadOption::YtPlaylist }
}
pub(crate) fn new_video (
url: &str,
chosen_format: youtube::VideoQualityAndFormatPreferences,
output_path: String,
media_selected: youtube::MediaSelection,
)
-> DownloadConfig
{
DownloadConfig { url: url.to_string(), chosen_format, output_path, media_selected,
include_indexes: false, download_target: analyzer::DownloadOption::YtVideo(0) }
}
}
impl DownloadConfig {
pub(crate) fn build_command(&self) -> (process::Command, DownloadConfig) {
(
match self.download_target {
analyzer::DownloadOption::YtVideo(_) => self.build_yt_video_command(),
analyzer::DownloadOption::YtPlaylist => self.build_yt_playlist_command(),
},
self.clone()
)
}
fn build_yt_playlist_command(&self) -> process::Command {
let mut command = process::Command::new("yt-dlp");
command.arg("-i");
command.arg("--yes-playlist");
self.choose_output_path(&mut command);
let id = match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::UniqueFormat(id) => id.to_string(),
_ => String::new(),
};
self.choose_format(&mut command, id.as_str());
command.arg(self.url.clone());
command
}
fn build_yt_video_command(&self) -> process::Command {
let mut command = process::Command::new("yt-dlp");
self.choose_output_path(&mut command);
let id = match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::UniqueFormat(id) => id.to_string(),
_ => String::new(),
};
self.choose_format(&mut command, &id);
command.arg("--no-playlist");
command.arg(self.url.clone());
command
}
pub fn build_command_for_video(&self, video_id: &str) -> process::Command {
let mut command = process::Command::new("yt-dlp");
self.choose_output_path(&mut command);
let id = match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::UniqueFormat(id) => id.to_string(),
_ => String::new(),
};
self.choose_format(&mut command, id.as_str());
command.arg("--no-playlist");
command.arg(video_id);
command
}
fn choose_output_path(&self, command: &mut process::Command) {
command.arg("-o");
command.arg(
{
let mut path_and_scheme = String::new();
path_and_scheme.push_str(self.output_path.as_str());
if self.download_target == analyzer::DownloadOption::YtPlaylist {
#[cfg(target_os = "windows")]
path_and_scheme.push_str("\\%(playlist)s\\");
#[cfg(not(target_os = "windows"))]
path_and_scheme.push_str("/%(playlist)s/");
if self.include_indexes {
path_and_scheme.push_str("%(playlist_index)s_");
};
path_and_scheme.push_str("%(title)s");
} else {
#[cfg(target_os = "windows")]
path_and_scheme.push_str("\\%(title)s.%(ext)s");
#[cfg(not(target_os = "windows"))]
path_and_scheme.push_str("/%(title)s.%(ext)s");
}
path_and_scheme
}
);
}
fn choose_format(&self, command: &mut process::Command, format_id: &str) {
match self.media_selected {
youtube::MediaSelection::FullVideo => {
match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::BestQuality => {}
youtube::VideoQualityAndFormatPreferences::SmallestSize => {
command.arg("-S").arg("+size,+br");
}
youtube::VideoQualityAndFormatPreferences::UniqueFormat(_) => {
command.arg("-f").arg(format_id);
}
youtube::VideoQualityAndFormatPreferences::ConvertTo(f) => {
command.arg("--recode-video").arg(f.as_str());
}
}
}
youtube::MediaSelection::AudioOnly => {
match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::BestQuality => {
command.arg("-f").arg("bestaudio");
}
youtube::VideoQualityAndFormatPreferences::SmallestSize => {
command.arg("-f").arg("worstaudio");
}
youtube::VideoQualityAndFormatPreferences::UniqueFormat(_) => {
command.arg("-f").arg(format_id);
}
youtube::VideoQualityAndFormatPreferences::ConvertTo(f) => {
command.arg("-x").arg("--audio-format").arg(f.as_str());
}
}
}
youtube::MediaSelection::VideoOnly => {
match &self.chosen_format {
youtube::VideoQualityAndFormatPreferences::BestQuality => {
command.arg("-f").arg("bestvideo");
}
youtube::VideoQualityAndFormatPreferences::SmallestSize => {
command.arg("-f").arg("worstvideo");
}
youtube::VideoQualityAndFormatPreferences::UniqueFormat(_) => {
command.arg("-f").arg(format_id);
}
youtube::VideoQualityAndFormatPreferences::ConvertTo(f) => {
command.arg("--recode-video").arg(f.as_str());
}
}
}
};
}
}