implement function to find best quality video format
This commit is contained in:
parent
33c49cc56f
commit
1cef56d964
2 changed files with 35 additions and 10 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std::{process::ExitStatus, str::Utf8Error};
|
use std::str::Utf8Error;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
|
@ -18,6 +18,13 @@ pub struct YtDlpFormat {
|
||||||
pub abr: Option<f32>,
|
pub abr: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct VideoFormat<'a> {
|
||||||
|
pub format: &'a YtDlpFormat,
|
||||||
|
pub width: u16,
|
||||||
|
pub height: u16,
|
||||||
|
}
|
||||||
|
|
||||||
impl YtDlpFormat {
|
impl YtDlpFormat {
|
||||||
pub fn process(&mut self) {
|
pub fn process(&mut self) {
|
||||||
if self.acodec.as_ref().is_some_and(|v| v == "none") {
|
if self.acodec.as_ref().is_some_and(|v| v == "none") {
|
||||||
|
|
@ -75,12 +82,28 @@ impl YtDlpInfo {
|
||||||
Ok(info)
|
Ok(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn best_video_format(&self) -> Option<&str> {
|
pub fn best_video_format(&self) -> Option<&YtDlpFormat> {
|
||||||
//self.formats
|
let mut formats: Vec<VideoFormat> = self
|
||||||
// .iter()
|
.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));
|
||||||
|
|
||||||
|
match formats.last() {
|
||||||
todo!()
|
Some(vf) => Some(vf.format),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,9 +39,11 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let info = YtDlp::load_info(env::var("TEST_URL")?.as_str())
|
let info = YtDlp::load_info(env::var("TEST_URL")?.as_str())
|
||||||
.await
|
.await
|
||||||
.expect("load_info");
|
.expect("load_info");
|
||||||
for format in info.formats {
|
// for format in info.formats {
|
||||||
println!("{}", format);
|
// println!("{}", format);
|
||||||
}
|
// }
|
||||||
|
let video = info.best_video_format().unwrap();
|
||||||
|
println!("{}", video);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
//bot_main().await
|
//bot_main().await
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue