ffmpeg to generate video previews!

This commit is contained in:
Brandon Thomas committed 2021-05-25 01:27:26 -04:00
1 parent aacc6e5fb9
commit d45b1f6fb2
5 files changed
+168 -1

No files matched your search

@@ -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
@@ -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,
@@ -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(())
}
}
+1
View File
@@ -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;
+50 -1
View File
@@ -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<PathBuf> = None;
let mut maybe_image_preview_object_name : Option<String> = None;
let mut maybe_video_preview_filename : Option<PathBuf> = None;
let mut maybe_video_preview_object_name : Option<String> = 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,