use std::{io::Write, path::Path, thread};
use ffmpeg_sidecar::{
command::FfmpegCommand,
event::{FfmpegEvent, LogLevel},
};
fn main() {
let input_path = "output/h265.mp4";
if !Path::new(input_path).exists() {
create_h265_source(input_path);
}
let mut input = FfmpegCommand::new()
.input(input_path)
.rawvideo()
.spawn()
.unwrap();
let transformed_frames = input.iter().unwrap().filter_frames();
let mut output = FfmpegCommand::new()
.args([
"-f", "rawvideo", "-pix_fmt", "rgb24", "-s", "600x800", "-r", "30",
]) .input("-")
.args(["-c:v", "libx265"])
.args(["-y", "output/h265_overlay.mp4"])
.spawn()
.unwrap();
let mut stdin = output.take_stdin().unwrap();
thread::spawn(move || {
transformed_frames.for_each(|f| {
stdin.write_all(&f.data).ok();
});
});
output.iter().unwrap().for_each(|e| match e {
FfmpegEvent::Log(LogLevel::Error, e) => println!("Error: {}", e),
FfmpegEvent::Progress(p) => println!("Progress: {} / 00:00:15", p.time),
_ => {}
});
}
fn create_h265_source(path_str: &str) {
println!("Creating H265 source video: {}", path_str);
FfmpegCommand::new()
.args("-f lavfi -i testsrc=size=600x800:rate=30:duration=15 -c:v libx265".split(' '))
.arg(path_str)
.spawn()
.unwrap()
.iter()
.unwrap()
.for_each(|e| match e {
FfmpegEvent::Log(LogLevel::Error, e) => println!("Error: {}", e),
FfmpegEvent::Progress(p) => println!("Progress: {} / 00:00:15", p.time),
_ => {}
});
println!("Created H265 source video: {}", path_str);
}