ffmpegvideo-format-conversionwebmmp4api

How to Convert Video Format with FFmpeg (MP4, WebM, MKV, AVI)

·Javid Jamae·7 min read
How to Convert Video Format with FFmpeg (MP4, WebM, MKV, AVI)

Converting video between formats is one of the most common FFmpeg operations, and one of the most error-prone. Pick the wrong codec for a container and FFmpeg exits with a cryptic error. Pick the right codec but wrong settings and your file size doubles.

This guide covers the exact FFmpeg commands for converting between MP4, WebM, MOV, and AVI. Each conversion includes the CLI command and the equivalent API call using FFmpeg Micro, so you can run it from any app or automation tool without installing FFmpeg.

Quick Answer

To convert MP4 to WebM with FFmpeg:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm

Or via the FFmpeg Micro API (no FFmpeg install needed):

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/input.mp4"}],
    "outputFormat": "webm",
    "options": [
      {"option": "-c:v", "argument": "libvpx-vp9"},
      {"option": "-crf", "argument": "30"},
      {"option": "-b:v", "argument": "0"},
      {"option": "-c:a", "argument": "libopus"}
    ]
  }'

Container vs. Codec: Why Format Conversion Breaks

Most format conversion failures come from one misunderstanding: the container (MP4, WebM, MKV) is just a wrapper. The codec (H.264, VP9, AAC) is the actual encoding inside it.

Every container only accepts certain codecs:

ContainerVideo CodecsAudio Codecs
MP4H.264, H.265/HEVC, AV1AAC, MP3
WebMVP8, VP9, AV1Vorbis, Opus
MOVH.264, H.265, ProResAAC, PCM
AVIMost codecs (legacy)Most codecs (legacy)
MKVVirtually any codecVirtually any codec

If you put H.264 video inside a WebM container, FFmpeg fails with: "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio are supported for WebM." The container is rejecting the codec. We confirmed this by running a real API call without specifying VP9, and got exactly this error.

Convert MP4 to WebM

WebM is the standard for web video when you need better compression than H.264. VP9 gives you smaller files at the same quality, but encoding takes longer.

CLI:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm

The -b:v 0 flag tells VP9 to use pure constant quality mode. Without it, VP9 defaults to constrained quality with a target bitrate that usually wastes space.

API:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/input.mp4"}],
    "outputFormat": "webm",
    "options": [
      {"option": "-c:v", "argument": "libvpx-vp9"},
      {"option": "-crf", "argument": "30"},
      {"option": "-b:v", "argument": "0"},
      {"option": "-c:a", "argument": "libopus"}
    ]
  }'
{
  "id": "67e11131-a21c-4f60-ba9e-c524f74c3357",
  "status": "pending",
  "outputFormat": "webm"
}

We ran this against a 13-second 640x360 MP4. The API completed processing in under 1 second.

Convert MKV or MOV to MP4

MKV files from video editors and MOV files from iPhones often already contain H.264 video and AAC audio. Since MP4 supports both codecs, you can remux without re-encoding:

CLI (remux, near-instant):

ffmpeg -i input.mkv -c copy -movflags +faststart output.mp4

This copies the streams into a new container. No re-encoding means it finishes in seconds regardless of file length. The -movflags +faststart flag moves MP4 metadata to the front of the file so browsers can start playback before the download finishes. Always add this for web delivery.

If the MKV or MOV contains codecs that MP4 doesn't support (like VP9 inside MKV), you need to re-encode:

CLI (re-encode):

ffmpeg -i input.mkv -c:v libx264 -crf 23 -c:a aac output.mp4

API:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/input.mkv"}],
    "outputFormat": "mp4"
  }'

The API selects H.264 + AAC by default when you set outputFormat to mp4. No codec flags needed for the common case.

Convert AVI to MP4

AVI is a legacy format. Most AVI files use older codecs (MPEG-4 Part 2, DivX, Xvid) that need re-encoding for modern playback:

