Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WasmEdge FFmpeg Plugin to Process Video and Audio Files #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,4 @@
target = "wasm32-wasi"

[target.wasm32-wasi]
runner = "./scripts/wasmedge-runner.sh"

[env]
# ffmpeg install path (default is ```assets/ffmpeg-lib```)
FFMPEG_DIR = { value = "assets/ffmpeg-lib", relative = true }
# wasi sysroot path (default is ```assets/wasi-sysroot```)
WASI_SYSROOT = { value = "assets/wasi-sysroot", relative = true }
# lib clang rt path (default is ```assets/clang-rt```)
CLANG_RT = { value = "assets/clang-rt", relative = true }

# config wasi-sysroot path (default is ```./asset/wasi-sysroot```)
#BINDGEN_EXTRA_CLANG_ARGS = "--sysroot=./asset/wasi-sysroot --target=wasm32-wasi -fvisibility=default"
BINDGEN_EXTRA_CLANG_ARGS = "--sysroot=/opt/wasi-sdk/share/wasi-sysroot --target=wasm32-wasi -fvisibility=default"
runner = "./scripts/wasmedge-runner.sh"
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ lazy_static = { version = "^1", optional = true }
symphonia-core = { version = "^0", optional = true }

[dependencies.ffmpeg-next]
features = ["static"]
optional = true
git = "https://github.com/yanghaku/rust-ffmpeg.git"
branch = "wasm32-wasi"
git = "https://github.com/Hrushi20/rust-ffmpeg"
branch = "master"


[features]
Expand Down
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ from https://storage.googleapis.com/mediapipe-tasks/gesture_recognizer/victory.j
Example output in console:

```console
$ cargo run --release --example gesture_recognition -- ./assets/models/gesture_recognition/gesture_recognizer.task ./assets/testdata/img/gesture_recognition_google_samples/victory.jpg
$ cargo run --release --example gesture_recognition -- ./assets/models/gesture_recognition/gesture_recognizer.task ./assets/testdata/img/victory.jpg
Finished release [optimized] target(s) in 0.02s
Running `/mediapipe-rs/./scripts/wasmedge-runner.sh target/wasm32-wasi/release/examples/gesture_recognition.wasm ./assets/models/gesture_recognition/gesture_recognizer.task ./assets/testdata/img/gesture_recognition_google_samples/victory.jpg`
Category name: "Victory"
Expand Down Expand Up @@ -389,21 +389,10 @@ fn inference(

## Use the FFMPEG feature to process video and audio.

When building the library with ```ffmpeg``` feature using cargo, users must set the following environment variables:
When building the library with ```ffmpeg``` feature using cargo, users must build WasmEdge by enabling FFmpeg Plugin:

* ```FFMPEG_DIR```: the pre-built FFmpeg library path. You can download it from
https://github.com/yanghaku/ffmpeg-wasm32-wasi/releases.
* ```WASI_SDK``` or (```WASI_SYSROOT``` and ```CLANG_RT```), You can download it from
https://github.com/WebAssembly/wasi-sdk/releases
* ```BINDGEN_EXTRA_CLANG_ARGS```: set **sysroot** and **target** and **function visibility** for libclang.
(The sysroot must be **absolute path**).

Example:

```shell
export FFMPEG_DIR=/path/to/ffmpeg/library
export WASI_SDK=/opt/wasi-sdk
export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/opt/wasi-sdk/share/wasi-sysroot --target=wasm32-wasi -fvisibility=default"
```console
$ cmake -DCMAKE_BUILD_TYPE=Release -GNinja -DWASMEDGE_PLUGIN_WASI_NN_BACKEND="TensorflowLite" -DWASMEDGE_PLUGIN_FFMPEG=ON ..

# Then run cargo
```
Expand Down
4 changes: 2 additions & 2 deletions examples/audio_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn read_audio_using_symphonia(audio_path: String) -> SymphoniaAudioData {
}

#[cfg(feature = "ffmpeg")]
fn read_video_using_ffmpeg(audio_path: String) -> FFMpegAudioData {
fn read_audio_using_ffmpeg(audio_path: String) -> FFMpegAudioData {
ffmpeg_next::init().unwrap();
FFMpegAudioData::new(ffmpeg_next::format::input(&audio_path.as_str()).unwrap()).unwrap()
}
Expand All @@ -43,7 +43,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(not(feature = "ffmpeg"))]
let audio = read_audio_using_symphonia(audio_path);
#[cfg(feature = "ffmpeg")]
let audio = read_video_using_ffmpeg(audio_path);
let audio = read_audio_using_ffmpeg(audio_path);

let classification_results = AudioClassifierBuilder::new()
.max_results(3) // set max result
Expand Down
91 changes: 0 additions & 91 deletions scripts/ffmpeg-deps-init.sh

This file was deleted.

12 changes: 7 additions & 5 deletions src/preprocess/vision/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use ffmpeg_next::avUtilTypes::AVPixelFormat;

type FFMpegVideoInput =
common::ffmpeg_input::FFMpegInput<ffmpeg_next::decoder::Video, ffmpeg_next::frame::Video>;
Expand Down Expand Up @@ -33,11 +34,12 @@ impl FFMpegVideoData {
} else {
source.decoder.time_base().numerator()
};

let filter_desc_in = format!(
"buffer=video_size={}x{}:pix_fmt={}:time_base={}/{}:pixel_aspect={}/{}",
source.decoder.width(),
source.decoder.height(),
ffmpeg_next::ffi::AVPixelFormat::from(source.decoder.format()) as u32,
ffmpeg_next::util::format::Pixel::from(source.decoder.format() as AVPixelFormat).mask(),
time_base_num,
source.decoder.time_base().denominator(),
source.decoder.aspect_ratio().numerator(),
Expand Down Expand Up @@ -105,16 +107,16 @@ impl<'a> ImageToTensor for FFMpegFrame<'a> {
}
let out_format = match to_tensor_info.color_space {
ImageColorSpaceType::GRAYSCALE => {
ffmpeg_next::ffi::AVPixelFormat::AV_PIX_FMT_GRAY8 as u32
ffmpeg_next::format::Pixel::GRAY8
}
_ => ffmpeg_next::ffi::AVPixelFormat::AV_PIX_FMT_RGB24 as u32,
_ => ffmpeg_next::format::Pixel::RGB24,
};
desc.extend(
format!(
"[s_in];[s_in]scale={}:{}[f];[f]format=pix_fmts={}[out];[out]buffersink",
to_tensor_info.width(),
to_tensor_info.height(),
out_format
out_format.mask()
)
.chars(),
);
Expand Down Expand Up @@ -158,7 +160,7 @@ impl<'a> ImageToTensor for FFMpegFrame<'a> {
let img = image::ImageBuffer::<image::Rgb<u8>, &[u8]>::from_raw(
to_tensor_info.width(),
to_tensor_info.height(),
data,
&data,
)
.unwrap();
image::rgb8_image_buffer_to_tensor(&img, to_tensor_info, output_buffer)?;
Expand Down