add MediaProbe, and sec to tome

This commit is contained in:
jb-alvarado 2022-02-18 16:46:05 +01:00
parent a09e9f4eb7
commit 0b82779292

View File

@ -1,6 +1,10 @@
use chrono::prelude::*; use chrono::prelude::*;
use chrono::Duration; use chrono::Duration;
use ffprobe::{ffprobe, Format, Stream};
use serde::{Deserialize, Serialize};
use std::fs::metadata; use std::fs::metadata;
use std::time::{UNIX_EPOCH};
use std::time;
mod arg_parse; mod arg_parse;
mod config; mod config;
@ -9,11 +13,66 @@ mod json_reader;
mod playlist; mod playlist;
pub use arg_parse::get_args; pub use arg_parse::get_args;
pub use config::{Config, get_config}; pub use config::{get_config, Config};
// pub use folder::walk; // pub use folder::walk;
pub use json_reader::read_json; pub use json_reader::read_json;
pub use playlist::program; pub use playlist::program;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MediaProbe {
format: Option<Format>,
audio_streams: Option<Vec<Stream>>,
video_streams: Option<Vec<Stream>>,
}
impl MediaProbe {
fn new(input: String) -> Self {
let probe = ffprobe(&input);
let mut a_stream: Vec<Stream> = vec![];
let mut v_stream: Vec<Stream> = 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 { // pub fn get_timestamp() -> i64 {
// let local: DateTime<Local> = Local::now(); // let local: DateTime<Local> = Local::now();
@ -51,3 +110,11 @@ pub fn modified_time(path: String) -> Option<DateTime<Local>> {
None 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::<Utc>::from(d);
date_time.format("%H:%M:%S").to_string()
}