折腾笔记[1]-打包ffmpeg-cli到单个wasm二进制

摘要

使用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.
分割线

posted @ 2024-09-28 15:37  qsBye  阅读(11)  评论(0编辑  收藏  举报