2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Add transparency to gifs, rustfmt

This commit is contained in:
asonix 2023-02-25 14:31:57 -06:00
parent 277850ba00
commit dc27200a35
2 changed files with 52 additions and 51 deletions

View file

@ -54,23 +54,6 @@ impl TranscodeOptions {
self.input_format.to_file_extension() self.input_format.to_file_extension()
} }
const fn output_ffmpeg_video_codec(&self) -> &'static str {
match self.output {
TranscodeOutputOptions::Gif => "gif",
TranscodeOutputOptions::Video { video_codec, .. } => video_codec.to_ffmpeg_codec(),
}
}
const fn output_ffmpeg_audio_codec(&self) -> Option<&'static str> {
match self.output {
TranscodeOutputOptions::Video {
audio_codec: Some(audio_codec),
..
} => Some(audio_codec.to_ffmpeg_codec()),
_ => None,
}
}
const fn output_ffmpeg_format(&self) -> &'static str { const fn output_ffmpeg_format(&self) -> &'static str {
match self.output { match self.output {
TranscodeOutputOptions::Gif => "gif", TranscodeOutputOptions::Gif => "gif",
@ -106,43 +89,61 @@ impl TranscodeOptions {
output_path: &str, output_path: &str,
alpha: bool, alpha: bool,
) -> Result<Process, std::io::Error> { ) -> Result<Process, std::io::Error> {
if let Some(audio_codec) = self.output_ffmpeg_audio_codec() { match self.output {
Process::run( TranscodeOutputOptions::Gif => Process::run("ffmpeg", &[
"ffmpeg", "-hide_banner",
&[
"-i", "-i",
input_path, input_path,
"-pix_fmt", "-filter_complex",
if alpha { "yuva420p" } else { "yuv420p" }, "[0:v] split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse",
"-vf", "-an",
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-c:a",
audio_codec,
"-c:v",
self.output_ffmpeg_video_codec(),
"-f", "-f",
self.output_ffmpeg_format(), self.output_ffmpeg_format(),
output_path, output_path
], ]),
) TranscodeOutputOptions::Video {
} else { video_codec,
Process::run( audio_codec: None,
} => Process::run(
"ffmpeg", "ffmpeg",
&[ &[
"-hide_banner",
"-i", "-i",
input_path, input_path,
"-pix_fmt", "-pix_fmt",
if alpha { "yuva420p" } else { "yuv420p" }, video_codec.pix_fmt(alpha),
"-vf", "-vf",
"scale=trunc(iw/2)*2:trunc(ih/2)*2", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-an", "-an",
"-c:v", "-c:v",
self.output_ffmpeg_video_codec(), video_codec.to_ffmpeg_codec(),
"-f", "-f",
self.output_ffmpeg_format(), self.output_ffmpeg_format(),
output_path, output_path,
], ],
) ),
TranscodeOutputOptions::Video {
video_codec,
audio_codec: Some(audio_codec),
} => Process::run(
"ffmpeg",
&[
"-hide_banner",
"-i",
input_path,
"-pix_fmt",
video_codec.pix_fmt(alpha),
"-vf",
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-c:a",
audio_codec.to_ffmpeg_codec(),
"-c:v",
video_codec.to_ffmpeg_codec(),
"-f",
self.output_ffmpeg_format(),
output_path,
],
),
} }
} }
@ -296,6 +297,13 @@ impl VideoCodec {
Self::Vp9 => "vp9", Self::Vp9 => "vp9",
} }
} }
const fn pix_fmt(&self, alpha: bool) -> &'static str {
match (self, alpha) {
(VideoCodec::Vp8 | VideoCodec::Vp9, true) => "yuva420p",
_ => "yuv420p",
}
}
} }
impl AudioCodec { impl AudioCodec {
@ -567,6 +575,7 @@ pub(crate) async fn thumbnail<S: Store>(
let process = Process::run( let process = Process::run(
"ffmpeg", "ffmpeg",
&[ &[
"-hide_banner",
"-i", "-i",
input_file_str, input_file_str,
"-frames:v", "-frames:v",

View file

@ -163,11 +163,7 @@ where
} }
#[tracing::instrument(skip(self, hash))] #[tracing::instrument(skip(self, hash))]
async fn add_existing_alias( async fn add_existing_alias(&mut self, hash: &[u8], alias: Alias) -> Result<(), Error> {
&mut self,
hash: &[u8],
alias: Alias,
) -> Result<(), Error> {
AliasRepo::create(&self.repo, &alias) AliasRepo::create(&self.repo, &alias)
.await? .await?
.map_err(|_| UploadError::DuplicateAlias)?; .map_err(|_| UploadError::DuplicateAlias)?;
@ -181,11 +177,7 @@ where
} }
#[tracing::instrument(level = "debug", skip(self, hash))] #[tracing::instrument(level = "debug", skip(self, hash))]
async fn create_alias( async fn create_alias(&mut self, hash: &[u8], input_type: ValidInputType) -> Result<(), Error> {
&mut self,
hash: &[u8],
input_type: ValidInputType,
) -> Result<(), Error> {
loop { loop {
let alias = Alias::generate(input_type.as_ext().to_string()); let alias = Alias::generate(input_type.as_ext().to_string());