2024-03-23 22:55:48 +07:00
|
|
|
use std::{path::Path, process::Command};
|
|
|
|
|
|
2024-03-25 17:14:33 +07:00
|
|
|
use crate::track::TrackInfo;
|
|
|
|
|
|
2024-03-25 16:56:06 +07:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct DownloadError;
|
|
|
|
|
|
|
|
|
|
pub fn download_from_youtube(url: &str) -> Result<TrackInfo, DownloadError> {
|
2024-03-23 22:55:48 +07:00
|
|
|
let output = Command::new("yt-dlp")
|
|
|
|
|
.args([
|
|
|
|
|
"-o",
|
|
|
|
|
"%(id)s",
|
|
|
|
|
"--extract-audio",
|
|
|
|
|
"--audio-format",
|
|
|
|
|
"mp3",
|
|
|
|
|
"--print",
|
|
|
|
|
"%(id)s",
|
2024-03-25 17:14:33 +07:00
|
|
|
"--no-simulate",
|
2024-03-23 22:55:48 +07:00
|
|
|
url,
|
|
|
|
|
])
|
|
|
|
|
.output()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2024-03-25 17:14:33 +07:00
|
|
|
if !output.stderr.is_empty() || output.stdout.is_empty() {
|
2024-03-25 16:56:06 +07:00
|
|
|
return Err(DownloadError);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 22:55:48 +07:00
|
|
|
let filename = std::str::from_utf8(output.stdout.as_slice())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.replace('\n', "")
|
|
|
|
|
+ ".mp3";
|
|
|
|
|
|
2024-03-25 16:56:06 +07:00
|
|
|
Ok(TrackInfo::new(&Path::new(filename.as_str())))
|
2024-03-23 22:55:48 +07:00
|
|
|
}
|