Converting WebM to MP4 Using FFmpeg

Converting WebM to MP4 is a common task for developers, especially when building video pipelines, handling browser-recorded media, or ensuring universal playback compatibility across devices. This guide provides practical, production-ready examples using FFmpeg.

Why Convert WebM to MP4?

WebM is optimized for web streaming but has limited compatibility, especially with platforms like Safari and iOS. MP4 (particularly with H.264 video and AAC audio) ensures universal playback across browsers, devices, and editing tools, making it the preferred choice for developers and content creators.

Quick Comparison: WebM vs MP4

FormatContainerVideo CodecsAudio CodecsCompatibility
WebMMatroska-basedVP8, VP9, AV1Vorbis, OpusChrome, Firefox, Opera; limited Safari/iOS support
MP4MPEG-4 Part 14H.264/AVC, H.265/HEVCAAC, MP3Nearly universal across devices, browsers, and tools

Bottom line: MP4 ensures broader compatibility and reliable playback due to its use of widely-supported codecs like H.264 and AAC. In contrast, WebM's codecs—VP8, VP9, and AV1—are optimized for streaming but have limited support, particularly in Safari and on iOS devices.

How to Convert WebM to MP4 with FFmpeg

1. Install FFmpeg

Before converting video formats, you'll need FFmpeg installed.

macOS:

brew install ffmpeg

Ubuntu/Debian:

sudo apt update && sudo apt install ffmpeg

Windows: Download from FFmpeg's official site, or use Chocolatey:

choco install ffmpeg

Check out our guide on How to Use FFmpeg.

2. Convert a Single WebM File to MP4

Simple conversion command:

ffmpeg -i input.webm output.mp4

This converts video from WebM codecs (VP8/VP9) and audio codecs (Opus/Vorbis) to MP4-compatible H.264 video and AAC audio.

For explicit codec control:

ffmpeg -i input.webm -c:v libx264 -c:a aac output.mp4

Command breakdown:

  • -i input.webm specifies the input file.
  • -c:v libx264 encodes video in H.264.
  • -c:a aac encodes audio in AAC.

3. Improve Playback with Faststart

Use faststart for streaming or downloads, ensuring quick playback:

ffmpeg -i input.webm -c:v libx264 -c:a aac -movflags faststart output.mp4

The -movflags faststart option optimizes MP4 files for quicker playback by moving the 'moov atom' (metadata required for playback) to the start of the file. Without faststart, playback may only begin after the entire file is downloaded, resulting in delayed streaming or longer waiting times for users.

4. Control File Size and Quality

Control quality using CRF (Constant Rate Factor); lower CRF numbers yield higher quality:

Example:

ffmpeg -i input.webm -c:v libx264 -crf 23 -preset faster -c:a aac -b:a 128k output.mp4
  • -crf 18–28: Adjust quality (18 is higher quality, 28 is lower).
  • -preset: Adjust encoding speed and compression (veryfast, medium, slow).
  • -b:a 128k: Set audio bitrate.

For more, see: Compress Video with FFmpeg

5. Batch Convert WebM to MP4

macOS/Linux:

for f in *.webm; do
ffmpeg -i "$f" -c:v libx264 -c:a aac "${f%.webm}.mp4"
done

Windows PowerShell:

Get-ChildItem *.webm | ForEach-Object {
ffmpeg -i $_.Name -c:v libx264 -c:a aac ($_.Name -replace ".webm$", ".mp4")
}

Batch process multiple files efficiently. For more details: Batch Conversion with FFmpeg

Bonus: Stream Copy (Only if Codecs Match)

Occasionally, a WebM file may already contain MP4-compatible codecs (H.264/AAC). In these rare cases, you can avoid transcoding:

ffmpeg -i input.webm -c copy output.mp4

This is faster and preserves original quality, but it only works if the original codecs match MP4 standards. Check codecs with:

ffmpeg -i input.webm

Final Summary

WebM is ideal for streaming and browser capture but lacks universal compatibility. MP4 ensures seamless playback everywhere.

TaskRecommended Command
Basic conversionffmpeg -i input.webm output.mp4
Set codecs explicitlyffmpeg -i input.webm -c:v libx264 -c:a aac output.mp4
Fast start playback-movflags faststart
Control quality-crf 20 -preset slow
Batch conversionShell script or PowerShell loop

Get started with Shotstack's video editing API in two steps:

  1. Sign up for free to get your API key.
  2. Send an API request to create your video:
    curl --request POST 'https://api.shotstack.io/v1/render' \
    --header 'x-api-key: YOUR_API_KEY' \
    --data-raw '{
    "timeline": {
    "tracks": [
    {
    "clips": [
    {
    "asset": {
    "type": "video",
    "src": "https://shotstack-assets.s3.amazonaws.com/footage/beach-overhead.mp4"
    },
    "start": 0,
    "length": "auto"
    }
    ]
    }
    ]
    },
    "output": {
    "format": "mp4",
    "size": {
    "width": 1280,
    "height": 720
    }
    }
    }'
Peace Aisosa

BY PEACE AISOSA
4th April 2025

Studio Real Estate
Experience Shotstack for yourself.
SIGN UP FOR FREE

You might also like

Batch convert videos using FFmpeg

Batch convert videos using FFmpeg

Maab Saleem