implement function to find best audio format
This commit is contained in:
parent
b6f5dc234d
commit
e35f84cdc3
4 changed files with 33 additions and 1 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
|
@ -653,6 +653,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
"ordered-float",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"teloxide",
|
"teloxide",
|
||||||
|
|
@ -755,6 +756,15 @@ dependencies = [
|
||||||
"vcpkg",
|
"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]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.0"
|
version = "2.3.0"
|
||||||
|
|
|
||||||
|
|
@ -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"] }
|
teloxide = { version = "0.12.2", git ="https://github.com/teloxide/teloxide", features = ["macros"] }
|
||||||
serde = { version = "1.0.196", features = ["derive"] }
|
serde = { version = "1.0.196", features = ["derive"] }
|
||||||
serde_json = "1.0.113"
|
serde_json = "1.0.113"
|
||||||
|
ordered-float = "4.2.0"
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use serde::Deserialize;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
use ordered_float::OrderedFloat;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct YtDlpFormat {
|
pub struct YtDlpFormat {
|
||||||
|
|
@ -18,13 +19,17 @@ pub struct YtDlpFormat {
|
||||||
pub abr: Option<f32>,
|
pub abr: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct VideoFormat<'a> {
|
struct VideoFormat<'a> {
|
||||||
pub format: &'a YtDlpFormat,
|
pub format: &'a YtDlpFormat,
|
||||||
pub width: u16,
|
pub width: u16,
|
||||||
pub height: u16,
|
pub height: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct AudioFormat<'a> {
|
||||||
|
pub format: &'a YtDlpFormat,
|
||||||
|
pub abr: f32
|
||||||
|
}
|
||||||
|
|
||||||
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") {
|
||||||
|
|
@ -104,6 +109,19 @@ impl YtDlpInfo {
|
||||||
None => None,
|
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)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,9 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let video = info.best_av_format().unwrap();
|
let video = info.best_av_format().unwrap();
|
||||||
println!("{}", video);
|
println!("{}", video);
|
||||||
|
|
||||||
|
let audio = info.best_audio_format().unwrap();
|
||||||
|
println!("{}", audio);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
//bot_main().await
|
//bot_main().await
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue