implement function to find best quality video format

This commit is contained in:
mykola2312 2024-02-20 14:24:19 +02:00
parent 28799ca380
commit d1bced33b5
2 changed files with 35 additions and 10 deletions

View file

@ -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<f32>,
}
#[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<VideoFormat> = 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()));

View file

@ -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