check exit status instead of stdout empty & stderr non-empty. also begin working on algorithm to determine best format

This commit is contained in:
mykola2312 2024-02-19 20:24:20 +02:00
parent 28771f3e28
commit 33c49cc56f

View file

@ -1,7 +1,7 @@
use core::fmt; use core::fmt;
use serde::Deserialize; use serde::Deserialize;
use serde_json; use serde_json;
use std::str::Utf8Error; use std::{process::ExitStatus, str::Utf8Error};
use tokio::process::Command; use tokio::process::Command;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -12,10 +12,10 @@ pub struct YtDlpFormat {
pub width: Option<u16>, pub width: Option<u16>,
pub height: Option<u16>, pub height: Option<u16>,
pub ext: String, pub ext: String,
pub acodec: Option<String>,
pub vcodec: Option<String>, pub vcodec: Option<String>,
pub abr: Option<f32>, pub acodec: Option<String>,
pub vbr: Option<f32>, pub vbr: Option<f32>,
pub abr: Option<f32>,
} }
impl YtDlpFormat { impl YtDlpFormat {
@ -66,10 +66,21 @@ pub struct YtDlpInfo {
} }
impl YtDlpInfo { impl YtDlpInfo {
pub fn process(&mut self) { pub fn parse(json: &[u8]) -> Result<YtDlpInfo, serde_json::Error> {
for format in &mut self.formats { let mut info: YtDlpInfo = serde_json::from_slice(json)?;
for format in &mut info.formats {
format.process() format.process()
} }
Ok(info)
}
pub fn best_video_format(&self) -> Option<&str> {
//self.formats
// .iter()
todo!()
} }
} }
@ -119,15 +130,12 @@ impl YtDlp {
.args(["-m", "yt_dlp", url, "-j"]) .args(["-m", "yt_dlp", url, "-j"])
.output() .output()
.await?; .await?;
if output.stdout.is_empty() && !output.stderr.is_empty() { if !output.status.success() {
let message = std::str::from_utf8(&output.stderr)?; let message = std::str::from_utf8(&output.stderr)?;
return Err(YtDlpError::ErrorMessage(message.to_string())); return Err(YtDlpError::ErrorMessage(message.to_string()));
} }
let mut info: YtDlpInfo = serde_json::from_slice(&output.stdout)?; Ok(YtDlpInfo::parse(&output.stdout)?)
info.process();
Ok(info)
} }
} }