From ee67670a897acce9e1e8e2dbd9c17b53652e9982 Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Fri, 24 Feb 2023 12:22:35 -0500 Subject: [PATCH] [+] Encoder::exec_all --- backend/encoding.toml | 4 ++-- backend/src/encoder.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/encoding.toml b/backend/encoding.toml index 02ee85b..7314700 100644 --- a/backend/encoding.toml +++ b/backend/encoding.toml @@ -2,12 +2,12 @@ processes = 2 [[encoders]] name = "H264 Full VBR35" -cmd = "ffmpeg -i {{INPUT}} -c:v h264_nvenc -cq:v 35 -rc:v vbr -c:a aac -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart {{OUTPUT}}" +cmd = "ffmpeg -i {{INPUT}} -nostdin -c:v h264_nvenc -cq:v 35 -rc:v vbr -c:a aac -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart {{OUTPUT}}" suffix = "h264-vbr35.mp4" [[encoders]] name = "AV1 Full CRF38" -cmd = "ffmpeg -i {{INPUT}} -c:v libsvtav1 -crf 38 -c:a aac -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart {{OUTPUT}}" +cmd = "ffmpeg -i {{INPUT}} -nostdin -c:v libsvtav1 -crf 38 -c:a aac -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart {{OUTPUT}}" suffix = "av1-crf38.mp4" #[[encoders]] diff --git a/backend/src/encoder.rs b/backend/src/encoder.rs index bffb45f..0c82a23 100644 --- a/backend/src/encoder.rs +++ b/backend/src/encoder.rs @@ -3,6 +3,7 @@ use std::path::Path; use std::process::{Command, Output}; use serde::{Deserialize}; use anyhow::{Context, Result}; +use tempdir::TempDir; use crate::utils::run_cmd; #[derive(Deserialize, Debug)] @@ -32,6 +33,28 @@ impl Encoders { let content = fs::read_to_string(file)?; Ok(toml::from_str(&*content)?) } + + pub fn exec_all(&self, orig: &str, out: &Path) -> Result<()> { + for enc in self.encoders { + let enc_out = out.with_extension(enc.suffix); + // Skip if encoded video already exists + if enc_out.exists() { continue } + + // Create tmp (this is to prevent partially completed encoding results being detected as completed) + let tmp_dir = TempDir::new("meow_encoder_tmp")?; + let tmp_out = tmp_dir.path().join(out.file_name().context("No file name")?); + + // Convert to tmp + enc.execute(orig, tmp_out.to_str().context("Call to path.to_str failed")?)?; + + // Copy results + fs::copy(tmp_out, enc_out)?; + + // Cleanup tmp + tmp_dir.close()?; + } + Ok(()) + } }