use std::process::Command;
use std::process::Stdio;
use tempfile;
use tempfile::TempPath;
use crate::common::errors::MyError;
pub fn download_video(url: &str) -> Result<TempPath, MyError> {
if Command::new("yt-dlp").output().is_err() {
return Err(MyError::Application(
"yt-dlp is not installed.
To view YouTube videos Please install it and try again.
See https://github.com/yt-dlp/yt-dlp/wiki/Installation"
.to_string(),
));
};
let temp_file = tempfile::Builder::new()
.prefix("my_temp_file_")
.suffix(".webm")
.tempfile()?;
let output = Command::new("yt-dlp")
.arg(url)
.arg("-o")
.arg("-")
.stdout(Stdio::from(temp_file.as_file().try_clone()?))
.output()?;
if output.status.success() {
temp_file.as_file().sync_all()?;
let temp_file_path = temp_file.into_temp_path();
Ok(temp_file_path)
} else {
Err(MyError::Application(format!(
"Error downloading video: {:?}",
output.stderr
)))
}
}