折騰筆記[1]-打包ffmpeg-cli到單個wasm二進位制

qsBye發表於2024-09-28

摘要

使用c2w打包ffmpeg-cli到單個wasm二進位制,實現ffmpeg可執行檔案平臺無關執行(Build Once,Run Anywhere)。

Abstract

Using c2w to package the ffmpeg-cli into a single wasm binary, achieving platform-independent execution of the ffmpeg executable (Build Once, Run Anywhere).

原理簡介

c2w簡介

[https://github.com/ktock/container2wasm]
[https://ktock.github.io/container2wasm-demo/]
Container to WASM converter
container2wasm is a container-to-wasm image converter that enables to run the container on WASM.

Converts a container to WASM with emulation by Bochs (for x86_64 containers) and TinyEMU (for riscv64 containers).
Runs on WASI runtimes (e.g. wasmtime, wamr, wasmer, wasmedge, wazero)
Runs on browser
x86_64 or riscv64 containers are recommended. Other platforms (e.g. arm64) also work (but slow).
This is an experimental software.

Demo page of containers on browser (debian,python,node,vim): https://ktock.github.io/container2wasm-demo/

wasmtime簡介

[https://wasmtime.dev/]
A fast and secure runtime for WebAssembly
A Bytecode Alliance project

Fast. Wasmtime is built on the optimizing Cranelift code generator to quickly generate high-quality machine code either at runtime or ahead-of-time. Wasmtime is optimized for efficient instantiation, low-overhead calls between the embedder and wasm, and scalability of concurrent instances.

Secure. Wasmtime's development is strongly focused on correctness and security. Building on top of Rust's runtime safety guarantees, each Wasmtime feature goes through careful review and consideration via an RFC process. Once features are designed and implemented, they undergo 24/7 fuzzing donated by Google's OSS Fuzz. As features stabilize they become part of a release, and when things go wrong we have a well-defined security policy in place to quickly mitigate and patch any issues. We follow best practices for defense-in-depth and integrate protections and mitigations for issues like Spectre. Finally, we're working to push the state-of-the-art by collaborating with academic researchers to formally verify critical parts of Wasmtime and Cranelift.

Configurable. Wasmtime uses sensible defaults, but can also be configured to provide more fine-grained control over things like CPU and memory consumption. Whether you want to run Wasmtime in a tiny environment or on massive servers with many concurrent instances, we've got you covered.

WASI. Wasmtime supports a rich set of APIs for interacting with the host environment through the WASI standard.

Standards Compliant. Wasmtime passes the official WebAssembly test suite, implements the official C API of wasm, and implements future proposals to WebAssembly as well. Wasmtime developers are intimately engaged with the WebAssembly standards process all along the way too.

  • wasmtime支援的宿主平臺:
    v25.0.1: Release Wasmtime 25.0.1 (#9306) Latest
File Name Size Last Modified
wasi_snapshot_preview1.command.wasm 52.4 KB 12 hours ago
wasi_snapshot_preview1.proxy.wasm 17 KB 12 hours ago
wasi_snapshot_preview1.reactor.wasm 52.3 KB 12 hours ago
wasmtime-dev-aarch64-android-c-api.tar.xz 13.5 MB 12 hours ago
wasmtime-dev-aarch64-android.tar.xz 7.39 MB 12 hours ago
wasmtime-dev-aarch64-linux-c-api.tar.xz 14.1 MB 12 hours ago
wasmtime-dev-aarch64-linux.tar.xz 7.36 MB 12 hours ago
wasmtime-dev-aarch64-macos-c-api.tar.xz 12.3 MB 12 hours ago
wasmtime-dev-aarch64-macos.tar.xz 6.54 MB 12 hours ago
wasmtime-dev-aarch64-windows-c-api.zip 19.9 MB 12 hours ago
wasmtime-dev-aarch64-windows.zip 8.38 MB 12 hours ago
wasmtime-dev-riscv64gc-linux-c-api.tar.xz 16.3 MB 12 hours ago
wasmtime-dev-riscv64gc-linux.tar.xz 7.88 MB 12 hours ago
wasmtime-dev-s390x-linux-c-api.tar.xz 15.4 MB 12 hours ago
wasmtime-dev-s390x-linux.tar.xz 7.83 MB 12 hours ago
wasmtime-dev-src.tar.gz 142 MB 12 hours ago
wasmtime-dev-x86_64-android-c-api.tar.xz 14 MB 12 hours ago
wasmtime-dev-x86_64-android.tar.xz 8.03 MB 12 hours ago
wasmtime-dev-x86_64-linux-c-api.tar.xz 14.7 MB 12 hours ago
wasmtime-dev-x86_64-linux.tar.xz 8.08 MB 12 hours ago
wasmtime-dev-x86_64-macos-c-api.tar.xz 13.3 MB 12 hours ago
wasmtime-dev-x86_64-macos.tar.xz 7.49 MB 12 hours ago
wasmtime-dev-x86_64-mingw-c-api.zip 23.5 MB 12 hours ago
wasmtime-dev-x86_64-mingw.zip 14.9 MB 12 hours ago
wasmtime-dev-x86_64-musl-c-api.tar.xz 14.7 MB 12 hours ago
wasmtime-dev-x86_64-musl.tar.xz 8.11 MB 12 hours ago
wasmtime-dev-x86_64-windows-c-api.zip 21 MB 12 hours ago
wasmtime-dev-x86_64-windows.msi 8.88 MB 12 hours ago
wasmtime-dev-x86_64-windows.zip 9.17 MB 12 hours ago
wasmtime-platform.h 9.14 KB 12 hours ago
Source code (zip) - 13 hours ago
Source code (tar.gz) - 13 hours ago

實現

使用c2w打包ffmpeg-cli進入wasm

[https://github.com/qsbye/ffmpeg.wasm]
[https://gitee.com/qsbye/ffmpeg.wasm] release頁面有打包的wasm二進位制檔案

# 打包/Package
docker pull linuxserver/ffmpeg:version-7.0.2-cli
c2w linuxserver/ffmpeg:version-7.0.2-cli /home/qsbye/ffmpeg-7.0.2-cli.wasm
# 測試/Test
# 安裝wasmtime執行時,當然也可以使用wasmer等其他的執行時(未測試)
curl https://wasmtime.dev/install.sh -sSf | bash
wasmtime ffmpeg-7.0.2-cli.wasm
# 轉換資料夾下的sheep.mp3到sheep.wav檔案
wasmtime run --dir $PWD::/root ffmpeg-7.0.2-cli.wasm -- -i /root/sheep.mp3 /root/sheep.wav

輸出:

ffmpeg version 7.0.2 Copyright (c) 2000-2024 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu4)
  configuration: --disable-debug --disable-doc --disable-ffplay --enable-alsa --enable-cuda-llvm --enable-cuvid --enable-ffprobe --enable-gpl --enable-libaom --enable-libass --enable-libdav1d --enable-libfdk_aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libplacebo --enable-librav1e --enable-librist --enable-libshaderc --enable-libsrt --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpl --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-nonfree --enable-nvdec --enable-nvenc --enable-opencl --enable-openssl --enable-stripping --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan
  libavutil      59.  8.100 / 59.  8.100
  libavcodec     61.  3.100 / 61.  3.100
  libavformat    61.  1.100 / 61.  1.100
  libavdevice    61.  1.100 / 61.  1.100
  libavfilter    10.  1.100 / 10.  1.100
  libswscale      8.  1.100 /  8.  1.100
  libswresample   5.  1.100 /  5.  1.100
  libpostproc    58.  1.100 / 58.  1.100
Universal media converter
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'
  • 優點:執行簡單,平臺無關,不會受到環境變數和動態連結庫版本影響,可以在docker映象缺失的riscv64架構計算機執行。

  • 缺點:二進位制檔案大,執行比較緩慢

  • 吐槽:
    😫本地安裝的ffmpeg經常出現如下動態連結庫找不到狀況😄

dyld[44495]: Library not loaded: /opt/homebrew/opt/gettext/lib/libintl.8.dylib
  Referenced from: <DE8EDE65-C534-3BE7-83AC-04F5198C3291> /opt/homebrew/Homebrew/Cellar/fontconfig/2.15.0/lib/libfontconfig.1.dylib
  Reason: tried: '/Users/workspace/.x-cmd.root/local/data/pkg/sphere/X/l/j/h/lib/libintl.8.dylib' (no such file), '/Users/workspace/.wasmedge/lib/libintl.8.dylib' (no such file), '/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/usr/local/lib/libintl.8.dylib' (no such file), '/usr/lib/libintl.8.dylib' (no such file, not in dyld cache)Library not loaded: /opt/homebrew/opt/gettext/lib/libintl.8.dylib
  Referenced from: <B65B4C16-ACEF-323C-A75F-5AAAE4FF00FC> /opt/homebrew/Homebrew/Cellar/gnutls/3.7.8_1/lib/libgnutls.30.dylib
  Reason: tried: '/Users/workspace/.x-cmd.root/local/data/pkg/sphere/X/l/j/h/lib/libintl.8.dylib' (no such file), '/Users/workspace/.wasmedge/lib/libintl.8.dylib' (no such file), '/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/opt/homebrew/opt/gettext/lib/libintl.8.dylib' (no such file), '/usr/local/lib/libintl.8.dylib' (no such file), '/usr/lib/libintl.8.dylib' (no such file, not in dyld cache)
zsh: abort      ffmpeg

聯絡方式

如果對本文有疑問或者提出建議可評論區留言或者傳送郵件到2557877116@qq.com.
分割線

相關文章