every 30 frames, one conversion to jpg done

This commit is contained in:
Muaz Ahmad 2023-10-26 16:35:08 +05:00
parent 7d24ea4d5c
commit 9521c6cf15
2 changed files with 30 additions and 0 deletions

View file

@ -48,6 +48,7 @@ fn write_raw(cmd_in: &mut ChildStdin, media: util::RawMedia) -> Result<(), Box<d
} }
pub fn read_raw_loop(mut cmd_in: ChildStdin, c_in: mpsc::Receiver<util::RawMedia>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) { pub fn read_raw_loop(mut cmd_in: ChildStdin, c_in: mpsc::Receiver<util::RawMedia>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
let mut i = 0;
loop { loop {
let media = match c_in.recv() { let media = match c_in.recv() {
Ok(x) => x, Ok(x) => x,
@ -56,10 +57,22 @@ pub fn read_raw_loop(mut cmd_in: ChildStdin, c_in: mpsc::Receiver<util::RawMedia
// needed, park to keep c_err // needed, park to keep c_err
// allocated // allocated
}; };
if i % 30 == 0 {
match media.media_type {
util::RawMediaType::YUV420P => {
let yuv = media.sample.clone();
thread::spawn(move || {
util::cursed_jpg(yuv);
});
},
_ => ()
}
}
match write_raw(&mut cmd_in, media) { match write_raw(&mut cmd_in, media) {
Ok(_) => (), Ok(_) => (),
Err(err) => {util::thread_freeze(c_err, err); return} // park thread, wait for main to Err(err) => {util::thread_freeze(c_err, err); return} // park thread, wait for main to
// exit // exit
} }
i += 1;
} }
} }

View file

@ -1,9 +1,11 @@
// general use structs, functions and errors go here // general use structs, functions and errors go here
use std::io::Write;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::thread; use std::thread;
use std::sync::mpsc; use std::sync::mpsc;
use std::process::{Command, Stdio};
// Data structs/enums // Data structs/enums
@ -236,3 +238,18 @@ pub fn thread_freeze(err_sender: mpsc::Sender<Box<dyn Error + Send + Sync>>, err
pub fn blank_vec(n: usize) -> Vec<u8> { pub fn blank_vec(n: usize) -> Vec<u8> {
return vec![0u8; n]; return vec![0u8; n];
} }
pub fn cursed_jpg(yuv: Vec<u8>) {
let mut cmd = Command::new("ffmpeg").args([
"-pix_fmt", "yuv420p",
"-f", "rawvideo",
"-s", "1280x720",
"-i", "-",
"-q:v", "10",
"out.jpg",
"-y", "-hide_banner", "-loglevel", "quiet",
]).stdin(Stdio::piped()).spawn().unwrap();
let mut stdin = cmd.stdin.take().unwrap();
stdin.write_all(yuv.as_slice());
drop(stdin);
}