Overview
This service wraps@ffmpeg/ffmpeg to:
- Mux video and audio streams into MP4 containers
- Embed WebVTT subtitles into MKV containers
- Handle stream synchronization and codec configuration
- Perform automatic cleanup of temporary files
Types
MuxRequest
Configuration object for the muxing operation.FFmpeg instance to use for muxing operations
Name of the output file (e.g., “output.mp4” or “output.mkv”)
Video stream data as a Blob. If omitted, only audio will be muxed
Audio stream data as a Blob. If omitted, only video will be muxed
WebVTT subtitle content as a string. When provided, output will be MKV format
ISO 639 language code for subtitle track (e.g., “en”, “es”). Defaults to “und” (undetermined)
MuxResult
Result of the muxing operation.The muxed media file as a Blob
MIME type of the output file. Either “video/mp4” or “video/x-matroska” (MKV)
Methods
muxStreams
Muxes video, audio, and subtitle streams into a single container file.Parameters
Mux configuration object
Returns
Promise<MuxResult>
Promise resolving to the muxed file blob and MIME type
Behavior
File handling:- Writes input streams to FFmpeg’s virtual filesystem as
video.ts,audio.ts, andsubtitles.vtt - Reads the output file after muxing completes
- Automatically cleans up temporary files on both success and failure
- Video + Audio: Maps both streams with
-map 0:v:0 -map 1:a:0, copies codecs, applies AAC ADTS to ASC bitstream filter - Video only: Maps video stream with
-map 0:v:0, copies video codec - Audio only: Maps audio stream, re-encodes to AAC at 192kbps with resampling to fix timing
- Subtitles are only supported with MKV containers (
video/x-matroska) - WebVTT format is used for subtitle encoding
- Language metadata is added to the subtitle track
- Throws
"No media to mux"if neither video nor audio blob is provided - Performs best-effort cleanup of temporary files even on failure
Example
The
-shortest flag is always applied to prevent the output from extending beyond the shortest input stream.FFmpeg command examples
Video + audio muxing
Audio-only with resampling
Video + audio + subtitles
Source location
~/workspace/source/src/background/src/services/ffmpeg-muxer.ts:26