implement function to find best audio format

This commit is contained in:
mykola2312 2024-02-20 16:39:15 +02:00
parent b6f5dc234d
commit e35f84cdc3
4 changed files with 33 additions and 1 deletions

10
Cargo.lock generated
View file

@ -653,6 +653,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"dotenv",
"ordered-float",
"serde",
"serde_json",
"teloxide",
@ -755,6 +756,15 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "ordered-float"
version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76df7075c7d4d01fdcb46c912dd17fba5b60c78ea480b475f2b6ab6f666584e"
dependencies = [
"num-traits",
]
[[package]]
name = "percent-encoding"
version = "2.3.0"

View file

@ -12,3 +12,4 @@ tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros", "process"
teloxide = { version = "0.12.2", git ="https://github.com/teloxide/teloxide", features = ["macros"] }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
ordered-float = "4.2.0"

View file

@ -3,6 +3,7 @@ use serde::Deserialize;
use serde_json;
use std::str::Utf8Error;
use tokio::process::Command;
use ordered_float::OrderedFloat;
#[derive(Deserialize, Debug)]
pub struct YtDlpFormat {
@ -18,13 +19,17 @@ pub struct YtDlpFormat {
pub abr: Option<f32>,
}
#[derive(Debug)]
struct VideoFormat<'a> {
pub format: &'a YtDlpFormat,
pub width: u16,
pub height: u16,
}
struct AudioFormat<'a> {
pub format: &'a YtDlpFormat,
pub abr: f32
}
impl YtDlpFormat {
pub fn process(&mut self) {
if self.acodec.as_ref().is_some_and(|v| v == "none") {
@ -104,6 +109,19 @@ impl YtDlpInfo {
None => None,
}
}
pub fn best_audio_format(&self) -> Option<&YtDlpFormat> {
let format = self
.formats
.iter()
.filter_map(|f| Some(AudioFormat { format: f, abr: f.abr? }))
.max_by_key(|f| OrderedFloat(f.abr));
match format {
Some(af) => Some(af.format),
None => None,
}
}
}
#[derive(Debug)]

View file

@ -47,6 +47,9 @@ async fn main() -> anyhow::Result<()> {
let video = info.best_av_format().unwrap();
println!("{}", video);
let audio = info.best_audio_format().unwrap();
println!("{}", audio);
Ok(())
//bot_main().await
}