fix logic error

This commit is contained in:
Daniella 2022-10-11 07:09:25 +02:00
parent 319a6385dd
commit c9d7f0c2ad

View file

@ -327,62 +327,63 @@ impl EventHandler for Handler {
async fn main() { async fn main() {
if !Path::new("vid_encoded/").is_dir() { if !Path::new("vid_encoded/").is_dir() {
println!("encode: encoding video..."); println!("encode: encoding video...");
fs::create_dir("vid").expect("encode: unable to modify files"); if let Ok(_) = fs::create_dir("vid") {
let mut command = process::Command::new("ffmpeg") let mut command = process::Command::new("ffmpeg")
.args([ .args([
"-i", "-i",
"vid.mp4", "vid.mp4",
"-vf", "-vf",
"fps=fps=25", "fps=fps=25",
"-deadline", "-deadline",
"realtime", "realtime",
"vid_25fps.mp4", "vid_25fps.mp4",
]) ])
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.spawn() .spawn()
.expect("encode: unable to find or run ffmpeg"); .expect("encode: unable to find or run ffmpeg");
command.wait().expect("encode: ffmpeg failed: mp4->mp4"); command.wait().expect("encode: ffmpeg failed: mp4->mp4");
let mut command = process::Command::new("ffmpeg") let mut command = process::Command::new("ffmpeg")
.args([ .args([
"-i", "-i",
"vid_25fps.mp4", "vid_25fps.mp4",
"-vf", "-vf",
"scale=240:180,setsar=1:1", "scale=240:180,setsar=1:1",
"-deadline", "-deadline",
"realtime", "realtime",
"vid/%0d.png", "vid/%0d.png",
]) ])
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.spawn() .spawn()
.expect("encode: unable to find or run ffmpeg"); .expect("encode: unable to find or run ffmpeg");
command.wait().expect("encode: ffmpeg failed: mp4->png"); command.wait().expect("encode: ffmpeg failed: mp4->png");
fs::remove_file("vid_25fps.mp4").expect("encode: rm vid_25fps.mp4 failed"); fs::remove_file("vid_25fps.mp4").expect("encode: rm vid_25fps.mp4 failed");
let mut command = process::Command::new("ffmpeg") let mut command = process::Command::new("ffmpeg")
.args(["-i", "vid.mp4", "-deadline", "realtime", "aud.opus"]) .args(["-i", "vid.mp4", "-deadline", "realtime", "aud.opus"])
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.spawn() .spawn()
.expect("encode: unable to find or run ffmpeg"); .expect("encode: unable to find or run ffmpeg");
command.wait().expect("encode: ffmpeg failed: mp4->opus"); command.wait().expect("encode: ffmpeg failed: mp4->opus");
fs::rename("aud.opus", "aud_encoded") fs::rename("aud.opus", "aud_encoded")
.expect("encode: unable to move aud.opus to aud_encoded"); .expect("encode: unable to move aud.opus to aud_encoded");
fs::create_dir("vid_encoded").expect("encode: unable to modify files"); }
let _ = fs::create_dir("vid_encoded");
let dir = fs::read_dir("vid") let dir = fs::read_dir("vid")
.expect("encode: unable to read files") .expect("encode: unable to read files")
.count(); .count();
let running = Arc::new(Mutex::new(0)); let running = Arc::new(Mutex::new(0));
println!("encode: encoding gifs..."); println!("encode: encoding gifs...");
for n in 0..((dir as f32 / (25.0 * 5.0)).ceil() as usize) { for n in 0..((dir as f32 / (25.0 * 5.0)).ceil() as usize) {
*running.lock().unwrap() += 1;
{ {
let running = running.clone(); let running = running.clone();
thread::spawn(move || { thread::spawn(move || {
*running.lock().unwrap() += 1;
let mut image = File::create(format!("vid_encoded/{n}")) let mut image = File::create(format!("vid_encoded/{n}"))
.expect("encode: unable to create gif file"); .expect("encode: unable to create gif file");
let mut encoder = Some( let mut encoder = Some(
@ -438,6 +439,7 @@ async fn main() {
while *running.lock().unwrap() >= 6 { while *running.lock().unwrap() >= 6 {
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
} }
tokio::time::sleep(Duration::from_millis(500)).await;
} }
while *running.lock().unwrap() != 0 { while *running.lock().unwrap() != 0 {
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;