add logging to ffmpeg stderr

This commit is contained in:
jb-alvarado 2022-03-11 17:49:24 +01:00
parent 072bb54e98
commit 47808f9bf7
4 changed files with 62 additions and 7 deletions

View File

@ -36,7 +36,7 @@ pub fn output(config: Config, log_format: String) -> process::Child {
let enc_proc = match Command::new("ffplay")
.args(enc_cmd)
.stdin(Stdio::piped())
// .stderr(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Err(e) => {

View File

@ -16,12 +16,20 @@ use simplelog::*;
mod desktop;
mod stream;
use crate::utils::{sec_to_time, watch_folder, Config, CurrentProgram, Media, Source};
use crate::utils::{
sec_to_time, stderr_reader, watch_folder, Config, CurrentProgram, Media, Source,
};
pub fn play(config: Config) {
let dec_pid: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
let mut thread_count = 2;
if config.processing.mode.as_str() == "folder" {
thread_count += 1;
}
let runtime = Builder::new_multi_thread()
.worker_threads(1)
.worker_threads(thread_count)
.enable_all()
.build()
.unwrap();
@ -67,9 +75,14 @@ pub fn play(config: Config) {
let mut enc_proc = match config.out.mode.as_str() {
"desktop" => desktop::output(config_clone, ff_log_format.clone()),
"stream" => stream::output(config_clone, ff_log_format.clone()),
_ => panic!("Output mode doesn't exists!")
_ => panic!("Output mode doesn't exists!"),
};
runtime.spawn(stderr_reader(
enc_proc.stderr.take().unwrap(),
"Decoder".to_string(),
));
let mut buffer: [u8; 65424] = [0; 65424];
for node in get_source {
@ -104,7 +117,7 @@ pub fn play(config: Config) {
let mut dec_proc = match Command::new("ffmpeg")
.args(dec_cmd)
.stdout(Stdio::piped())
// .stderr(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Err(e) => {
@ -121,6 +134,11 @@ pub fn play(config: Config) {
// debug!("Decoder PID: <yellow>{}</>", dec_pid.lock().unwrap());
runtime.spawn(stderr_reader(
dec_proc.stderr.take().unwrap(),
"Encoder".to_string(),
));
loop {
let dec_bytes_len = match dec_reader.read(&mut buffer[..]) {
Ok(length) => length,
@ -143,6 +161,8 @@ pub fn play(config: Config) {
sleep(Duration::from_secs(1));
println!("!!!!!!!!!!!!!!end");
match enc_proc.kill() {
Ok(_) => info!("Playout done..."),
Err(e) => panic!("Encoder error: {:?}", e),

View File

@ -38,7 +38,7 @@ pub fn output(config: Config, log_format: String) -> process::Child {
let enc_proc = match Command::new("ffmpeg")
.args(enc_cmd)
.stdin(Stdio::piped())
// .stderr(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Err(e) => {

View File

@ -2,7 +2,14 @@ use chrono::prelude::*;
use chrono::Duration;
use ffprobe::{ffprobe, Format, Stream};
use serde::{Deserialize, Serialize};
use std::{fs::metadata, path::Path, time, time::UNIX_EPOCH};
use std::{
fs::metadata,
io::{BufRead, BufReader, Error},
path::Path,
process::ChildStderr,
time,
time::UNIX_EPOCH,
};
use simplelog::*;
@ -297,3 +304,31 @@ pub fn seek_and_length(src: String, seek: f64, out: f64, duration: f64) -> Vec<S
source_cmd
}
pub async fn stderr_reader(
std_errors: ChildStderr,
suffix: String,
) -> Result<(), Error> {
// read ffmpeg stderr decoder and encoder instance
// and log the output
fn format_line(line: String, level: String) -> String {
line.replace(&format!("[{}] ", level), "")
}
let buffer = BufReader::new(std_errors);
for line in buffer.lines() {
let line = line?;
if line.contains("[info]") {
info!("<bright black>[{suffix}]</> {}", format_line(line, "info".to_string()))
} else if line.contains("[warning]") {
warn!("<bright black>[{suffix}]</> {}", format_line(line, "warning".to_string()))
} else {
error!("<bright black>[{suffix}]</> {}", format_line(line, "error".to_string()))
}
}
Ok(())
}