2021-10-23 19:14:12 +00:00
|
|
|
use crate::{
|
2023-02-04 23:32:36 +00:00
|
|
|
config::{AudioCodec, ImageFormat, MediaConfiguration, VideoCodec},
|
2021-10-23 19:14:12 +00:00
|
|
|
error::{Error, UploadError},
|
2022-09-26 01:39:09 +00:00
|
|
|
magick::{Details, ValidInputType},
|
2021-10-23 19:14:12 +00:00
|
|
|
process::Process,
|
2023-06-20 20:59:08 +00:00
|
|
|
store::{Store, StoreError},
|
2021-10-23 19:14:12 +00:00
|
|
|
};
|
2021-09-04 00:53:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
2023-02-25 18:59:47 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use std::collections::HashSet;
|
2022-09-26 00:34:51 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
2021-08-26 02:46:11 +00:00
|
|
|
|
2023-02-04 23:32:36 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct TranscodeOptions {
|
|
|
|
input_format: VideoFormat,
|
|
|
|
output: TranscodeOutputOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum TranscodeOutputOptions {
|
|
|
|
Gif,
|
|
|
|
Video {
|
|
|
|
video_codec: VideoCodec,
|
|
|
|
audio_codec: Option<AudioCodec>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TranscodeOptions {
|
|
|
|
pub(crate) fn new(
|
|
|
|
media: &MediaConfiguration,
|
|
|
|
details: &Details,
|
|
|
|
input_format: VideoFormat,
|
|
|
|
) -> Self {
|
|
|
|
if let VideoFormat::Gif = input_format {
|
|
|
|
if details.width <= media.gif.max_width
|
|
|
|
&& details.height <= media.gif.max_height
|
|
|
|
&& details.width * details.height <= media.gif.max_area
|
2023-02-04 23:52:23 +00:00
|
|
|
&& details.frames.unwrap_or(1) <= media.gif.max_frame_count
|
2023-02-04 23:32:36 +00:00
|
|
|
{
|
|
|
|
return Self {
|
|
|
|
input_format,
|
|
|
|
output: TranscodeOutputOptions::gif(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
input_format,
|
|
|
|
output: TranscodeOutputOptions::video(media),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 19:25:22 +00:00
|
|
|
pub(crate) const fn needs_reencode(&self) -> bool {
|
|
|
|
!matches!(
|
|
|
|
(self.input_format, &self.output),
|
|
|
|
(VideoFormat::Gif, TranscodeOutputOptions::Gif)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-04 23:32:36 +00:00
|
|
|
const fn input_file_extension(&self) -> &'static str {
|
|
|
|
self.input_format.to_file_extension()
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn output_ffmpeg_format(&self) -> &'static str {
|
|
|
|
match self.output {
|
|
|
|
TranscodeOutputOptions::Gif => "gif",
|
|
|
|
TranscodeOutputOptions::Video { video_codec, .. } => {
|
|
|
|
video_codec.to_output_format().to_ffmpeg_format()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn output_file_extension(&self) -> &'static str {
|
|
|
|
match self.output {
|
|
|
|
TranscodeOutputOptions::Gif => ".gif",
|
|
|
|
TranscodeOutputOptions::Video { video_codec, .. } => {
|
|
|
|
video_codec.to_output_format().to_file_extension()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:59:47 +00:00
|
|
|
const fn supports_alpha(&self) -> bool {
|
2023-02-25 19:01:55 +00:00
|
|
|
matches!(
|
|
|
|
self.output,
|
2023-02-25 18:59:47 +00:00
|
|
|
TranscodeOutputOptions::Gif
|
2023-02-25 19:01:55 +00:00
|
|
|
| TranscodeOutputOptions::Video {
|
|
|
|
video_codec: VideoCodec::Vp8 | VideoCodec::Vp9,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
)
|
2023-02-25 18:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(
|
|
|
|
&self,
|
|
|
|
input_path: &str,
|
|
|
|
output_path: &str,
|
|
|
|
alpha: bool,
|
|
|
|
) -> Result<Process, std::io::Error> {
|
2023-02-25 20:31:57 +00:00
|
|
|
match self.output {
|
|
|
|
TranscodeOutputOptions::Gif => Process::run("ffmpeg", &[
|
|
|
|
"-hide_banner",
|
2023-06-19 19:25:22 +00:00
|
|
|
"-v",
|
|
|
|
"warning",
|
2023-02-25 20:31:57 +00:00
|
|
|
"-i",
|
|
|
|
input_path,
|
|
|
|
"-filter_complex",
|
2023-06-19 19:25:22 +00:00
|
|
|
"[0:v] split [a][b]; [a] palettegen=stats_mode=single [p]; [b][p] paletteuse=new=1",
|
2023-02-25 20:31:57 +00:00
|
|
|
"-an",
|
|
|
|
"-f",
|
|
|
|
self.output_ffmpeg_format(),
|
|
|
|
output_path
|
|
|
|
]),
|
|
|
|
TranscodeOutputOptions::Video {
|
|
|
|
video_codec,
|
|
|
|
audio_codec: None,
|
|
|
|
} => Process::run(
|
2023-02-04 23:32:36 +00:00
|
|
|
"ffmpeg",
|
|
|
|
&[
|
2023-02-25 20:31:57 +00:00
|
|
|
"-hide_banner",
|
2023-06-19 19:25:22 +00:00
|
|
|
"-v",
|
|
|
|
"warning",
|
2023-02-04 23:32:36 +00:00
|
|
|
"-i",
|
|
|
|
input_path,
|
|
|
|
"-pix_fmt",
|
2023-02-25 20:31:57 +00:00
|
|
|
video_codec.pix_fmt(alpha),
|
2023-02-04 23:32:36 +00:00
|
|
|
"-vf",
|
|
|
|
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
|
2023-02-25 20:31:57 +00:00
|
|
|
"-an",
|
2023-02-04 23:32:36 +00:00
|
|
|
"-c:v",
|
2023-02-25 20:31:57 +00:00
|
|
|
video_codec.to_ffmpeg_codec(),
|
2023-02-04 23:32:36 +00:00
|
|
|
"-f",
|
|
|
|
self.output_ffmpeg_format(),
|
|
|
|
output_path,
|
|
|
|
],
|
2023-02-25 20:31:57 +00:00
|
|
|
),
|
|
|
|
TranscodeOutputOptions::Video {
|
|
|
|
video_codec,
|
|
|
|
audio_codec: Some(audio_codec),
|
|
|
|
} => Process::run(
|
2023-02-04 23:32:36 +00:00
|
|
|
"ffmpeg",
|
|
|
|
&[
|
2023-02-25 20:31:57 +00:00
|
|
|
"-hide_banner",
|
2023-06-19 19:25:22 +00:00
|
|
|
"-v",
|
|
|
|
"warning",
|
2023-02-04 23:32:36 +00:00
|
|
|
"-i",
|
|
|
|
input_path,
|
|
|
|
"-pix_fmt",
|
2023-02-25 20:31:57 +00:00
|
|
|
video_codec.pix_fmt(alpha),
|
2023-02-04 23:32:36 +00:00
|
|
|
"-vf",
|
|
|
|
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
|
2023-02-25 20:31:57 +00:00
|
|
|
"-c:a",
|
|
|
|
audio_codec.to_ffmpeg_codec(),
|
2023-02-04 23:32:36 +00:00
|
|
|
"-c:v",
|
2023-02-25 20:31:57 +00:00
|
|
|
video_codec.to_ffmpeg_codec(),
|
2023-02-04 23:32:36 +00:00
|
|
|
"-f",
|
|
|
|
self.output_ffmpeg_format(),
|
|
|
|
output_path,
|
|
|
|
],
|
2023-02-25 20:31:57 +00:00
|
|
|
),
|
2023-02-04 23:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) const fn output_type(&self) -> ValidInputType {
|
|
|
|
match self.output {
|
|
|
|
TranscodeOutputOptions::Gif => ValidInputType::Gif,
|
|
|
|
TranscodeOutputOptions::Video { video_codec, .. } => {
|
|
|
|
ValidInputType::from_video_codec(video_codec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TranscodeOutputOptions {
|
|
|
|
fn video(media: &MediaConfiguration) -> Self {
|
|
|
|
Self::Video {
|
|
|
|
video_codec: media.video_codec,
|
|
|
|
audio_codec: if media.enable_full_video {
|
|
|
|
Some(
|
|
|
|
media
|
|
|
|
.audio_codec
|
|
|
|
.unwrap_or(media.video_codec.to_output_format().default_audio_codec()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn gif() -> Self {
|
|
|
|
Self::Gif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:38:11 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2022-10-02 02:17:18 +00:00
|
|
|
pub(crate) enum VideoFormat {
|
2021-08-31 02:19:47 +00:00
|
|
|
Gif,
|
|
|
|
Mp4,
|
2022-09-26 00:34:51 +00:00
|
|
|
Webm,
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 00:38:11 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub(crate) enum OutputFormat {
|
|
|
|
Mp4,
|
|
|
|
Webm,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2021-08-28 22:15:14 +00:00
|
|
|
pub(crate) enum ThumbnailFormat {
|
|
|
|
Jpeg,
|
2021-08-31 02:19:47 +00:00
|
|
|
// Webp,
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub(crate) enum FileFormat {
|
|
|
|
Image(ImageFormat),
|
|
|
|
Video(VideoFormat),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidInputType {
|
|
|
|
pub(crate) fn to_file_format(self) -> FileFormat {
|
|
|
|
match self {
|
|
|
|
Self::Gif => FileFormat::Video(VideoFormat::Gif),
|
|
|
|
Self::Mp4 => FileFormat::Video(VideoFormat::Mp4),
|
|
|
|
Self::Webm => FileFormat::Video(VideoFormat::Webm),
|
|
|
|
Self::Jpeg => FileFormat::Image(ImageFormat::Jpeg),
|
|
|
|
Self::Png => FileFormat::Image(ImageFormat::Png),
|
|
|
|
Self::Webp => FileFormat::Image(ImageFormat::Webp),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VideoFormat {
|
2022-10-01 01:00:14 +00:00
|
|
|
const fn to_file_extension(self) -> &'static str {
|
2021-08-31 02:19:47 +00:00
|
|
|
match self {
|
2022-10-01 00:38:11 +00:00
|
|
|
Self::Gif => ".gif",
|
|
|
|
Self::Mp4 => ".mp4",
|
|
|
|
Self::Webm => ".webm",
|
2022-09-26 00:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
fn to_mime(self) -> mime::Mime {
|
2022-09-26 00:34:51 +00:00
|
|
|
match self {
|
2022-09-26 01:39:09 +00:00
|
|
|
Self::Gif => mime::IMAGE_GIF,
|
|
|
|
Self::Mp4 => crate::magick::video_mp4(),
|
|
|
|
Self::Webm => crate::magick::video_webm(),
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ThumbnailFormat {
|
2022-10-01 00:38:11 +00:00
|
|
|
const fn as_ffmpeg_codec(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Jpeg => "mjpeg",
|
|
|
|
// Self::Webp => "webp",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 01:00:14 +00:00
|
|
|
const fn to_file_extension(self) -> &'static str {
|
2022-10-01 00:38:11 +00:00
|
|
|
match self {
|
|
|
|
Self::Jpeg => ".jpeg",
|
|
|
|
// Self::Webp => ".webp",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn as_ffmpeg_format(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Jpeg => "image2",
|
|
|
|
// Self::Webp => "webp",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutputFormat {
|
|
|
|
const fn to_ffmpeg_format(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Mp4 => "mp4",
|
|
|
|
Self::Webm => "webm",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn default_audio_codec(self) -> AudioCodec {
|
2021-08-28 22:15:14 +00:00
|
|
|
match self {
|
2022-10-01 00:38:11 +00:00
|
|
|
Self::Mp4 => AudioCodec::Aac,
|
|
|
|
Self::Webm => AudioCodec::Opus,
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-01 01:00:14 +00:00
|
|
|
|
|
|
|
const fn to_file_extension(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Mp4 => ".mp4",
|
|
|
|
Self::Webm => ".webm",
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 00:38:11 +00:00
|
|
|
}
|
2021-08-28 22:15:14 +00:00
|
|
|
|
2022-10-01 00:38:11 +00:00
|
|
|
impl VideoCodec {
|
|
|
|
const fn to_output_format(self) -> OutputFormat {
|
2021-10-23 19:14:12 +00:00
|
|
|
match self {
|
2022-10-01 00:38:11 +00:00
|
|
|
Self::H264 | Self::H265 => OutputFormat::Mp4,
|
|
|
|
Self::Av1 | Self::Vp8 | Self::Vp9 => OutputFormat::Webm,
|
2021-10-23 19:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:38:11 +00:00
|
|
|
const fn to_ffmpeg_codec(self) -> &'static str {
|
2021-08-28 22:15:14 +00:00
|
|
|
match self {
|
2022-10-01 00:38:11 +00:00
|
|
|
Self::Av1 => "av1",
|
|
|
|
Self::H264 => "h264",
|
|
|
|
Self::H265 => "hevc",
|
|
|
|
Self::Vp8 => "vp8",
|
|
|
|
Self::Vp9 => "vp9",
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 20:31:57 +00:00
|
|
|
|
|
|
|
const fn pix_fmt(&self, alpha: bool) -> &'static str {
|
|
|
|
match (self, alpha) {
|
|
|
|
(VideoCodec::Vp8 | VideoCodec::Vp9, true) => "yuva420p",
|
|
|
|
_ => "yuv420p",
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 00:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioCodec {
|
|
|
|
const fn to_ffmpeg_codec(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Aac => "aac",
|
2023-03-10 03:29:56 +00:00
|
|
|
Self::Opus => "libopus",
|
2022-10-01 00:38:11 +00:00
|
|
|
Self::Vorbis => "vorbis",
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
const FORMAT_MAPPINGS: &[(&str, VideoFormat)] = &[
|
|
|
|
("gif", VideoFormat::Gif),
|
|
|
|
("mp4", VideoFormat::Mp4),
|
|
|
|
("webm", VideoFormat::Webm),
|
2022-09-26 00:34:51 +00:00
|
|
|
];
|
|
|
|
|
2023-02-04 23:32:36 +00:00
|
|
|
pub(crate) async fn input_type_bytes(
|
|
|
|
input: Bytes,
|
|
|
|
) -> Result<Option<(Details, ValidInputType)>, Error> {
|
2022-09-26 01:39:09 +00:00
|
|
|
if let Some(details) = details_bytes(input).await? {
|
2023-02-04 23:32:36 +00:00
|
|
|
let input_type = details.validate_input()?;
|
|
|
|
return Ok(Some((details, input_type)));
|
2022-09-26 01:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn details_store<S: Store>(
|
|
|
|
store: &S,
|
|
|
|
identifier: &S::Identifier,
|
|
|
|
) -> Result<Option<Details>, Error> {
|
2022-09-30 22:43:40 +00:00
|
|
|
details_file(move |mut tmp_one| async move {
|
|
|
|
let stream = store.to_stream(identifier, None, None).await?;
|
|
|
|
tmp_one.write_from_stream(stream).await?;
|
|
|
|
Ok(tmp_one)
|
|
|
|
})
|
|
|
|
.await
|
2022-09-26 01:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn details_bytes(input: Bytes) -> Result<Option<Details>, Error> {
|
2022-09-30 22:43:40 +00:00
|
|
|
details_file(move |mut tmp_one| async move {
|
|
|
|
tmp_one.write_from_bytes(input).await?;
|
|
|
|
Ok(tmp_one)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:59:47 +00:00
|
|
|
async fn alpha_pixel_formats() -> Result<HashSet<String>, Error> {
|
|
|
|
let process = Process::run(
|
|
|
|
"ffprobe",
|
|
|
|
&[
|
|
|
|
"-v",
|
|
|
|
"0",
|
|
|
|
"-show_entries",
|
|
|
|
"pixel_format=name:flags=alpha",
|
|
|
|
"-of",
|
|
|
|
"compact=p=0",
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
process.read().read_to_end(&mut output).await?;
|
|
|
|
let output = String::from_utf8_lossy(&output);
|
|
|
|
let formats = output
|
|
|
|
.split('\n')
|
|
|
|
.filter_map(|format| {
|
|
|
|
if format.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-02-25 19:01:55 +00:00
|
|
|
if !format.ends_with('1') {
|
2023-02-25 18:59:47 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(
|
|
|
|
format
|
|
|
|
.trim_start_matches("name=")
|
|
|
|
.trim_end_matches("|flags:alpha=1")
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(formats)
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[tracing::instrument(skip(f))]
|
2022-09-30 22:43:40 +00:00
|
|
|
async fn details_file<F, Fut>(f: F) -> Result<Option<Details>, Error>
|
|
|
|
where
|
|
|
|
F: FnOnce(crate::file::File) -> Fut,
|
|
|
|
Fut: std::future::Future<Output = Result<crate::file::File, Error>>,
|
|
|
|
{
|
2022-09-26 00:34:51 +00:00
|
|
|
let input_file = crate::tmp_file::tmp_file(None);
|
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
2023-06-20 20:59:08 +00:00
|
|
|
crate::store::file_store::safe_create_parent(&input_file)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2022-09-26 00:34:51 +00:00
|
|
|
|
2022-09-30 22:43:40 +00:00
|
|
|
let tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
let tmp_one = (f)(tmp_one).await?;
|
2022-09-26 00:34:51 +00:00
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
let process = Process::run(
|
|
|
|
"ffprobe",
|
|
|
|
&[
|
2022-09-26 00:34:51 +00:00
|
|
|
"-v",
|
|
|
|
"quiet",
|
2022-09-26 01:39:09 +00:00
|
|
|
"-select_streams",
|
|
|
|
"v:0",
|
|
|
|
"-count_frames",
|
2022-09-26 00:34:51 +00:00
|
|
|
"-show_entries",
|
2022-09-26 01:39:09 +00:00
|
|
|
"stream=width,height,nb_read_frames:format=format_name",
|
2022-09-26 00:34:51 +00:00
|
|
|
"-of",
|
|
|
|
"default=noprint_wrappers=1:nokey=1",
|
|
|
|
input_file_str,
|
2022-09-26 01:39:09 +00:00
|
|
|
],
|
|
|
|
)?;
|
2022-09-26 00:34:51 +00:00
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
process.read().read_to_end(&mut output).await?;
|
2022-09-26 01:39:09 +00:00
|
|
|
let output = String::from_utf8_lossy(&output);
|
|
|
|
tokio::fs::remove_file(input_file_str).await?;
|
|
|
|
|
|
|
|
parse_details(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_details(output: std::borrow::Cow<'_, str>) -> Result<Option<Details>, Error> {
|
2022-10-02 02:17:18 +00:00
|
|
|
tracing::debug!("OUTPUT: {}", output);
|
2022-09-26 01:39:09 +00:00
|
|
|
|
|
|
|
let mut lines = output.lines();
|
|
|
|
|
|
|
|
let width = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2022-09-26 00:34:51 +00:00
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
let height = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let frames = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let formats = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2022-09-26 00:34:51 +00:00
|
|
|
|
|
|
|
for (k, v) in FORMAT_MAPPINGS {
|
|
|
|
if formats.contains(k) {
|
2022-09-26 01:39:09 +00:00
|
|
|
return Ok(Some(parse_details_inner(width, height, frames, *v)?));
|
2022-09-26 00:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
fn parse_details_inner(
|
|
|
|
width: &str,
|
|
|
|
height: &str,
|
|
|
|
frames: &str,
|
2022-10-02 02:17:18 +00:00
|
|
|
format: VideoFormat,
|
2022-09-26 01:39:09 +00:00
|
|
|
) -> Result<Details, Error> {
|
|
|
|
let width = width.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
let height = height.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
let frames = frames.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
|
|
|
|
Ok(Details {
|
|
|
|
mime_type: format.to_mime(),
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames: Some(frames),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:59:47 +00:00
|
|
|
async fn pixel_format(input_file: &str) -> Result<String, Error> {
|
|
|
|
let process = Process::run(
|
|
|
|
"ffprobe",
|
|
|
|
&[
|
|
|
|
"-v",
|
|
|
|
"0",
|
|
|
|
"-select_streams",
|
|
|
|
"v:0",
|
|
|
|
"-show_entries",
|
|
|
|
"stream=pix_fmt",
|
|
|
|
"-of",
|
|
|
|
"compact=p=0:nk=1",
|
|
|
|
input_file,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
process.read().read_to_end(&mut output).await?;
|
|
|
|
Ok(String::from_utf8_lossy(&output).trim().to_string())
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[tracing::instrument(skip(input))]
|
2023-02-04 23:32:36 +00:00
|
|
|
pub(crate) async fn transcode_bytes(
|
2021-09-04 00:53:53 +00:00
|
|
|
input: Bytes,
|
2023-02-04 23:32:36 +00:00
|
|
|
transcode_options: TranscodeOptions,
|
2021-10-23 19:14:12 +00:00
|
|
|
) -> Result<impl AsyncRead + Unpin, Error> {
|
2023-02-04 23:32:36 +00:00
|
|
|
let input_file = crate::tmp_file::tmp_file(Some(transcode_options.input_file_extension()));
|
2021-10-23 19:14:12 +00:00
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
2023-06-20 20:59:08 +00:00
|
|
|
crate::store::file_store::safe_create_parent(&input_file)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
|
2023-02-04 23:32:36 +00:00
|
|
|
let output_file = crate::tmp_file::tmp_file(Some(transcode_options.output_file_extension()));
|
2021-10-23 19:14:12 +00:00
|
|
|
let output_file_str = output_file.to_str().ok_or(UploadError::Path)?;
|
2023-06-20 20:59:08 +00:00
|
|
|
crate::store::file_store::safe_create_parent(&output_file)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one.write_from_bytes(input).await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2023-02-25 18:59:47 +00:00
|
|
|
let alpha = if transcode_options.supports_alpha() {
|
|
|
|
static ALPHA_PIXEL_FORMATS: OnceCell<HashSet<String>> = OnceCell::new();
|
|
|
|
|
|
|
|
let format = pixel_format(input_file_str).await?;
|
|
|
|
|
|
|
|
match ALPHA_PIXEL_FORMATS.get() {
|
|
|
|
Some(alpha_pixel_formats) => alpha_pixel_formats.contains(&format),
|
|
|
|
None => {
|
|
|
|
let pixel_formats = alpha_pixel_formats().await?;
|
|
|
|
let alpha = pixel_formats.contains(&format);
|
|
|
|
let _ = ALPHA_PIXEL_FORMATS.set(pixel_formats);
|
|
|
|
alpha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let process = transcode_options.execute(input_file_str, output_file_str, alpha)?;
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2021-10-23 19:14:12 +00:00
|
|
|
process.wait().await?;
|
|
|
|
tokio::fs::remove_file(input_file).await?;
|
|
|
|
|
|
|
|
let tmp_two = crate::file::File::open(&output_file).await?;
|
2023-06-20 20:59:08 +00:00
|
|
|
let stream = tmp_two
|
|
|
|
.read_to_stream(None, None)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
let reader = tokio_util::io::StreamReader::new(stream);
|
|
|
|
let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, output_file);
|
|
|
|
|
|
|
|
Ok(Box::pin(clean_reader))
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip(store))]
|
2021-10-23 04:48:56 +00:00
|
|
|
pub(crate) async fn thumbnail<S: Store>(
|
|
|
|
store: S,
|
|
|
|
from: S::Identifier,
|
2022-10-02 02:17:18 +00:00
|
|
|
input_format: VideoFormat,
|
2021-08-28 22:15:14 +00:00
|
|
|
format: ThumbnailFormat,
|
2022-03-27 01:45:12 +00:00
|
|
|
) -> Result<impl AsyncRead + Unpin, Error> {
|
2022-10-01 01:00:14 +00:00
|
|
|
let input_file = crate::tmp_file::tmp_file(Some(input_format.to_file_extension()));
|
2021-10-23 19:14:12 +00:00
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
2023-06-20 20:59:08 +00:00
|
|
|
crate::store::file_store::safe_create_parent(&input_file)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
|
2022-10-01 01:00:14 +00:00
|
|
|
let output_file = crate::tmp_file::tmp_file(Some(format.to_file_extension()));
|
2021-10-23 19:14:12 +00:00
|
|
|
let output_file_str = output_file.to_str().ok_or(UploadError::Path)?;
|
2023-06-20 20:59:08 +00:00
|
|
|
crate::store::file_store::safe_create_parent(&output_file)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one
|
|
|
|
.write_from_stream(store.to_stream(&from, None, None).await?)
|
|
|
|
.await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
let process = Process::run(
|
|
|
|
"ffmpeg",
|
|
|
|
&[
|
2023-02-25 20:31:57 +00:00
|
|
|
"-hide_banner",
|
2023-06-19 19:25:22 +00:00
|
|
|
"-v",
|
|
|
|
"warning",
|
2021-10-23 04:48:56 +00:00
|
|
|
"-i",
|
2021-10-28 04:06:03 +00:00
|
|
|
input_file_str,
|
2023-02-04 23:47:03 +00:00
|
|
|
"-frames:v",
|
2021-10-23 04:48:56 +00:00
|
|
|
"1",
|
|
|
|
"-codec",
|
2022-10-01 00:38:11 +00:00
|
|
|
format.as_ffmpeg_codec(),
|
2021-10-23 04:48:56 +00:00
|
|
|
"-f",
|
2022-10-01 00:38:11 +00:00
|
|
|
format.as_ffmpeg_format(),
|
2021-10-28 04:06:03 +00:00
|
|
|
output_file_str,
|
2021-10-23 04:48:56 +00:00
|
|
|
],
|
|
|
|
)?;
|
2021-08-26 02:46:11 +00:00
|
|
|
|
2021-10-23 19:14:12 +00:00
|
|
|
process.wait().await?;
|
|
|
|
tokio::fs::remove_file(input_file).await?;
|
|
|
|
|
|
|
|
let tmp_two = crate::file::File::open(&output_file).await?;
|
2023-06-20 20:59:08 +00:00
|
|
|
let stream = tmp_two
|
|
|
|
.read_to_stream(None, None)
|
|
|
|
.await
|
|
|
|
.map_err(StoreError::from)?;
|
2021-10-23 19:14:12 +00:00
|
|
|
let reader = tokio_util::io::StreamReader::new(stream);
|
|
|
|
let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, output_file);
|
|
|
|
|
|
|
|
Ok(Box::pin(clean_reader))
|
2021-08-26 02:46:11 +00:00
|
|
|
}
|