mirror of
https://git.asonix.dog/asonix/pict-rs.git
synced 2024-12-28 05:10:36 +00:00
Enable mp4 with ffmpeg
This commit is contained in:
parent
db3024489e
commit
a746793187
14 changed files with 746 additions and 229 deletions
78
Cargo.lock
generated
78
Cargo.lock
generated
|
@ -469,6 +469,30 @@ dependencies = [
|
||||||
"which",
|
"which",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bindgen"
|
||||||
|
version = "0.54.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "66c0bb6167449588ff70803f4127f0684f9063097eca5016f37eb52b92c2cf36"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"cexpr",
|
||||||
|
"cfg-if",
|
||||||
|
"clang-sys",
|
||||||
|
"clap",
|
||||||
|
"env_logger",
|
||||||
|
"lazy_static",
|
||||||
|
"lazycell",
|
||||||
|
"log",
|
||||||
|
"peeking_take_while",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"regex",
|
||||||
|
"rustc-hash",
|
||||||
|
"shlex",
|
||||||
|
"which",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
|
@ -631,12 +655,6 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "color_quant"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "copyless"
|
name = "copyless"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
@ -765,6 +783,31 @@ version = "0.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
|
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ffmpeg-next"
|
||||||
|
version = "4.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e37dadc7853e912107a8a671b7b22bff1309c09d0ba8851493028f06a36fc9f"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"ffmpeg-sys-next",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ffmpeg-sys-next"
|
||||||
|
version = "4.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "01bba495d04757f7a3e471b3e411759a8968571c4618235bc1c9e1099b4a84d1"
|
||||||
|
dependencies = [
|
||||||
|
"bindgen 0.54.0",
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"num_cpus",
|
||||||
|
"pkg-config",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "flate2"
|
name = "flate2"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
@ -966,16 +1009,6 @@ dependencies = [
|
||||||
"pkg-config",
|
"pkg-config",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "gif"
|
|
||||||
version = "0.10.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "471d90201b3b223f3451cd4ad53e34295f16a1df17b1edf3736d47761c3981af"
|
|
||||||
dependencies = [
|
|
||||||
"color_quant",
|
|
||||||
"lzw",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gimli"
|
name = "gimli"
|
||||||
version = "0.21.0"
|
version = "0.21.0"
|
||||||
|
@ -1206,18 +1239,12 @@ dependencies = [
|
||||||
"linked-hash-map",
|
"linked-hash-map",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lzw"
|
|
||||||
version = "0.10.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "magick_rust"
|
name = "magick_rust"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
source = "git+https://git.asonix.dog/asonix/magick-rust#cd25f601629a2af0cffa5e4767720de69fd256dd"
|
source = "git+https://git.asonix.dog/asonix/magick-rust#cd25f601629a2af0cffa5e4767720de69fd256dd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen 0.53.3",
|
||||||
"libc",
|
"libc",
|
||||||
"pkg-config",
|
"pkg-config",
|
||||||
]
|
]
|
||||||
|
@ -1438,7 +1465,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pict-rs"
|
name = "pict-rs"
|
||||||
version = "0.1.12"
|
version = "0.2.0-alpha.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-form-data",
|
"actix-form-data",
|
||||||
"actix-fs",
|
"actix-fs",
|
||||||
|
@ -1447,8 +1474,9 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64 0.12.2",
|
"base64 0.12.2",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"ffmpeg-next",
|
||||||
|
"ffmpeg-sys-next",
|
||||||
"futures",
|
"futures",
|
||||||
"gif",
|
|
||||||
"magick_rust",
|
"magick_rust",
|
||||||
"mime",
|
"mime",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "pict-rs"
|
name = "pict-rs"
|
||||||
description = "A simple image hosting service"
|
description = "A simple image hosting service"
|
||||||
version = "0.1.12"
|
version = "0.2.0-alpha.1"
|
||||||
authors = ["asonix <asonix@asonix.dog>"]
|
authors = ["asonix <asonix@asonix.dog>"]
|
||||||
license = "AGPL-3.0"
|
license = "AGPL-3.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -19,7 +19,7 @@ anyhow = "1.0"
|
||||||
base64 = "0.12.1"
|
base64 = "0.12.1"
|
||||||
bytes = "0.5"
|
bytes = "0.5"
|
||||||
futures = "0.3.4"
|
futures = "0.3.4"
|
||||||
gif = "0.10.3"
|
ffmpeg-sys-next = "4.3.0"
|
||||||
magick_rust = { version = "0.14.0", git = "https://git.asonix.dog/asonix/magick-rust" }
|
magick_rust = { version = "0.14.0", git = "https://git.asonix.dog/asonix/magick-rust" }
|
||||||
mime = "0.3.1"
|
mime = "0.3.1"
|
||||||
once_cell = "1.4.0"
|
once_cell = "1.4.0"
|
||||||
|
@ -35,3 +35,8 @@ tracing = "0.1.15"
|
||||||
tracing-futures = "0.2.4"
|
tracing-futures = "0.2.4"
|
||||||
tracing-subscriber = { version = "0.2.5", features = ["fmt", "tracing-log"] }
|
tracing-subscriber = { version = "0.2.5", features = ["fmt", "tracing-log"] }
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
|
|
||||||
|
[dependencies.ffmpeg-next]
|
||||||
|
version = "4.3.0"
|
||||||
|
default-features = false
|
||||||
|
features = ["ffmpeg42", "codec", "filter", "device", "format", "resampling", "postprocessing", "software-resampling", "software-scaling"]
|
||||||
|
|
16
README.md
16
README.md
|
@ -93,19 +93,19 @@ pict-rs offers four endpoints:
|
||||||
response format are the same as the `POST /image` endpoint.
|
response format are the same as the `POST /image` endpoint.
|
||||||
- `GET /image/download?url=...` Download an image from a remote server, returning the same JSON
|
- `GET /image/download?url=...` Download an image from a remote server, returning the same JSON
|
||||||
payload as the `POST` endpoint
|
payload as the `POST` endpoint
|
||||||
- `GET /image/{file}` for getting a full-resolution image. `file` here is the `file` key from the
|
- `GET /image/original/{file}` for getting a full-resolution image. `file` here is the `file` key from the
|
||||||
`/image` endpoint's JSON
|
`/image` endpoint's JSON
|
||||||
- `GET /image/{transformations...}/{file}` get a file with transformations applied.
|
- `GET /image/process.{ext}?src={file}&...` get a file with transformations applied.
|
||||||
existing transformations include
|
existing transformations include
|
||||||
- `identity`: apply no changes
|
- `identity=true`: apply no changes
|
||||||
- `blur{float}`: apply a gaussian blur to the file
|
- `blur={float}`: apply a gaussian blur to the file
|
||||||
- `thumbnail{int}`: produce a thumbnail of the image fitting inside an `{int}` by `{int}` square
|
- `thumbnail={int}`: produce a thumbnail of the image fitting inside an `{int}` by `{int}` square
|
||||||
|
Supported `ext` file extensions include `png`, `jpg`, and `webp`
|
||||||
An example of usage could be
|
An example of usage could be
|
||||||
```
|
```
|
||||||
GET /image/thumbnail256/blur3.0/asdf.png
|
GET /image/process.jpg?src=asdf.png&thumbnail=256&blur=3.0
|
||||||
```
|
```
|
||||||
which would create a 256x256px
|
which would create a 256x256px JPEG thumbnail and blur it
|
||||||
thumbnail and blur it
|
|
||||||
- `DELETE /image/delete/{delete_token}/{file}` or `GET /image/delete/{delete_token}/{file}` to delete a file,
|
- `DELETE /image/delete/{delete_token}/{file}` or `GET /image/delete/{delete_token}/{file}` to delete a file,
|
||||||
where `delete_token` and `file` are from the `/image` endpoint's JSON
|
where `delete_token` and `file` are from the `/image` endpoint's JSON
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,16 @@ RUN \
|
||||||
libwebp6:$ARCH \
|
libwebp6:$ARCH \
|
||||||
libwebpdemux2:$ARCH \
|
libwebpdemux2:$ARCH \
|
||||||
libwebpmux3:$ARCH \
|
libwebpmux3:$ARCH \
|
||||||
libgomp1:$ARCH
|
libgomp1:$ARCH \
|
||||||
|
libavcodec-dev:$ARCH \
|
||||||
|
libavfilter-dev:$ARCH \
|
||||||
|
libavdevice-dev:$ARCH \
|
||||||
|
libavformat-dev:$ARCH \
|
||||||
|
libavresample-dev:$ARCH \
|
||||||
|
libavutil-dev:$ARCH \
|
||||||
|
libswscale-dev:$ARCH \
|
||||||
|
libswresample-dev:$ARCH \
|
||||||
|
ffmpeg
|
||||||
|
|
||||||
ENV \
|
ENV \
|
||||||
PATH=$PATH:/opt/build/.cargo/bin \
|
PATH=$PATH:/opt/build/.cargo/bin \
|
||||||
|
|
|
@ -128,6 +128,14 @@ RUN \
|
||||||
libgexiv2-dev:$ARCH \
|
libgexiv2-dev:$ARCH \
|
||||||
libxml2:$ARCH \
|
libxml2:$ARCH \
|
||||||
libltdl7:$ARCH \
|
libltdl7:$ARCH \
|
||||||
|
libavcodec-dev:$ARCH \
|
||||||
|
libavfilter-dev:$ARCH \
|
||||||
|
libavdevice-dev:$ARCH \
|
||||||
|
libavformat-dev:$ARCH \
|
||||||
|
libavresample-dev:$ARCH \
|
||||||
|
libavutil-dev:$ARCH \
|
||||||
|
libswscale-dev:$ARCH \
|
||||||
|
libswresample-dev:$ARCH \
|
||||||
llvm-dev \
|
llvm-dev \
|
||||||
libclang-dev \
|
libclang-dev \
|
||||||
clang
|
clang
|
||||||
|
@ -192,6 +200,14 @@ RUN \
|
||||||
libltdl7 \
|
libltdl7 \
|
||||||
libgomp1 \
|
libgomp1 \
|
||||||
libxml2 \
|
libxml2 \
|
||||||
|
libavcodec58 \
|
||||||
|
libavfilter7 \
|
||||||
|
libavdevice58 \
|
||||||
|
libavformat58 \
|
||||||
|
libavresample4 \
|
||||||
|
libavutil56 \
|
||||||
|
libswscale5 \
|
||||||
|
libswresample3 \
|
||||||
tini
|
tini
|
||||||
|
|
||||||
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
||||||
|
|
|
@ -128,6 +128,14 @@ RUN \
|
||||||
libgexiv2-dev:$ARCH \
|
libgexiv2-dev:$ARCH \
|
||||||
libxml2:$ARCH \
|
libxml2:$ARCH \
|
||||||
libltdl7:$ARCH \
|
libltdl7:$ARCH \
|
||||||
|
libavcodec-dev:$ARCH \
|
||||||
|
libavfilter-dev:$ARCH \
|
||||||
|
libavdevice-dev:$ARCH \
|
||||||
|
libavformat-dev:$ARCH \
|
||||||
|
libavresample-dev:$ARCH \
|
||||||
|
libavutil-dev:$ARCH \
|
||||||
|
libswscale-dev:$ARCH \
|
||||||
|
libswresample-dev:$ARCH \
|
||||||
llvm-dev \
|
llvm-dev \
|
||||||
libclang-dev \
|
libclang-dev \
|
||||||
clang && \
|
clang && \
|
||||||
|
@ -193,6 +201,14 @@ RUN \
|
||||||
libltdl7 \
|
libltdl7 \
|
||||||
libgomp1 \
|
libgomp1 \
|
||||||
libxml2 \
|
libxml2 \
|
||||||
|
libavcodec58 \
|
||||||
|
libavfilter7 \
|
||||||
|
libavdevice58 \
|
||||||
|
libavformat58 \
|
||||||
|
libavresample4 \
|
||||||
|
libavutil56 \
|
||||||
|
libswscale5 \
|
||||||
|
libswresample3 \
|
||||||
tini
|
tini
|
||||||
|
|
||||||
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
||||||
|
|
|
@ -128,6 +128,14 @@ RUN \
|
||||||
libgexiv2-dev:$ARCH \
|
libgexiv2-dev:$ARCH \
|
||||||
libxml2:$ARCH \
|
libxml2:$ARCH \
|
||||||
libltdl7:$ARCH \
|
libltdl7:$ARCH \
|
||||||
|
libavcodec-dev:$ARCH \
|
||||||
|
libavfilter-dev:$ARCH \
|
||||||
|
libavdevice-dev:$ARCH \
|
||||||
|
libavformat-dev:$ARCH \
|
||||||
|
libavresample-dev:$ARCH \
|
||||||
|
libavutil-dev:$ARCH \
|
||||||
|
libswscale-dev:$ARCH \
|
||||||
|
libswresample-dev:$ARCH \
|
||||||
llvm-dev \
|
llvm-dev \
|
||||||
libclang-dev \
|
libclang-dev \
|
||||||
clang && \
|
clang && \
|
||||||
|
@ -193,6 +201,14 @@ RUN \
|
||||||
libltdl7 \
|
libltdl7 \
|
||||||
libgomp1 \
|
libgomp1 \
|
||||||
libxml2 \
|
libxml2 \
|
||||||
|
libavcodec58 \
|
||||||
|
libavfilter7 \
|
||||||
|
libavdevice58 \
|
||||||
|
libavformat58 \
|
||||||
|
libavresample4 \
|
||||||
|
libavutil56 \
|
||||||
|
libswscale5 \
|
||||||
|
libswresample3 \
|
||||||
tini
|
tini
|
||||||
|
|
||||||
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
COPY --from=pict-rs-builder /opt/build/pict-rs/target/$TARGET/$BUILD_MODE/pict-rs /usr/local/bin/pict-rs
|
||||||
|
|
|
@ -16,6 +16,7 @@ function print_help() {
|
||||||
echo ""
|
echo ""
|
||||||
echo "Args:"
|
echo "Args:"
|
||||||
echo " tag: The git tag to be applied to the repository and docker build"
|
echo " tag: The git tag to be applied to the repository and docker build"
|
||||||
|
echo " branch: The git branch to use for tagging and publishing"
|
||||||
}
|
}
|
||||||
|
|
||||||
function build_image() {
|
function build_image() {
|
||||||
|
@ -30,8 +31,10 @@ function build_image() {
|
||||||
|
|
||||||
# Creating the new tag
|
# Creating the new tag
|
||||||
new_tag="$1"
|
new_tag="$1"
|
||||||
|
branch="$2"
|
||||||
|
|
||||||
require "$new_tag" "tag"
|
require "$new_tag" "tag"
|
||||||
|
require "$branch" "branch"
|
||||||
|
|
||||||
if ! docker run --rm -it arm64v8/alpine:3.11 /bin/sh -c 'echo "docker is configured correctly"'
|
if ! docker run --rm -it arm64v8/alpine:3.11 /bin/sh -c 'echo "docker is configured correctly"'
|
||||||
then
|
then
|
||||||
|
@ -41,7 +44,7 @@ fi
|
||||||
|
|
||||||
set -xe
|
set -xe
|
||||||
|
|
||||||
git checkout master
|
git checkout $branch
|
||||||
|
|
||||||
# Changing the docker-compose prod
|
# Changing the docker-compose prod
|
||||||
sed -i "s/asonix\/pictrs:.*/asonix\/pictrs:$new_tag/" docker-compose.yml
|
sed -i "s/asonix\/pictrs:.*/asonix\/pictrs:$new_tag/" docker-compose.yml
|
||||||
|
|
|
@ -24,9 +24,6 @@ pub(crate) enum UploadError {
|
||||||
#[error("No files present in upload")]
|
#[error("No files present in upload")]
|
||||||
NoFiles,
|
NoFiles,
|
||||||
|
|
||||||
#[error("Uploaded image could not be served, extension is missing")]
|
|
||||||
MissingExtension,
|
|
||||||
|
|
||||||
#[error("Requested a file that doesn't exist")]
|
#[error("Requested a file that doesn't exist")]
|
||||||
MissingAlias,
|
MissingAlias,
|
||||||
|
|
||||||
|
@ -60,6 +57,9 @@ pub(crate) enum UploadError {
|
||||||
#[error("Error validating Gif file, {0}")]
|
#[error("Error validating Gif file, {0}")]
|
||||||
Gif(#[from] GifError),
|
Gif(#[from] GifError),
|
||||||
|
|
||||||
|
#[error("Error transcoding, {0}")]
|
||||||
|
Transcode(crate::validate::transcode::Error),
|
||||||
|
|
||||||
#[error("Tried to create file, but file already exists")]
|
#[error("Tried to create file, but file already exists")]
|
||||||
FileExists,
|
FileExists,
|
||||||
|
|
||||||
|
|
228
src/main.rs
228
src/main.rs
|
@ -21,8 +21,12 @@ mod upload_manager;
|
||||||
mod validate;
|
mod validate;
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
config::Config, error::UploadError, middleware::Tracing, processor::process_image,
|
config::Config,
|
||||||
upload_manager::UploadManager, validate::image_webp,
|
error::UploadError,
|
||||||
|
middleware::Tracing,
|
||||||
|
processor::process_image,
|
||||||
|
upload_manager::UploadManager,
|
||||||
|
validate::{image_webp, video_mp4},
|
||||||
};
|
};
|
||||||
|
|
||||||
const MEGABYTES: usize = 1024 * 1024;
|
const MEGABYTES: usize = 1024 * 1024;
|
||||||
|
@ -31,6 +35,29 @@ const HOURS: u32 = 60 * 60;
|
||||||
static CONFIG: Lazy<Config> = Lazy::new(|| Config::from_args());
|
static CONFIG: Lazy<Config> = Lazy::new(|| Config::from_args());
|
||||||
static MAGICK_INIT: Once = Once::new();
|
static MAGICK_INIT: Once = Once::new();
|
||||||
|
|
||||||
|
// try moving a file
|
||||||
|
#[instrument]
|
||||||
|
async fn safe_move_file(from: PathBuf, to: PathBuf) -> Result<(), UploadError> {
|
||||||
|
if let Some(path) = to.parent() {
|
||||||
|
debug!("Creating directory {:?}", path);
|
||||||
|
actix_fs::create_dir_all(path.to_owned()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Checking if {:?} already exists", to);
|
||||||
|
if let Err(e) = actix_fs::metadata(to.clone()).await {
|
||||||
|
if e.kind() != Some(std::io::ErrorKind::NotFound) {
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(UploadError::FileExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Moving {:?} to {:?}", from, to);
|
||||||
|
actix_fs::copy(from.clone(), to).await?;
|
||||||
|
actix_fs::remove_file(from).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// Try writing to a file
|
// Try writing to a file
|
||||||
#[instrument(skip(bytes))]
|
#[instrument(skip(bytes))]
|
||||||
async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), UploadError> {
|
async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), UploadError> {
|
||||||
|
@ -67,24 +94,57 @@ async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), Upload
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_ext(mime: mime::Mime) -> &'static str {
|
pub(crate) fn tmp_file() -> PathBuf {
|
||||||
|
use rand::distributions::{Alphanumeric, Distribution};
|
||||||
|
let limit: usize = 10;
|
||||||
|
let rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
||||||
|
|
||||||
|
let name = format!("{}.tmp", s);
|
||||||
|
|
||||||
|
let mut path = std::env::temp_dir();
|
||||||
|
path.push("pict-rs");
|
||||||
|
path.push(&name);
|
||||||
|
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_ext(mime: mime::Mime) -> Result<&'static str, UploadError> {
|
||||||
if mime == mime::IMAGE_PNG {
|
if mime == mime::IMAGE_PNG {
|
||||||
".png"
|
Ok(".png")
|
||||||
} else if mime == mime::IMAGE_JPEG {
|
} else if mime == mime::IMAGE_JPEG {
|
||||||
".jpg"
|
Ok(".jpg")
|
||||||
} else if mime == mime::IMAGE_GIF {
|
} else if mime == video_mp4() {
|
||||||
".gif"
|
Ok(".mp4")
|
||||||
|
} else if mime == image_webp() {
|
||||||
|
Ok(".webp")
|
||||||
} else {
|
} else {
|
||||||
".webp"
|
Err(UploadError::UnsupportedFormat)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_ext(ext: std::ffi::OsString) -> mime::Mime {
|
fn from_name(name: &str) -> Result<mime::Mime, UploadError> {
|
||||||
match ext.to_str() {
|
match name
|
||||||
Some("png") => mime::IMAGE_PNG,
|
.rsplit('.')
|
||||||
Some("jpg") => mime::IMAGE_JPEG,
|
.next()
|
||||||
Some("gif") => mime::IMAGE_GIF,
|
.ok_or(UploadError::UnsupportedFormat)?
|
||||||
_ => image_webp(),
|
{
|
||||||
|
"jpg" => Ok(mime::IMAGE_JPEG),
|
||||||
|
"webp" => Ok(image_webp()),
|
||||||
|
"png" => Ok(mime::IMAGE_PNG),
|
||||||
|
"mp4" => Ok(video_mp4()),
|
||||||
|
"gif" => Ok(mime::IMAGE_GIF),
|
||||||
|
_ => Err(UploadError::UnsupportedFormat),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ext(ext: &str) -> Result<mime::Mime, UploadError> {
|
||||||
|
match ext {
|
||||||
|
"jpg" => Ok(mime::IMAGE_JPEG),
|
||||||
|
"png" => Ok(mime::IMAGE_PNG),
|
||||||
|
"webp" => Ok(image_webp()),
|
||||||
|
_ => Err(UploadError::UnsupportedFormat),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +183,11 @@ async fn upload(
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
struct UrlQuery {
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
/// download an image from a URL
|
/// download an image from a URL
|
||||||
#[instrument(skip(client, manager))]
|
#[instrument(skip(client, manager))]
|
||||||
async fn download(
|
async fn download(
|
||||||
|
@ -165,33 +230,71 @@ async fn delete(
|
||||||
Ok(HttpResponse::NoContent().finish())
|
Ok(HttpResponse::NoContent().finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serve files
|
type ProcessQuery = Vec<(String, String)>;
|
||||||
|
|
||||||
|
/// Process files
|
||||||
#[instrument(skip(manager, whitelist))]
|
#[instrument(skip(manager, whitelist))]
|
||||||
async fn serve(
|
async fn process(
|
||||||
segments: web::Path<String>,
|
query: web::Query<ProcessQuery>,
|
||||||
|
ext: web::Path<String>,
|
||||||
manager: web::Data<UploadManager>,
|
manager: web::Data<UploadManager>,
|
||||||
whitelist: web::Data<Option<HashSet<String>>>,
|
whitelist: web::Data<Option<HashSet<String>>>,
|
||||||
) -> Result<HttpResponse, UploadError> {
|
) -> Result<HttpResponse, UploadError> {
|
||||||
let mut segments: Vec<String> = segments
|
let (alias, operations) =
|
||||||
.into_inner()
|
query
|
||||||
.split('/')
|
.into_inner()
|
||||||
.map(|s| s.to_string())
|
.into_iter()
|
||||||
.collect();
|
.fold((String::new(), Vec::new()), |(s, mut acc), (k, v)| {
|
||||||
let alias = segments.pop().ok_or(UploadError::MissingFilename)?;
|
if k == "src" {
|
||||||
|
(v, acc)
|
||||||
|
} else {
|
||||||
|
acc.push((k, v));
|
||||||
|
(s, acc)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
debug!("Building chain");
|
if alias == "" {
|
||||||
let chain = self::processor::build_chain(&segments, whitelist.as_ref().as_ref());
|
return Err(UploadError::MissingFilename);
|
||||||
debug!("Chain built");
|
}
|
||||||
|
|
||||||
let name = manager.from_alias(alias).await?;
|
let name = manager.from_alias(alias).await?;
|
||||||
let base = manager.image_dir();
|
|
||||||
let path = self::processor::build_path(base, &chain, name.clone());
|
|
||||||
|
|
||||||
let ext = path
|
let operations = if let Some(whitelist) = whitelist.as_ref() {
|
||||||
.extension()
|
operations
|
||||||
.ok_or(UploadError::MissingExtension)?
|
.into_iter()
|
||||||
.to_owned();
|
.filter(|(k, _)| whitelist.contains(&k.to_lowercase()))
|
||||||
let ext = from_ext(ext);
|
.collect()
|
||||||
|
} else {
|
||||||
|
operations
|
||||||
|
};
|
||||||
|
|
||||||
|
let chain = self::processor::build_chain(&operations);
|
||||||
|
|
||||||
|
let ext = ext.into_inner();
|
||||||
|
let content_type = from_ext(&ext)?;
|
||||||
|
let processed_name = format!("{}.{}", name, ext);
|
||||||
|
let base = manager.image_dir();
|
||||||
|
let mut path = self::processor::build_path(base, &chain, processed_name);
|
||||||
|
|
||||||
|
if let Some((updated_path, exists)) = self::processor::prepare_image(path.clone()).await? {
|
||||||
|
path = updated_path.clone();
|
||||||
|
|
||||||
|
if exists.is_new() {
|
||||||
|
// Save the transcoded file in another task
|
||||||
|
debug!("Spawning storage task");
|
||||||
|
let span = Span::current();
|
||||||
|
let manager2 = manager.clone();
|
||||||
|
let name = name.clone();
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
let entered = span.enter();
|
||||||
|
if let Err(e) = manager2.store_variant(updated_path, name).await {
|
||||||
|
error!("Error storing variant, {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
drop(entered);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If the thumbnail doesn't exist, we need to create it
|
// If the thumbnail doesn't exist, we need to create it
|
||||||
if let Err(e) = actix_fs::metadata(path.clone()).await {
|
if let Err(e) = actix_fs::metadata(path.clone()).await {
|
||||||
|
@ -203,15 +306,30 @@ async fn serve(
|
||||||
let mut original_path = manager.image_dir();
|
let mut original_path = manager.image_dir();
|
||||||
original_path.push(name.clone());
|
original_path.push(name.clone());
|
||||||
|
|
||||||
// apply chain to the provided image
|
if let Some((updated_path, exists)) =
|
||||||
let img_bytes = match process_image(original_path.clone(), chain).await? {
|
self::processor::prepare_image(original_path.clone()).await?
|
||||||
Some(bytes) => bytes,
|
{
|
||||||
None => {
|
original_path = updated_path.clone();
|
||||||
let stream = actix_fs::read_to_stream(original_path).await?;
|
|
||||||
|
|
||||||
return Ok(srv_response(stream, ext));
|
if exists.is_new() {
|
||||||
|
// Save the transcoded file in another task
|
||||||
|
debug!("Spawning storage task");
|
||||||
|
let span = Span::current();
|
||||||
|
let manager2 = manager.clone();
|
||||||
|
let name = name.clone();
|
||||||
|
actix_rt::spawn(async move {
|
||||||
|
let entered = span.enter();
|
||||||
|
if let Err(e) = manager2.store_variant(updated_path, name).await {
|
||||||
|
error!("Error storing variant, {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
drop(entered);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// apply chain to the provided image
|
||||||
|
let img_bytes = process_image(original_path.clone(), chain).await?;
|
||||||
|
|
||||||
let path2 = path.clone();
|
let path2 = path.clone();
|
||||||
let img_bytes2 = img_bytes.clone();
|
let img_bytes2 = img_bytes.clone();
|
||||||
|
@ -221,7 +339,7 @@ async fn serve(
|
||||||
let span = Span::current();
|
let span = Span::current();
|
||||||
actix_rt::spawn(async move {
|
actix_rt::spawn(async move {
|
||||||
let entered = span.enter();
|
let entered = span.enter();
|
||||||
if let Err(e) = manager.store_variant(path2.clone()).await {
|
if let Err(e) = manager.store_variant(path2.clone(), name).await {
|
||||||
error!("Error storing variant, {}", e);
|
error!("Error storing variant, {}", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -236,13 +354,29 @@ async fn serve(
|
||||||
Box::pin(futures::stream::once(async {
|
Box::pin(futures::stream::once(async {
|
||||||
Ok(img_bytes) as Result<_, UploadError>
|
Ok(img_bytes) as Result<_, UploadError>
|
||||||
})),
|
})),
|
||||||
ext,
|
content_type,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let stream = actix_fs::read_to_stream(path).await?;
|
let stream = actix_fs::read_to_stream(path).await?;
|
||||||
|
|
||||||
Ok(srv_response(stream, ext))
|
Ok(srv_response(stream, content_type))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serve files
|
||||||
|
#[instrument(skip(manager))]
|
||||||
|
async fn serve(
|
||||||
|
alias: web::Path<String>,
|
||||||
|
manager: web::Data<UploadManager>,
|
||||||
|
) -> Result<HttpResponse, UploadError> {
|
||||||
|
let name = manager.from_alias(alias.into_inner()).await?;
|
||||||
|
let content_type = from_name(&name)?;
|
||||||
|
let mut path = manager.image_dir();
|
||||||
|
path.push(name);
|
||||||
|
|
||||||
|
let stream = actix_fs::read_to_stream(path).await?;
|
||||||
|
|
||||||
|
Ok(srv_response(stream, content_type))
|
||||||
}
|
}
|
||||||
|
|
||||||
// A helper method to produce responses with proper cache headers
|
// A helper method to produce responses with proper cache headers
|
||||||
|
@ -261,11 +395,6 @@ where
|
||||||
.streaming(stream.err_into())
|
.streaming(stream.err_into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize)]
|
|
||||||
struct UrlQuery {
|
|
||||||
url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> Result<(), anyhow::Error> {
|
async fn main() -> Result<(), anyhow::Error> {
|
||||||
MAGICK_INIT.call_once(|| {
|
MAGICK_INIT.call_once(|| {
|
||||||
|
@ -368,7 +497,8 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||||
.route(web::delete().to(delete))
|
.route(web::delete().to(delete))
|
||||||
.route(web::get().to(delete)),
|
.route(web::get().to(delete)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/{tail:.*}").route(web::get().to(serve))),
|
.service(web::resource("/original/{filename}").route(web::get().to(serve)))
|
||||||
|
.service(web::resource("/process.{ext}").route(web::get().to(process))),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/import")
|
web::resource("/import")
|
||||||
|
|
148
src/processor.rs
148
src/processor.rs
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use magick_rust::MagickWand;
|
use magick_rust::MagickWand;
|
||||||
use std::{collections::HashSet, path::PathBuf};
|
use std::path::PathBuf;
|
||||||
use tracing::{debug, instrument, Span};
|
use tracing::{debug, instrument, Span};
|
||||||
|
|
||||||
pub(crate) trait Processor {
|
pub(crate) trait Processor {
|
||||||
|
@ -17,21 +17,12 @@ pub(crate) trait Processor {
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
fn parse(s: &str) -> Option<Box<dyn Processor + Send>>
|
fn parse(k: &str, v: &str) -> Option<Box<dyn Processor + Send>>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
fn path(&self, path: PathBuf) -> PathBuf;
|
fn path(&self, path: PathBuf) -> PathBuf;
|
||||||
fn process(&self, wand: &mut MagickWand) -> Result<bool, UploadError>;
|
fn process(&self, wand: &mut MagickWand) -> Result<(), UploadError>;
|
||||||
|
|
||||||
fn is_whitelisted(whitelist: Option<&HashSet<String>>) -> bool
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
whitelist
|
|
||||||
.map(|wl| wl.contains(Self::name()))
|
|
||||||
.unwrap_or(true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Identity;
|
pub(crate) struct Identity;
|
||||||
|
@ -51,7 +42,7 @@ impl Processor for Identity {
|
||||||
s == Self::name()
|
s == Self::name()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(_: &str) -> Option<Box<dyn Processor + Send>>
|
fn parse(_: &str, _: &str) -> Option<Box<dyn Processor + Send>>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
@ -63,8 +54,8 @@ impl Processor for Identity {
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&self, _: &mut MagickWand) -> Result<bool, UploadError> {
|
fn process(&self, _: &mut MagickWand) -> Result<(), UploadError> {
|
||||||
Ok(false)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +73,14 @@ impl Processor for Thumbnail {
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
s.starts_with(Self::name())
|
s == Self::name()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(s: &str) -> Option<Box<dyn Processor + Send>>
|
fn parse(_: &str, v: &str) -> Option<Box<dyn Processor + Send>>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
let size = s.trim_start_matches(Self::name()).parse().ok()?;
|
let size = v.parse().ok()?;
|
||||||
Some(Box::new(Thumbnail(size)))
|
Some(Box::new(Thumbnail(size)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +90,7 @@ impl Processor for Thumbnail {
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&self, wand: &mut MagickWand) -> Result<bool, UploadError> {
|
fn process(&self, wand: &mut MagickWand) -> Result<(), UploadError> {
|
||||||
debug!("Thumbnail");
|
debug!("Thumbnail");
|
||||||
let width = wand.get_image_width();
|
let width = wand.get_image_width();
|
||||||
let height = wand.get_image_height();
|
let height = wand.get_image_height();
|
||||||
|
@ -115,12 +106,9 @@ impl Processor for Thumbnail {
|
||||||
};
|
};
|
||||||
|
|
||||||
wand.op(|w| w.sample_image(new_width as usize, new_height as usize))?;
|
wand.op(|w| w.sample_image(new_width as usize, new_height as usize))?;
|
||||||
Ok(true)
|
|
||||||
} else if wand.op(|w| w.get_image_format())? == "GIF" {
|
|
||||||
Ok(true)
|
|
||||||
} else {
|
|
||||||
Ok(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,11 +123,11 @@ impl Processor for Blur {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_processor(s: &str) -> bool {
|
fn is_processor(s: &str) -> bool {
|
||||||
s.starts_with(Self::name())
|
s == Self::name()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(s: &str) -> Option<Box<dyn Processor + Send>> {
|
fn parse(_: &str, v: &str) -> Option<Box<dyn Processor + Send>> {
|
||||||
let sigma = s.trim_start_matches(Self::name()).parse().ok()?;
|
let sigma = v.parse().ok()?;
|
||||||
Some(Box::new(Blur(sigma)))
|
Some(Box::new(Blur(sigma)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,21 +137,20 @@ impl Processor for Blur {
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&self, wand: &mut MagickWand) -> Result<bool, UploadError> {
|
fn process(&self, wand: &mut MagickWand) -> Result<(), UploadError> {
|
||||||
debug!("Blur");
|
debug!("Blur");
|
||||||
if self.0 > 0.0 {
|
if self.0 > 0.0 {
|
||||||
wand.op(|w| w.gaussian_blur_image(0.0, self.0))?;
|
wand.op(|w| w.gaussian_blur_image(0.0, self.0))?;
|
||||||
Ok(true)
|
|
||||||
} else {
|
|
||||||
Ok(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! parse {
|
macro_rules! parse {
|
||||||
($x:ident, $y:expr, $z:expr) => {{
|
($x:ident, $k:expr, $v:expr) => {{
|
||||||
if $x::is_processor($y) && $x::is_whitelisted($z) {
|
if $x::is_processor($k) {
|
||||||
return $x::parse($y);
|
return $x::parse($k, $v);
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -181,15 +168,18 @@ impl std::fmt::Debug for ProcessChain {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub(crate) fn build_chain(args: &[String], whitelist: Option<&HashSet<String>>) -> ProcessChain {
|
pub(crate) fn build_chain(args: &[(String, String)]) -> ProcessChain {
|
||||||
let inner = args
|
let inner = args
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|arg| {
|
.filter_map(|(k, v)| {
|
||||||
parse!(Identity, arg.as_str(), whitelist);
|
let k = k.as_str();
|
||||||
parse!(Thumbnail, arg.as_str(), whitelist);
|
let v = v.as_str();
|
||||||
parse!(Blur, arg.as_str(), whitelist);
|
|
||||||
|
|
||||||
debug!("Skipping {}, invalid or whitelisted", arg);
|
parse!(Identity, k, v);
|
||||||
|
parse!(Thumbnail, k, v);
|
||||||
|
parse!(Blur, k, v);
|
||||||
|
|
||||||
|
debug!("Skipping {}: {}, invalid", k, v);
|
||||||
|
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
|
@ -208,15 +198,72 @@ pub(crate) fn build_path(base: PathBuf, chain: &ProcessChain, filename: String)
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_motion(s: &str) -> bool {
|
||||||
|
s.ends_with(".gif") || s.ends_with(".mp4")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) enum Exists {
|
||||||
|
Exists,
|
||||||
|
New,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Exists {
|
||||||
|
pub(crate) fn is_new(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Exists::New => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn prepare_image(
|
||||||
|
original_file: PathBuf,
|
||||||
|
) -> Result<Option<(PathBuf, Exists)>, UploadError> {
|
||||||
|
let original_path_str = ptos(&original_file)?;
|
||||||
|
let jpg_path = format!("{}.jpg", original_path_str);
|
||||||
|
let jpg_path = PathBuf::from(jpg_path);
|
||||||
|
|
||||||
|
if actix_fs::metadata(jpg_path.clone()).await.is_ok() {
|
||||||
|
return Ok(Some((jpg_path, Exists::Exists)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_motion(&original_path_str) {
|
||||||
|
let orig_path = original_path_str.clone();
|
||||||
|
|
||||||
|
let tmpfile = crate::tmp_file();
|
||||||
|
let tmpfile2 = tmpfile.clone();
|
||||||
|
|
||||||
|
let res = web::block(move || {
|
||||||
|
use crate::validate::transcode::{transcode, Target};
|
||||||
|
|
||||||
|
transcode(orig_path, tmpfile, Target::Jpeg).map_err(UploadError::Transcode)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
actix_fs::remove_file(tmpfile2).await?;
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
return match crate::safe_move_file(tmpfile2, jpg_path.clone()).await {
|
||||||
|
Err(UploadError::FileExists) => Ok(Some((jpg_path, Exists::Exists))),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
_ => Ok(Some((jpg_path, Exists::New))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub(crate) async fn process_image(
|
pub(crate) async fn process_image(
|
||||||
original_file: PathBuf,
|
original_file: PathBuf,
|
||||||
chain: ProcessChain,
|
chain: ProcessChain,
|
||||||
) -> Result<Option<Bytes>, UploadError> {
|
) -> Result<Bytes, UploadError> {
|
||||||
let original_path_str = ptos(&original_file)?;
|
let original_path_str = ptos(&original_file)?;
|
||||||
let span = Span::current();
|
|
||||||
|
|
||||||
let opt = web::block(move || {
|
let span = Span::current();
|
||||||
|
let bytes = web::block(move || {
|
||||||
let entered = span.enter();
|
let entered = span.enter();
|
||||||
|
|
||||||
let mut wand = MagickWand::new();
|
let mut wand = MagickWand::new();
|
||||||
|
@ -226,23 +273,16 @@ pub(crate) async fn process_image(
|
||||||
let format = wand.op(|w| w.get_image_format())?;
|
let format = wand.op(|w| w.get_image_format())?;
|
||||||
|
|
||||||
debug!("Processing image");
|
debug!("Processing image");
|
||||||
let mut changed = false;
|
|
||||||
|
|
||||||
for processor in chain.inner.into_iter() {
|
for processor in chain.inner.into_iter() {
|
||||||
debug!("Step");
|
debug!("Step");
|
||||||
changed |= processor.process(&mut wand)?;
|
processor.process(&mut wand)?;
|
||||||
debug!("Step complete");
|
|
||||||
}
|
|
||||||
|
|
||||||
if changed {
|
|
||||||
let vec = wand.op(|w| w.write_image_blob(&format))?;
|
|
||||||
return Ok(Some(Bytes::from(vec)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let vec = wand.op(|w| w.write_image_blob(&format))?;
|
||||||
drop(entered);
|
drop(entered);
|
||||||
Ok(None) as Result<Option<Bytes>, UploadError>
|
return Ok(Bytes::from(vec)) as Result<Bytes, UploadError>;
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(opt)
|
Ok(bytes)
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,12 +108,11 @@ impl UploadManager {
|
||||||
|
|
||||||
/// Store the path to a generated image variant so we can easily clean it up later
|
/// Store the path to a generated image variant so we can easily clean it up later
|
||||||
#[instrument(skip(self))]
|
#[instrument(skip(self))]
|
||||||
pub(crate) async fn store_variant(&self, path: PathBuf) -> Result<(), UploadError> {
|
pub(crate) async fn store_variant(
|
||||||
let filename = path
|
&self,
|
||||||
.file_name()
|
path: PathBuf,
|
||||||
.and_then(|f| f.to_str())
|
filename: String,
|
||||||
.map(|s| s.to_string())
|
) -> Result<(), UploadError> {
|
||||||
.ok_or(UploadError::Path)?;
|
|
||||||
let path_string = path.to_str().ok_or(UploadError::Path)?.to_string();
|
let path_string = path.to_str().ok_or(UploadError::Path)?.to_string();
|
||||||
|
|
||||||
let fname_tree = self.inner.filename_tree.clone();
|
let fname_tree = self.inner.filename_tree.clone();
|
||||||
|
@ -276,7 +275,7 @@ impl UploadManager {
|
||||||
{
|
{
|
||||||
// -- READ IN BYTES FROM CLIENT --
|
// -- READ IN BYTES FROM CLIENT --
|
||||||
debug!("Reading stream");
|
debug!("Reading stream");
|
||||||
let tmpfile = tmp_file();
|
let tmpfile = crate::tmp_file();
|
||||||
safe_save_stream(tmpfile.clone(), stream).await?;
|
safe_save_stream(tmpfile.clone(), stream).await?;
|
||||||
|
|
||||||
let content_type = if validate {
|
let content_type = if validate {
|
||||||
|
@ -312,7 +311,7 @@ impl UploadManager {
|
||||||
{
|
{
|
||||||
// -- READ IN BYTES FROM CLIENT --
|
// -- READ IN BYTES FROM CLIENT --
|
||||||
debug!("Reading stream");
|
debug!("Reading stream");
|
||||||
let tmpfile = tmp_file();
|
let tmpfile = crate::tmp_file();
|
||||||
safe_save_stream(tmpfile.clone(), stream).await?;
|
safe_save_stream(tmpfile.clone(), stream).await?;
|
||||||
|
|
||||||
// -- VALIDATE IMAGE --
|
// -- VALIDATE IMAGE --
|
||||||
|
@ -426,7 +425,7 @@ impl UploadManager {
|
||||||
let mut real_path = self.image_dir();
|
let mut real_path = self.image_dir();
|
||||||
real_path.push(name);
|
real_path.push(name);
|
||||||
|
|
||||||
safe_move_file(tmpfile, real_path).await?;
|
crate::safe_move_file(tmpfile, real_path).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -505,7 +504,7 @@ impl UploadManager {
|
||||||
let mut path = image_dir.clone();
|
let mut path = image_dir.clone();
|
||||||
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
||||||
|
|
||||||
let filename = file_name(s, content_type.clone());
|
let filename = file_name(s, content_type.clone())?;
|
||||||
|
|
||||||
path.push(filename.clone());
|
path.push(filename.clone());
|
||||||
|
|
||||||
|
@ -596,7 +595,7 @@ impl UploadManager {
|
||||||
loop {
|
loop {
|
||||||
debug!("Alias gen loop");
|
debug!("Alias gen loop");
|
||||||
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
||||||
let alias = file_name(s, content_type.clone());
|
let alias = file_name(s, content_type.clone())?;
|
||||||
|
|
||||||
let res = self.save_alias(hash, &alias).await?;
|
let res = self.save_alias(hash, &alias).await?;
|
||||||
|
|
||||||
|
@ -635,44 +634,6 @@ impl UploadManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn tmp_file() -> PathBuf {
|
|
||||||
use rand::distributions::{Alphanumeric, Distribution};
|
|
||||||
let limit: usize = 10;
|
|
||||||
let rng = rand::thread_rng();
|
|
||||||
|
|
||||||
let s: String = Alphanumeric.sample_iter(rng).take(limit).collect();
|
|
||||||
|
|
||||||
let name = format!("{}.tmp", s);
|
|
||||||
|
|
||||||
let mut path = std::env::temp_dir();
|
|
||||||
path.push("pict-rs");
|
|
||||||
path.push(&name);
|
|
||||||
|
|
||||||
path
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument]
|
|
||||||
async fn safe_move_file(from: PathBuf, to: PathBuf) -> Result<(), UploadError> {
|
|
||||||
if let Some(path) = to.parent() {
|
|
||||||
debug!("Creating directory {:?}", path);
|
|
||||||
actix_fs::create_dir_all(path.to_owned()).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("Checking if {:?} already exists", to);
|
|
||||||
if let Err(e) = actix_fs::metadata(to.clone()).await {
|
|
||||||
if e.kind() != Some(std::io::ErrorKind::NotFound) {
|
|
||||||
return Err(e.into());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(UploadError::FileExists);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("Moving {:?} to {:?}", from, to);
|
|
||||||
actix_fs::copy(from.clone(), to).await?;
|
|
||||||
actix_fs::remove_file(from).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(stream))]
|
#[instrument(skip(stream))]
|
||||||
async fn safe_save_stream<E>(to: PathBuf, stream: UploadStream<E>) -> Result<(), UploadError>
|
async fn safe_save_stream<E>(to: PathBuf, stream: UploadStream<E>) -> Result<(), UploadError>
|
||||||
where
|
where
|
||||||
|
@ -710,8 +671,8 @@ fn trans_err(e: UploadError) -> sled::transaction::ConflictableTransactionError<
|
||||||
sled::transaction::ConflictableTransactionError::Abort(e)
|
sled::transaction::ConflictableTransactionError::Abort(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn file_name(name: String, content_type: mime::Mime) -> String {
|
fn file_name(name: String, content_type: mime::Mime) -> Result<String, UploadError> {
|
||||||
format!("{}{}", name, to_ext(content_type))
|
Ok(format!("{}{}", name, to_ext(content_type)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alias_key(hash: &[u8], id: &str) -> Vec<u8> {
|
fn alias_key(hash: &[u8], id: &str) -> Vec<u8> {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use crate::{config::Format, error::UploadError, upload_manager::tmp_file};
|
use crate::{config::Format, error::UploadError, tmp_file};
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use magick_rust::MagickWand;
|
use magick_rust::MagickWand;
|
||||||
use rexiv2::{MediaType, Metadata};
|
use rexiv2::{MediaType, Metadata};
|
||||||
use std::{
|
use std::path::PathBuf;
|
||||||
fs::File,
|
use tracing::{debug, error, instrument, warn, Span};
|
||||||
io::{BufReader, BufWriter, Write},
|
|
||||||
path::PathBuf,
|
pub(crate) mod transcode;
|
||||||
};
|
|
||||||
use tracing::{debug, error, instrument, trace, warn, Span};
|
use self::transcode::{Error as TranscodeError, Target};
|
||||||
|
|
||||||
pub(crate) trait Op {
|
pub(crate) trait Op {
|
||||||
fn op<F, T>(&self, f: F) -> Result<T, UploadError>
|
fn op<F, T>(&self, f: F) -> Result<T, UploadError>
|
||||||
|
@ -57,10 +57,10 @@ impl Op for MagickWand {
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub(crate) enum GifError {
|
pub(crate) enum GifError {
|
||||||
#[error("Error decoding gif")]
|
#[error("{0}")]
|
||||||
Decode(#[from] gif::DecodingError),
|
Decode(#[from] TranscodeError),
|
||||||
|
|
||||||
#[error("Error reading bytes")]
|
#[error("{0}")]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +68,10 @@ pub(crate) fn image_webp() -> mime::Mime {
|
||||||
"image/webp".parse().unwrap()
|
"image/webp".parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn video_mp4() -> mime::Mime {
|
||||||
|
"video/mp4".parse().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn ptos(p: &PathBuf) -> Result<String, UploadError> {
|
pub(crate) fn ptos(p: &PathBuf) -> Result<String, UploadError> {
|
||||||
Ok(p.to_str().ok_or(UploadError::Path)?.to_owned())
|
Ok(p.to_str().ok_or(UploadError::Path)?.to_owned())
|
||||||
}
|
}
|
||||||
|
@ -103,7 +107,7 @@ pub(crate) async fn validate_image(
|
||||||
let newfile = tmp_file();
|
let newfile = tmp_file();
|
||||||
validate_gif(&tmpfile, &newfile)?;
|
validate_gif(&tmpfile, &newfile)?;
|
||||||
|
|
||||||
mime::IMAGE_GIF
|
video_mp4()
|
||||||
}
|
}
|
||||||
(Some(Format::Jpeg), MediaType::Jpeg) | (None, MediaType::Jpeg) => {
|
(Some(Format::Jpeg), MediaType::Jpeg) | (None, MediaType::Jpeg) => {
|
||||||
validate_format(&tmpfile_str, "JPEG")?;
|
validate_format(&tmpfile_str, "JPEG")?;
|
||||||
|
@ -137,7 +141,10 @@ pub(crate) async fn validate_image(
|
||||||
return Err(UploadError::UnsupportedFormat);
|
return Err(UploadError::UnsupportedFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
wand.op(|w| w.write_image(&newfile_str))?;
|
if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) {
|
||||||
|
std::fs::remove_file(&newfile_str)?;
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fs::rename(&newfile, &tmpfile)?;
|
std::fs::rename(&newfile, &tmpfile)?;
|
||||||
|
@ -156,7 +163,10 @@ pub(crate) async fn validate_image(
|
||||||
wand.op_mut(|w| w.set_image_format(format.to_magick_format()))?;
|
wand.op_mut(|w| w.set_image_format(format.to_magick_format()))?;
|
||||||
|
|
||||||
debug!("writing: {}", newfile_str);
|
debug!("writing: {}", newfile_str);
|
||||||
wand.op(|w| w.write_image(&newfile_str))?;
|
if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) {
|
||||||
|
std::fs::remove_file(&newfile_str)?;
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fs::rename(&newfile, &tmpfile)?;
|
std::fs::rename(&newfile, &tmpfile)?;
|
||||||
|
@ -180,31 +190,12 @@ pub(crate) async fn validate_image(
|
||||||
#[instrument]
|
#[instrument]
|
||||||
fn validate_gif(from: &PathBuf, to: &PathBuf) -> Result<(), GifError> {
|
fn validate_gif(from: &PathBuf, to: &PathBuf) -> Result<(), GifError> {
|
||||||
debug!("Transmuting GIF");
|
debug!("Transmuting GIF");
|
||||||
use gif::{Parameter, SetParameter};
|
|
||||||
|
|
||||||
let mut decoder = gif::Decoder::new(BufReader::new(File::open(from)?));
|
if let Err(e) = self::transcode::transcode(from, to, Target::Mp4) {
|
||||||
|
std::fs::remove_file(to)?;
|
||||||
decoder.set(gif::ColorOutput::Indexed);
|
return Err(e.into());
|
||||||
|
|
||||||
let mut reader = decoder.read_info()?;
|
|
||||||
|
|
||||||
let width = reader.width();
|
|
||||||
let height = reader.height();
|
|
||||||
let global_palette = reader.global_palette().unwrap_or(&[]);
|
|
||||||
|
|
||||||
let mut writer = BufWriter::new(File::create(to)?);
|
|
||||||
let mut encoder = gif::Encoder::new(&mut writer, width, height, global_palette)?;
|
|
||||||
|
|
||||||
gif::Repeat::Infinite.set_param(&mut encoder)?;
|
|
||||||
|
|
||||||
while let Some(frame) = reader.read_next_frame()? {
|
|
||||||
trace!("Writing frame");
|
|
||||||
encoder.write_frame(frame)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
drop(encoder);
|
|
||||||
writer.flush()?;
|
|
||||||
|
|
||||||
std::fs::rename(to, from)?;
|
std::fs::rename(to, from)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
302
src/validate/transcode.rs
Normal file
302
src/validate/transcode.rs
Normal file
|
@ -0,0 +1,302 @@
|
||||||
|
use ffmpeg_next::{
|
||||||
|
self, codec, filter, format, frame, media,
|
||||||
|
util::{format::pixel::Pixel, rational::Rational},
|
||||||
|
};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub(crate) enum Error {
|
||||||
|
#[error("No video stream present")]
|
||||||
|
MissingVideo,
|
||||||
|
|
||||||
|
#[error("Input format is not supported")]
|
||||||
|
UnsupportedFormat,
|
||||||
|
|
||||||
|
#[error("No frame-rate present in input video")]
|
||||||
|
FrameRate,
|
||||||
|
|
||||||
|
#[error("Filter {0} should have been set up by now")]
|
||||||
|
MissingFilter(&'static str),
|
||||||
|
|
||||||
|
#[error("{0}")]
|
||||||
|
Transcode(#[from] ffmpeg_next::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub(crate) enum Target {
|
||||||
|
Mp4,
|
||||||
|
Jpeg,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
Png,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Target {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Target::Mp4 => "mp4",
|
||||||
|
Target::Jpeg => "image2",
|
||||||
|
Target::Png => "image2",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn codec(&self) -> codec::id::Id {
|
||||||
|
match self {
|
||||||
|
Target::Mp4 => codec::id::Id::H264,
|
||||||
|
Target::Jpeg => codec::id::Id::MJPEG,
|
||||||
|
Target::Png => codec::id::Id::PNG,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn frames(&self) -> Option<usize> {
|
||||||
|
match self {
|
||||||
|
Target::Mp4 => None,
|
||||||
|
Target::Jpeg => Some(1),
|
||||||
|
Target::Png => Some(1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pixel_value(pixel: Pixel) -> i32 {
|
||||||
|
let av_px_fmt: ffmpeg_sys_next::AVPixelFormat = pixel.into();
|
||||||
|
unsafe { std::mem::transmute::<_, i32>(av_px_fmt) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn filter(
|
||||||
|
decoder: &codec::decoder::Video,
|
||||||
|
encoder: &codec::encoder::Video,
|
||||||
|
) -> Result<filter::Graph, Error> {
|
||||||
|
let mut filter = filter::Graph::new();
|
||||||
|
|
||||||
|
let aspect = Rational::new(decoder.width() as i32, decoder.height() as i32).reduce();
|
||||||
|
|
||||||
|
let av_px_fmt = pixel_value(decoder.format());
|
||||||
|
|
||||||
|
let args = format!(
|
||||||
|
"video_size={width}x{height}:pix_fmt={pix_fmt}:time_base={time_base_num}/{time_base_den}:pixel_aspect={pix_aspect_num}/{pix_aspect_den}",
|
||||||
|
width=decoder.width(),
|
||||||
|
height=decoder.height(),
|
||||||
|
pix_fmt=av_px_fmt,
|
||||||
|
time_base_num=decoder.time_base().numerator(),
|
||||||
|
time_base_den=decoder.time_base().denominator(),
|
||||||
|
pix_aspect_num=aspect.numerator(),
|
||||||
|
pix_aspect_den=aspect.denominator(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let buffer = filter::find("buffer").ok_or(Error::MissingFilter("buffer"))?;
|
||||||
|
filter.add(&buffer, "in", &args)?;
|
||||||
|
|
||||||
|
let buffersink = filter::find("buffersink").ok_or(Error::MissingFilter("buffersink"))?;
|
||||||
|
let mut out = filter.add(&buffersink, "out", "")?;
|
||||||
|
out.set_pixel_format(encoder.format());
|
||||||
|
|
||||||
|
filter.output("in", 0)?.input("out", 0)?.parse("null")?;
|
||||||
|
filter.validate()?;
|
||||||
|
|
||||||
|
println!("{}", filter.dump());
|
||||||
|
|
||||||
|
if let Some(codec) = encoder.codec() {
|
||||||
|
if !codec
|
||||||
|
.capabilities()
|
||||||
|
.contains(codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE)
|
||||||
|
{
|
||||||
|
filter
|
||||||
|
.get("out")
|
||||||
|
.ok_or(Error::MissingFilter("out"))?
|
||||||
|
.sink()
|
||||||
|
.set_frame_size(encoder.frame_size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Transcoder {
|
||||||
|
stream: usize,
|
||||||
|
filter: filter::Graph,
|
||||||
|
decoder: codec::decoder::Video,
|
||||||
|
encoder: codec::encoder::Video,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transcoder {
|
||||||
|
fn decode(
|
||||||
|
&mut self,
|
||||||
|
packet: &ffmpeg_next::Packet,
|
||||||
|
decoded: &mut frame::Video,
|
||||||
|
) -> Result<bool, ffmpeg_next::Error> {
|
||||||
|
self.decoder.decode(packet, decoded)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_frame(&mut self, decoded: &frame::Video) -> Result<(), Error> {
|
||||||
|
self.filter
|
||||||
|
.get("in")
|
||||||
|
.ok_or(Error::MissingFilter("out"))?
|
||||||
|
.source()
|
||||||
|
.add(decoded)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode(
|
||||||
|
&mut self,
|
||||||
|
decoded: &mut frame::Video,
|
||||||
|
encoded: &mut ffmpeg_next::Packet,
|
||||||
|
octx: &mut format::context::Output,
|
||||||
|
in_time_base: Rational,
|
||||||
|
out_time_base: Rational,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
while let Ok(()) = self
|
||||||
|
.filter
|
||||||
|
.get("out")
|
||||||
|
.ok_or(Error::MissingFilter("out"))?
|
||||||
|
.sink()
|
||||||
|
.frame(decoded)
|
||||||
|
{
|
||||||
|
if let Ok(true) = self.encoder.encode(decoded, encoded) {
|
||||||
|
encoded.set_stream(0);
|
||||||
|
encoded.rescale_ts(in_time_base, out_time_base);
|
||||||
|
encoded.write_interleaved(octx)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(
|
||||||
|
&mut self,
|
||||||
|
decoded: &mut frame::Video,
|
||||||
|
encoded: &mut ffmpeg_next::Packet,
|
||||||
|
octx: &mut format::context::Output,
|
||||||
|
in_time_base: Rational,
|
||||||
|
out_time_base: Rational,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
self.filter
|
||||||
|
.get("in")
|
||||||
|
.ok_or(Error::MissingFilter("in"))?
|
||||||
|
.source()
|
||||||
|
.flush()?;
|
||||||
|
|
||||||
|
self.encode(decoded, encoded, octx, in_time_base, out_time_base)?;
|
||||||
|
|
||||||
|
while let Ok(true) = self.encoder.flush(encoded) {
|
||||||
|
encoded.set_stream(0);
|
||||||
|
encoded.rescale_ts(in_time_base, out_time_base);
|
||||||
|
encoded.write_interleaved(octx)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transcoder(
|
||||||
|
ictx: &mut format::context::Input,
|
||||||
|
octx: &mut format::context::Output,
|
||||||
|
target: Target,
|
||||||
|
) -> Result<Transcoder, Error> {
|
||||||
|
let input = ictx
|
||||||
|
.streams()
|
||||||
|
.best(media::Type::Video)
|
||||||
|
.ok_or(Error::MissingVideo)?;
|
||||||
|
let mut decoder = input.codec().decoder().video()?;
|
||||||
|
let codec_id = target.codec();
|
||||||
|
let codec = ffmpeg_next::encoder::find(codec_id)
|
||||||
|
.ok_or(Error::UnsupportedFormat)?
|
||||||
|
.video()?;
|
||||||
|
let global = octx
|
||||||
|
.format()
|
||||||
|
.flags()
|
||||||
|
.contains(format::flag::Flags::GLOBAL_HEADER);
|
||||||
|
|
||||||
|
decoder.set_parameters(input.parameters())?;
|
||||||
|
|
||||||
|
let mut output = octx.add_stream(codec)?;
|
||||||
|
let mut encoder = output.codec().encoder().video()?;
|
||||||
|
|
||||||
|
if global {
|
||||||
|
encoder.set_flags(codec::flag::Flags::GLOBAL_HEADER);
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder.set_format(
|
||||||
|
codec
|
||||||
|
.formats()
|
||||||
|
.ok_or(Error::UnsupportedFormat)?
|
||||||
|
.next()
|
||||||
|
.ok_or(Error::UnsupportedFormat)?,
|
||||||
|
);
|
||||||
|
encoder.set_bit_rate(decoder.bit_rate());
|
||||||
|
encoder.set_max_bit_rate(decoder.max_bit_rate());
|
||||||
|
|
||||||
|
encoder.set_width(decoder.width());
|
||||||
|
encoder.set_height(decoder.height());
|
||||||
|
encoder.set_bit_rate(decoder.bit_rate());
|
||||||
|
encoder.set_max_bit_rate(decoder.max_bit_rate());
|
||||||
|
encoder.set_time_base(decoder.frame_rate().ok_or(Error::FrameRate)?.invert());
|
||||||
|
output.set_time_base(decoder.time_base());
|
||||||
|
|
||||||
|
let encoder = encoder.open_as(codec)?;
|
||||||
|
output.set_parameters(&encoder);
|
||||||
|
|
||||||
|
let filter = filter(&decoder, &encoder)?;
|
||||||
|
|
||||||
|
Ok(Transcoder {
|
||||||
|
stream: input.index(),
|
||||||
|
filter: filter,
|
||||||
|
decoder: decoder,
|
||||||
|
encoder: encoder,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn transcode<P, Q>(input: P, output: Q, target: Target) -> Result<(), Error>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
Q: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let mut ictx = format::input(&input)?;
|
||||||
|
let mut octx = format::output_as(&output, target.name())?;
|
||||||
|
let mut transcoder = transcoder(&mut ictx, &mut octx, target)?;
|
||||||
|
|
||||||
|
octx.write_header()?;
|
||||||
|
|
||||||
|
let in_time_base = transcoder.decoder.time_base();
|
||||||
|
let out_time_base = octx.stream(0).ok_or(Error::MissingVideo)?.time_base();
|
||||||
|
|
||||||
|
let mut decoded = frame::Video::empty();
|
||||||
|
let mut encoded = ffmpeg_next::Packet::empty();
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for (stream, mut packet) in ictx.packets() {
|
||||||
|
if stream.index() == transcoder.stream {
|
||||||
|
packet.rescale_ts(stream.time_base(), in_time_base);
|
||||||
|
|
||||||
|
if let Ok(true) = transcoder.decode(&packet, &mut decoded) {
|
||||||
|
let timestamp = decoded.timestamp();
|
||||||
|
decoded.set_pts(timestamp);
|
||||||
|
|
||||||
|
transcoder.add_frame(&decoded)?;
|
||||||
|
|
||||||
|
transcoder.encode(
|
||||||
|
&mut decoded,
|
||||||
|
&mut encoded,
|
||||||
|
&mut octx,
|
||||||
|
in_time_base,
|
||||||
|
out_time_base,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.frames().map(|f| count >= f).unwrap_or(false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transcoder.flush(
|
||||||
|
&mut decoded,
|
||||||
|
&mut encoded,
|
||||||
|
&mut octx,
|
||||||
|
in_time_base,
|
||||||
|
out_time_base,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
octx.write_trailer()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue