From 28799ca3800d3eb5d41ecbfba2e8f35f48dcd952 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 19 Feb 2024 20:24:20 +0200 Subject: [PATCH] check exit status instead of stdout empty & stderr non-empty. also begin working on algorithm to determine best format --- src/dl/yt_dlp.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index 9731157..0c0b9fe 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -1,7 +1,7 @@ use core::fmt; use serde::Deserialize; use serde_json; -use std::str::Utf8Error; +use std::{process::ExitStatus, str::Utf8Error}; use tokio::process::Command; #[derive(Deserialize, Debug)] @@ -12,10 +12,10 @@ pub struct YtDlpFormat { pub width: Option, pub height: Option, pub ext: String, - pub acodec: Option, pub vcodec: Option, - pub abr: Option, + pub acodec: Option, pub vbr: Option, + pub abr: Option, } impl YtDlpFormat { @@ -66,10 +66,21 @@ pub struct YtDlpInfo { } impl YtDlpInfo { - pub fn process(&mut self) { - for format in &mut self.formats { + pub fn parse(json: &[u8]) -> Result { + let mut info: YtDlpInfo = serde_json::from_slice(json)?; + for format in &mut info.formats { 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"]) .output() .await?; - - if output.stdout.is_empty() && !output.stderr.is_empty() { + + if !output.status.success() { let message = std::str::from_utf8(&output.stderr)?; return Err(YtDlpError::ErrorMessage(message.to_string())); } - let mut info: YtDlpInfo = serde_json::from_slice(&output.stdout)?; - info.process(); - - Ok(info) + Ok(YtDlpInfo::parse(&output.stdout)?) } }