diff --git a/migrations/2021-05-15-071601_assets/up.sql b/migrations/2021-05-15-071601_assets/up.sql index 2628966066..a96a3a2693 100644 --- a/migrations/2021-05-15-071601_assets/up.sql +++ b/migrations/2021-05-15-071601_assets/up.sql @@ -201,6 +201,10 @@ CREATE TABLE w2l_templates ( -- The "full url" of the cached faces object private_bucket_cached_faces_object_name VARCHAR(255) NOT NULL, + -- The "full url" of the preview image and/or video + maybe_private_bucket_preview_image_object_name VARCHAR(255) DEFAULT NULL, + maybe_private_bucket_preview_video_object_name VARCHAR(255) DEFAULT NULL, + -- For the thumbnail we show. public_bucket_hash CHAR(64) NOT NULL, -- The "full url" version of the path diff --git a/src/job_queries/w2l_download_job_queries.rs b/src/job_queries/w2l_download_job_queries.rs index 155592af82..67d66a0343 100644 --- a/src/job_queries/w2l_download_job_queries.rs +++ b/src/job_queries/w2l_download_job_queries.rs @@ -115,6 +115,8 @@ pub async fn insert_w2l_template(pool: &MySqlPool, private_bucket_hash: &str, private_bucket_object_name: &str, private_bucket_cached_faces_object_name: &str, + maybe_image_preview_object_name: Option<&str>, + maybe_video_preview_object_name: Option<&str>, frame_width: u32, frame_height: u32, frame_count: u64, @@ -142,6 +144,8 @@ SET private_bucket_cached_faces_object_name = ?, public_bucket_hash = 'TODO', public_bucket_object_name = 'TODO', + maybe_private_bucket_preview_image_object_name = ?, + maybe_private_bucket_preview_video_object_name = ?, frame_width = ?, frame_height = ?, frame_count = ?, @@ -157,6 +161,8 @@ SET private_bucket_hash.to_string(), private_bucket_object_name.to_string(), private_bucket_cached_faces_object_name.to_string(), + maybe_image_preview_object_name, + maybe_video_preview_object_name, frame_width, frame_height, frame_count, diff --git a/src/script_execution/ffmpeg_generate_preview_video_command.rs b/src/script_execution/ffmpeg_generate_preview_video_command.rs new file mode 100644 index 0000000000..e4f2f429a4 --- /dev/null +++ b/src/script_execution/ffmpeg_generate_preview_video_command.rs @@ -0,0 +1,107 @@ +use crate::AnyhowResult; +use log::{info,warn}; +use std::process::{Command, Stdio}; +use subprocess::{Popen, PopenConfig, Redirection}; +use std::fs::OpenOptions; + +/// This command is used to generate ffmpeg previews for videos. +#[derive(Clone)] +pub struct FfmpegGeneratePreviewVideoCommand { +} + +impl FfmpegGeneratePreviewVideoCommand { + pub fn new() -> Self { + Self { + } + } + + pub fn execute(&self, + input_video_filename: &str, + output_video_filename: &str, + output_width: u32, + output_height: u32, + spawn_process: bool) -> AnyhowResult<()> + { + let mut command = String::new(); + + let dimensions = format!("{}x{}", output_width, output_height); + + command.push_str("echo 'test'"); + command.push_str(" && "); + command.push_str("ffmpeg"); + command.push_str(" -i "); + command.push_str(&input_video_filename); + command.push_str(" -lossless 0 "); + command.push_str(" -ss 00:00:00 "); + command.push_str(" -t 00:00:03 "); + command.push_str(" -s "); + command.push_str(&dimensions); + command.push_str(" "); // NB: no arg flag for output filename + command.push_str(&output_video_filename); + + info!("Command: {:?}", command); + + let command_parts = [ + "bash", + "-c", + &command + ]; + + if spawn_process { + // NB: This forks and returns immediately. + //let _child_pid = command_builder.spawn()?; + + let stdout_file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(true) + .open("/tmp/ffmpeg_stdout.txt")?; + + let stderr_file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(true) + .open("/tmp/ffmpeg_stderr.txt")?; + + let mut p = Popen::create(&command_parts, PopenConfig { + //stdout: Redirection::Pipe, + //stderr: Redirection::Pipe, + stdout: Redirection::File(stdout_file), + stderr: Redirection::File(stderr_file), + ..Default::default() + })?; + + info!("Pid : {:?}", p.pid()); + + p.detach(); + + } else { + // NB: This is a blocking call. + /*let output = command_builder.output()?; + + info!("Output status: {}", output.status); + info!("Stdout: {:?}", String::from_utf8(output.stdout)); + error!("Stderr: {:?}", String::from_utf8(output.stderr)); + + if !output.status.success() { + bail!("Bad error code: {:?}", output.status); + }*/ + + let mut p = Popen::create(&command_parts, PopenConfig { + //stdout: Redirection::Pipe, + //stderr: Redirection::Pipe, + ..Default::default() + })?; + + info!("Pid : {:?}", p.pid()); + + let exit_status = p.wait()?; + + info!("Exit status: {:?}", exit_status); + } + + Ok(()) + } +} diff --git a/src/script_execution/mod.rs b/src/script_execution/mod.rs index 23c8b87876..4e01cca42a 100644 --- a/src/script_execution/mod.rs +++ b/src/script_execution/mod.rs @@ -1,3 +1,4 @@ +pub mod ffmpeg_generate_preview_video_command; pub mod google_drive_download_command; pub mod wav2lip_inference_command; pub mod wav2lip_process_upload_command; diff --git a/src/w2l_download_job.rs b/src/w2l_download_job.rs index 04ffc45c31..0e6958c9c4 100644 --- a/src/w2l_download_job.rs +++ b/src/w2l_download_job.rs @@ -39,6 +39,7 @@ use std::process::Command; use std::time::Duration; use tempdir::TempDir; use crate::script_execution::wav2lip_process_upload_command::Wav2LipPreprocessClient; +use crate::script_execution::ffmpeg_generate_preview_video_command::FfmpegGeneratePreviewVideoCommand; // Buckets const ENV_ACCESS_KEY : &'static str = "ACCESS_KEY"; @@ -62,6 +63,7 @@ struct Downloader { pub bucket_client: BucketClient, pub google_drive_downloader: GoogleDriveDownloadCommand, pub w2l_processor: Wav2LipPreprocessClient, + pub ffmpeg_preview_generator: FfmpegGeneratePreviewVideoCommand, // Command to run pub download_script: String, // Root to store W2L templates @@ -140,6 +142,7 @@ async fn main() -> AnyhowResult<()> { bucket_client, download_script, google_drive_downloader, + ffmpeg_preview_generator: FfmpegGeneratePreviewVideoCommand {}, w2l_processor: w2l_preprecess_command, bucket_root_w2l_template_uploads: bucket_root.to_string(), }; @@ -289,7 +292,7 @@ async fn process_job(downloader: &Downloader, job: &W2lTemplateUploadJobRecord) let file_metadata = read_metadata_file(&output_metadata_filename)?; - // ==================== UPLOAD TO BUCKET ==================== // + // ==================== BASE OBJECT NAMES BASED ON HASH ==================== // let private_bucket_hash = get_file_hash(&download_filename)?; @@ -300,6 +303,38 @@ async fn process_job(downloader: &Downloader, job: &W2lTemplateUploadJobRecord) &private_bucket_hash, Some(&downloader.bucket_root_w2l_template_uploads))?; + // ==================== GENERATE VIDEO PREVIEWS ==================== // + + let mut maybe_image_preview_filename : Option = None; + let mut maybe_image_preview_object_name : Option = None; + + let mut maybe_video_preview_filename : Option = None; + let mut maybe_video_preview_object_name : Option = None; + + if file_metadata.is_video { + let preview_filename = format!("{}_preview.webp", &download_filename); + + downloader.ffmpeg_preview_generator.execute( + &download_filename, + &preview_filename, + 500, + 500, + false + )?; + + let video_preview_path = PathBuf::from(&preview_filename); + check_file_exists(&video_preview_path)?; + + let preview_object_path = format!("{}_preview.webp", full_object_path); + maybe_video_preview_object_name = Some(preview_object_path); + maybe_video_preview_filename = Some(video_preview_path); + + } else { + // TODO + } + + // ==================== UPLOAD TO BUCKET ==================== // + info!("Image/video destination bucket path: {}", full_object_path); // Full path to cached faces @@ -317,6 +352,18 @@ async fn process_job(downloader: &Downloader, job: &W2lTemplateUploadJobRecord) &full_object_path_cached_faces, &cached_faces_path).await?; + // TODO: Fix this ugh. + if let Some(video_preview_filename) = maybe_video_preview_filename.as_deref() { + if let Some(video_preview_object_name) = maybe_video_preview_object_name.as_deref() { + info!("Uploading video preview..."); + downloader.bucket_client.upload_filename( + &video_preview_object_name, + video_preview_filename).await?; + } + } + + // ==================== SAVE RECORDS ==================== // + let template_type = if file_metadata.is_video { "video" } else { "image" }; info!("Saving model record..."); @@ -327,6 +374,8 @@ async fn process_job(downloader: &Downloader, job: &W2lTemplateUploadJobRecord) &private_bucket_hash, &full_object_path, &full_object_path_cached_faces, + maybe_image_preview_object_name.as_deref(), + maybe_video_preview_object_name.as_deref(), file_metadata.width, file_metadata.height, file_metadata.num_frames,