From d86d9a322863360beab648b2bbd68fab99801bd0 Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 30 Sep 2022 20:00:14 -0500 Subject: [PATCH] Put proper file extension on videos --- docker/object-storage/docker-compose.yml | 2 +- docker/object-storage/pict-rs.toml | 1 + src/ffmpeg.rs | 20 ++++++++++++++------ src/magick.rs | 9 ++++++++- src/validate.rs | 6 +++--- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/docker/object-storage/docker-compose.yml b/docker/object-storage/docker-compose.yml index ac82284..1e402cd 100644 --- a/docker/object-storage/docker-compose.yml +++ b/docker/object-storage/docker-compose.yml @@ -21,7 +21,7 @@ services: - ../../:/opt/app pictrs_proxy: - image: asonix/pictrs-proxy:0.4.0-alpha.4 + image: asonix/pictrs-proxy:0.4.0-beta.1 ports: - "8081:8081" environment: diff --git a/docker/object-storage/pict-rs.toml b/docker/object-storage/pict-rs.toml index cebd98d..84614dd 100644 --- a/docker/object-storage/pict-rs.toml +++ b/docker/object-storage/pict-rs.toml @@ -23,6 +23,7 @@ max_area = 40000000 max_file_size = 40 enable_silent_video = true enable_full_video = true +video_codec = "vp9" filters = ['blur', 'crop', 'identity', 'resize', 'thumbnail'] skip_validate_imports = false diff --git a/src/ffmpeg.rs b/src/ffmpeg.rs index 64f219a..b5f8a9c 100644 --- a/src/ffmpeg.rs +++ b/src/ffmpeg.rs @@ -29,7 +29,7 @@ pub(crate) enum ThumbnailFormat { } impl InputFormat { - const fn to_ext(self) -> &'static str { + const fn to_file_extension(self) -> &'static str { match self { Self::Gif => ".gif", Self::Mp4 => ".mp4", @@ -54,7 +54,7 @@ impl ThumbnailFormat { } } - const fn to_ext(self) -> &'static str { + const fn to_file_extension(self) -> &'static str { match self { Self::Jpeg => ".jpeg", // Self::Webp => ".webp", @@ -83,6 +83,13 @@ impl OutputFormat { Self::Webm => AudioCodec::Opus, } } + + const fn to_file_extension(self) -> &'static str { + match self { + Self::Mp4 => ".mp4", + Self::Webm => ".webm", + } + } } impl VideoCodec { @@ -245,11 +252,12 @@ pub(crate) async fn trancsocde_bytes( video_codec: VideoCodec, audio_codec: Option, ) -> Result { - let input_file = crate::tmp_file::tmp_file(Some(input_format.to_ext())); + let input_file = crate::tmp_file::tmp_file(Some(input_format.to_file_extension())); let input_file_str = input_file.to_str().ok_or(UploadError::Path)?; crate::store::file_store::safe_create_parent(&input_file).await?; - let output_file = crate::tmp_file::tmp_file(Some(".mp4")); + let output_file = + crate::tmp_file::tmp_file(Some(video_codec.to_output_format().to_file_extension())); let output_file_str = output_file.to_str().ok_or(UploadError::Path)?; crate::store::file_store::safe_create_parent(&output_file).await?; @@ -317,11 +325,11 @@ pub(crate) async fn thumbnail( input_format: InputFormat, format: ThumbnailFormat, ) -> Result { - let input_file = crate::tmp_file::tmp_file(Some(input_format.to_ext())); + let input_file = crate::tmp_file::tmp_file(Some(input_format.to_file_extension())); let input_file_str = input_file.to_str().ok_or(UploadError::Path)?; crate::store::file_store::safe_create_parent(&input_file).await?; - let output_file = crate::tmp_file::tmp_file(Some(format.to_ext())); + let output_file = crate::tmp_file::tmp_file(Some(format.to_file_extension())); let output_file_str = output_file.to_str().ok_or(UploadError::Path)?; crate::store::file_store::safe_create_parent(&output_file).await?; diff --git a/src/magick.rs b/src/magick.rs index 75c7d82..24353a7 100644 --- a/src/magick.rs +++ b/src/magick.rs @@ -1,5 +1,5 @@ use crate::{ - config::ImageFormat, + config::{ImageFormat, VideoCodec}, error::{Error, UploadError}, process::Process, repo::Alias, @@ -81,6 +81,13 @@ impl ValidInputType { } } + pub(crate) fn from_video_codec(codec: VideoCodec) -> Self { + match codec { + VideoCodec::Av1 | VideoCodec::Vp8 | VideoCodec::Vp9 => Self::Webm, + VideoCodec::H264 | VideoCodec::H265 => Self::Mp4, + } + } + pub(crate) fn from_format(format: ImageFormat) -> Self { match format { ImageFormat::Jpeg => ValidInputType::Jpeg, diff --git a/src/validate.rs b/src/validate.rs index 4e030d1..f4501df 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -63,7 +63,7 @@ pub(crate) async fn validate_bytes( return Err(UploadError::SilentVideoDisabled.into()); } Ok(( - ValidInputType::Mp4, + ValidInputType::from_video_codec(video_codec), Either::right(Either::left( crate::ffmpeg::trancsocde_bytes( bytes, @@ -81,7 +81,7 @@ pub(crate) async fn validate_bytes( return Err(UploadError::SilentVideoDisabled.into()); } Ok(( - ValidInputType::Mp4, + ValidInputType::from_video_codec(video_codec), Either::right(Either::left( crate::ffmpeg::trancsocde_bytes( bytes, @@ -99,7 +99,7 @@ pub(crate) async fn validate_bytes( return Err(UploadError::SilentVideoDisabled.into()); } Ok(( - ValidInputType::Mp4, + ValidInputType::from_video_codec(video_codec), Either::right(Either::left( crate::ffmpeg::trancsocde_bytes( bytes,