diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index 0c0b9fe..7c824cc 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -1,7 +1,7 @@ use core::fmt; use serde::Deserialize; use serde_json; -use std::{process::ExitStatus, str::Utf8Error}; +use std::str::Utf8Error; use tokio::process::Command; #[derive(Deserialize, Debug)] @@ -18,6 +18,13 @@ pub struct YtDlpFormat { pub abr: Option, } +#[derive(Debug)] +struct VideoFormat<'a> { + pub format: &'a YtDlpFormat, + pub width: u16, + pub height: u16, +} + impl YtDlpFormat { pub fn process(&mut self) { if self.acodec.as_ref().is_some_and(|v| v == "none") { @@ -75,12 +82,28 @@ impl YtDlpInfo { Ok(info) } - pub fn best_video_format(&self) -> Option<&str> { - //self.formats - // .iter() - + pub fn best_video_format(&self) -> Option<&YtDlpFormat> { + let mut formats: Vec = self + .formats + .iter() + .filter_map(|f| { + if f.vcodec.is_some() && f.acodec.is_some() { + Some(VideoFormat { + format: &f, + width: f.width?, + height: f.height?, + }) + } else { + None + } + }) + .collect(); + formats.sort_unstable_by_key(|f| (f.width, f.height)); - todo!() + match formats.last() { + Some(vf) => Some(vf.format), + None => None, + } } } @@ -130,7 +153,7 @@ impl YtDlp { .args(["-m", "yt_dlp", url, "-j"]) .output() .await?; - + if !output.status.success() { let message = std::str::from_utf8(&output.stderr)?; return Err(YtDlpError::ErrorMessage(message.to_string())); diff --git a/src/main.rs b/src/main.rs index 3c1e848..598e2cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,11 @@ async fn main() -> anyhow::Result<()> { let info = YtDlp::load_info(env::var("TEST_URL")?.as_str()) .await .expect("load_info"); - for format in info.formats { - println!("{}", format); - } + // for format in info.formats { + // println!("{}", format); + // } + let video = info.best_video_format().unwrap(); + println!("{}", video); Ok(()) //bot_main().await