fix json parsing error

This commit is contained in:
mykola2312 2024-09-13 08:35:03 +03:00
parent cccff3edc2
commit 2e6edffcb7
3 changed files with 26 additions and 13 deletions

View file

@ -137,14 +137,10 @@ enum Command {
DeclineChat(String),
}
use crate::dl::spawn::spawn;
use crate::dl::ffprobe::FFProbe;
async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult {
//bot.send_message(msg.chat.id, t!("test_response")).await?;
let output = spawn("python", &["-c", "import os; print(os.environ)"])
.await
.unwrap();
println!("{}", std::str::from_utf8(&output.stdout[0..4095]).unwrap());
dbg!(FFProbe::probe("/home/mykola/Videos/test-video").await);
Ok(())
}

View file

@ -5,6 +5,8 @@ use tracing::{event, Level};
use super::types::HandlerResult;
use crate::dl::download;
use crate::dl::ffprobe::FFProbe;
async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
let output = match download(url.as_str()).await {
Ok(file) => file,
@ -17,6 +19,8 @@ async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
// query media info with
// ffprobe -v quiet -print_format json -show_streams -select_streams v:0 input.mp4
let probe = FFProbe::probe(&output.path).await;
dbg!(probe);
let mut video = bot.send_video(msg.chat.id, InputFile::file(&output.path));
// set width, height and so on

View file

@ -1,17 +1,27 @@
use super::spawn::{spawn, SpawnError};
use serde::Deserialize;
use serde::{de, de::Error, Deserialize, Deserializer};
use std::fmt;
fn duration_from_str<'de, D>(deserializer: D) -> Result<f64, D::Error>
where D: Deserializer<'de>
{
let s = String::deserialize(deserializer)?;
Ok(str::parse(&s).map_err(de::Error::custom)?)
}
#[derive(Deserialize, Debug)]
pub struct FFProbeStream {
pub index: u32,
pub codec_name: String,
pub width: u32,
pub height: u32,
pub coded_width: u32,
pub coded_height: u32,
pub width: Option<u32>,
pub height: Option<u32>,
pub coded_width: Option<u32>,
pub coded_height: Option<u32>,
pub time_base: String,
pub duration_ts: u64,
#[serde(deserialize_with = "duration_from_str")]
pub duration: f64
}
@ -22,12 +32,15 @@ pub struct FFProbeOutput {
impl FFProbeOutput {
pub fn parse(json: &[u8]) -> Result<FFProbeOutput, serde_json::Error> {
let output: FFProbeOutput = serde_json::from_slice(json)?;
let output: Result<FFProbeOutput, _> = serde_json::from_slice(json);
dbg!(output);
Ok(output)
todo!();
//Ok(output)
}
}
#[derive(Debug)]
pub enum FFProbeError {
SpawnError(SpawnError),
JsonError