diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 73410ef4..57276a04 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,10 @@ use chrono::prelude::*; use chrono::Duration; +use ffprobe::{ffprobe, Format, Stream}; +use serde::{Deserialize, Serialize}; use std::fs::metadata; +use std::time::{UNIX_EPOCH}; +use std::time; mod arg_parse; mod config; @@ -9,11 +13,66 @@ mod json_reader; mod playlist; pub use arg_parse::get_args; -pub use config::{Config, get_config}; +pub use config::{get_config, Config}; // pub use folder::walk; pub use json_reader::read_json; pub use playlist::program; +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct MediaProbe { + format: Option, + audio_streams: Option>, + video_streams: Option>, +} + +impl MediaProbe { + fn new(input: String) -> Self { + let probe = ffprobe(&input); + let mut a_stream: Vec = vec![]; + let mut v_stream: Vec = vec![]; + + match probe { + Ok(obj) => { + for stream in obj.streams { + let cp_stream = stream.clone(); + match cp_stream.codec_type { + Some(codec_type) => { + if codec_type == "audio" { + a_stream.push(stream) + } else if codec_type == "video" { + v_stream.push(stream) + } + } + + _ => { + println!("No codec type found for stream: {:?}", &stream) + } + } + } + + MediaProbe { + format: Some(obj.format), + audio_streams: if a_stream.len() > 0 {Some(a_stream)} else {None}, + video_streams: if v_stream.len() > 0 {Some(v_stream)} else {None}, + } + } + Err(err) => { + println!( + "Can't read source '{}' with ffprobe, source is probably damaged! Error is: {:?}", + input, + err + ); + + MediaProbe { + format: None, + audio_streams: None, + video_streams: None, + } + } + } + } +} + // pub fn get_timestamp() -> i64 { // let local: DateTime = Local::now(); @@ -51,3 +110,11 @@ pub fn modified_time(path: String) -> Option> { None } + +pub fn sec_to_time(sec: f64) -> String { + let d = UNIX_EPOCH + time::Duration::from_secs(sec as u64); + // Create DateTime from SystemTime + let date_time = DateTime::::from(d); + + date_time.format("%H:%M:%S").to_string() +}