use std::ffi::OsString;
use std::io;
use super::{Command, SpawnCommand, SpawnedCommand};
#[derive(Debug, Clone)]
pub struct YtDlpCommand {
url: String,
cmd_name: OsString,
extract_audio: bool,
format: Option<String>,
}
impl From<YtDlpCommand> for Command {
fn from(value: YtDlpCommand) -> Self {
value.into_command()
}
}
impl SpawnCommand for YtDlpCommand {
fn spawn(self) -> io::Result<SpawnedCommand> {
self.into_command().spawn()
}
}
impl YtDlpCommand {
pub fn new<S>(url: S) -> Self
where
S: Into<String>,
{
Self {
url: url.into(),
cmd_name: "yt-dlp".into(),
extract_audio: false,
format: None,
}
}
#[must_use]
pub fn into_command(self) -> Command {
let mut cmd = Command::new(&self.cmd_name).args([
&self.url,
"--quiet",
"--no-part",
"--no-continue",
"-o",
"-",
]);
if self.extract_audio {
cmd = cmd.arg("-x");
}
if let Some(format) = &self.format {
cmd = cmd.args(["-f", format]);
}
cmd
}
#[must_use]
pub fn extract_audio(mut self, extract_audio: bool) -> Self {
self.extract_audio = extract_audio;
self
}
#[must_use]
pub fn format<S>(mut self, format: S) -> Self
where
S: Into<String>,
{
self.format = Some(format.into());
self
}
#[must_use]
pub fn yt_dlp_path<S>(mut self, path: S) -> Self
where
S: Into<OsString>,
{
self.cmd_name = path.into();
self
}
}