Skip to main content

Convert media files using FFmpeg WebAssembly and a local server

Project description

ffmpeg-wasm-bridge

A simple Python server that allows to convert files totally on device using FFmpeg WebAssembly.

Install it from PyPI and use it both from the command line and in your Python projects: pip install ffmpeg-wasm-bridge

How it works

This script creates a local server (usually, on http://localhost:10000, but can be customized). The user will have to open the webpage, and then the script will handle everything else: the files will be loaded in FFmpeg WebAssembly, the command will be executed and the file will be saved on the user's local drive.

Usage

From the command line

From the command line, pass all the FFmpeg arguments (excluding ffmpeg at the start) in the Python server.py file. Connect to the webpage the script will print in the console, and the script will handle everything else.

Example: python3 server.py -i file_0.mp3 -acodec libopus file_0.ogg

In another Python script

You can use this server in your Python scripts/applications using the two exposed classes (FFmpegServer and FFmpegOperation). You can find a small script example here below:

server = FFmpegServer()
await asyncio.create_task(server.start())
operation = FFmpegOperation(server)
await operation.run(commands=["-i", "file_0.mp3" "-acodec", "libopus", "file_0.ogg"])

To learn more about what each class does, and what are the function you should run, see the Classes section below.

Classes

There are three classes that can help you with implementing ffmpeg-wasm-bridge in your application:

FFmpegServer

The server that'll be used for this FFmpeg operation. While you can do whatever you want, I would suggest you to create only a FFmpegServer per application, since multiple servers are unnecessary (but they should still work!)

Parameters

You can specify two optional parameters to the FFmpegServer class:

Parameter name Description Extra notes
host The hostname for the new sever. Defaults to 127.0.0.1
suggested_port the port that should be used for this server. Defaults to 10000.
Note: the script will always use the first available port from this value. So, for example, if port 10000 is being used elsewhere, the script will try using port 10001, and so on.

Functions

Function name Description Parameters
start Starts the FFmpeg server None

FFmpegOperation

This class creates a new FFmpeg WebAssembly instance on the webpage. Each instance has its own file system. While an instance can run only a FFmpeg command at a time, you are free to create multiple FFmpegOperations to run multiple commands.

Parameters

You need to specify only a parameter in the FFmpegOperation class, that is the FFmpegServer this class will depend on.

Functions

Function name Description Parameters
create_ffmpeg Creates a new FFmpeg WebAssembly object in the webpage. This should be called only one time, and will automatically be called when you use any other function of the FFmpegOperation class, so you can ignore it.
run Runs the provided command.
  • commands: a list of strings with the FFmpeg command to execute (without ffmpeg at the start);
  • files: an optional FileBridge list. See below to understand what the FileBridge class does. If nothing is passed, the script will automatically try to understand which files should be loaded in FFmpeg WebAssembly.
delete_files Deletes files that have been manually loaded in FFmpeg WebAssembly, so the ones where you have specified the force_loading_name property in the FileBridge object. All the other files will automatically be deleted after the conversion has been done. This function has only an argument, that is the list of FileBridge objects to delete.
reload_ffmpeg Reloads the FFmpeg object. This could help reducing RAM usage from FFmpeg WebAssembly.

FileBridge

This class is used to specify that a file should be read by the webpage.

Parameters

Parameter name Description Extra notes
path A string with the file path on the user's device -
appears_in_command_str Optional value. If the path appears in a different way in the command (for exampe, it's a relative path from somewhere else), use this field to write how in the command this file appears -
force_loading_name Optional value. If this value is passed, the FFmpegOperation instance will fetch the file and add it in memory with this name, even if it's not mentioned in the command. A possible usage of this command is if you need to merge files (and so you need to provide a file list as a txt value, and then load all the videos/audios in the memory).
Note: the webpage won't automatically delete from FFmpeg WebAssembly these files, so you should manually do it by calling the delete_files function.

Licenses

This project is published under the MIT license.

The project uses the following open source libraries:

Note that the server will download the first time it's run the FFmpeg WebAssembly files from JSDelivr. After that time, you can use the server completely offline.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ffmpeg_wasm_bridge-1.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ffmpeg_wasm_bridge-1.0.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file ffmpeg_wasm_bridge-1.0.0.tar.gz.

File metadata

  • Download URL: ffmpeg_wasm_bridge-1.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ffmpeg_wasm_bridge-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2fb0acbc446da0c2e1f2b794276633b5f13a2e8bde0704570d1eca457b6c0814
MD5 7253663d63e87a86244acb8d52d53628
BLAKE2b-256 c60c2cbb8188a3d9c48cf03fed5509eb273d692ae635e40a861ce4ce2b45fbcc

See more details on using hashes here.

Provenance

The following attestation bundles were made for ffmpeg_wasm_bridge-1.0.0.tar.gz:

Publisher: python-build.yml on dinoosauro/ffmpeg-wasm-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ffmpeg_wasm_bridge-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ffmpeg_wasm_bridge-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8656eab6796242f1dc3c56e903c40dbc23d493d91eab8f1bf1a3f71b9d8a98d5
MD5 134c4077db434fd13445658dbe9af45c
BLAKE2b-256 bc96e2830fa680e6a00cec62ade58f81e5808f7a9679ed94b6ef45e2fc986600

See more details on using hashes here.

Provenance

The following attestation bundles were made for ffmpeg_wasm_bridge-1.0.0-py3-none-any.whl:

Publisher: python-build.yml on dinoosauro/ffmpeg-wasm-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page