fix: bound ffmpeg encoding processes

This commit is contained in:
2026-09-24 18:46:09 +08:00
parent 1f9da14772
commit 704b14f05a
2 changed files with 43 additions and 11 deletions
+21 -4
View File
@@ -333,7 +333,7 @@ impl PixivAPI {
let framerate = 1000.0 / median as f64;
let output = out_dir.path().join("ugoira.mp4");
let status = std::process::Command::new("ffmpeg")
let mut child = std::process::Command::new("ffmpeg")
.args([
"-y",
"-framerate",
@@ -357,10 +357,27 @@ impl PixivAPI {
])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.spawn()
.map_err(|e| format!("ffmpeg spawn failed: {e}"))?;
if !status.success() {
return Err(format!("ffmpeg exited with {status}"));
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(300);
loop {
match child
.try_wait()
.map_err(|e| format!("ffmpeg wait failed: {e}"))?
{
Some(status) => {
if !status.success() {
return Err(format!("ffmpeg exited with {status}"));
}
break;
}
None if std::time::Instant::now() >= deadline => {
let _ = child.kill();
let _ = child.wait();
return Err("ffmpeg exceeded 300s".to_string());
}
None => std::thread::sleep(std::time::Duration::from_millis(50)),
}
}
Ok((output.to_string_lossy().into_owned(), out_dir))
})