CLI:

ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac output.mp4

API:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/input.avi"}],
    "outputFormat": "mp4"
  }'

Batch Convert Videos via API

The CLI approach breaks down at scale. Converting 50 or 500 files means writing a shell loop and hoping your machine doesn't run out of memory.

The API handles this natively. Each job runs on its own cloud instance:

const API_KEY = process.env.FFMPEG_MICRO_API_KEY;

async function convertVideo(inputUrl: string, outputFormat: string) {
  const res = await fetch('https://api.ffmpeg-micro.com/v1/transcodes', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      inputs: [{ url: inputUrl }],
      outputFormat,
    }),
  });
  return res.json();
}

const videos = [
  'https://storage.example.com/video1.mov',
  'https://storage.example.com/video2.avi',
  'https://storage.example.com/video3.mkv',
];

const jobs = await Promise.all(
  videos.map(url => convertVideo(url, 'mp4'))
);

console.log(`Started ${jobs.length} conversions`);

No concurrency limits to manage, no server to scale up.

Common Pitfalls

WebM + H.264 = instant failure. The WebM container only accepts VP8, VP9, or AV1 video. If you set outputFormat: "webm" without specifying a VP9 codec, the job fails. Always pass -c:v libvpx-vp9 (or libvpx for VP8) when targeting WebM.

**Missing -b:v 0 for VP9 CRF mode.** Without this flag, VP9 uses constrained quality with a default target bitrate. The result is usually a larger file than necessary. Pair -crf with -b:v 0 for pure quality-based encoding.

**Forgetting -movflags +faststart for web MP4s.** Without this, the browser must download the entire file before playback starts. Add it to every MP4 destined for web delivery or streaming.

Re-encoding when remuxing would do. If the source codec is compatible with the target container (H.264 in MKV going to MP4), use -c copy to remux. It finishes instantly and is completely lossless.

Assuming "format" means "codec." "Convert to MP4" is ambiguous. MP4 can contain H.264, H.265, or AV1. If you need a specific codec for device compatibility, spell it out with -c:v libx264.

FAQ

Can I convert video format without installing FFmpeg?

Yes. FFmpeg Micro is a cloud API that runs FFmpeg on managed infrastructure. Send an HTTP request with your video URL and target format. No FFmpeg binary, no server to maintain. Get a free API key at ffmpeg-micro.com.

What's the difference between remuxing and transcoding?

Remuxing (-c copy) changes the container without touching the video or audio data. It's near-instant and lossless. Transcoding (-c:v libx264) decodes and re-encodes the streams, which takes longer and can reduce quality slightly, but lets you change the actual codec.

Which format should I use for web video?

MP4 with H.264 has the widest browser support. WebM with VP9 gives better compression but slightly less compatibility on older devices. For maximum reach, go with MP4. For bandwidth savings on modern browsers, WebM.

How long does format conversion take via API?

It depends on video length and whether re-encoding is needed. A 13-second 640x360 MP4 to WebM conversion completed in under 1 second on FFmpeg Micro. Longer videos scale roughly linearly with duration.

Can I convert MKV or AVI files to MP4 via API?

Yes. Pass any publicly accessible video URL (or upload via the presigned upload flow) and set outputFormat: "mp4". The API handles codec detection and re-encoding automatically.

*Last verified: 2026-05-18 against FFmpeg Micro API v1 and FFmpeg 7.0.2*

About Javid Jamae

Founder & CEO at FFmpeg Micro

Javid is a software engineer, author, and entrepreneur with over 25 years of professional software development experience across enterprise, startup, and consulting environments. He founded FFmpeg Micro to make video processing accessible to developers through a simple, automation-first REST API.

Software EngineeringVideo ProcessingFFmpegCloud ArchitectureAPI DesignAutomation

Ready to process videos at scale?

Start using FFmpeg Micro's simple API today. No infrastructure required.

Get Started Free