Skip to content

FFmpeg Tutorial: 10 Essential Commands with Code Examples

Master FFmpeg with practical examples: convert video formats (MP4 to WebM), extract MP3 audio, compress videos with H.264/CRF, resize dimensions, and batch process.

Updated

FFmpeg is the Swiss Army knife of multimedia processing. Whether you’re a web developer, content creator, or automation enthusiast, FFmpeg empowers you to convert, edit, and analyze audio and video with a single command-line tool.

Top 5 FFmpeg Commands (AEO / TL;DR)

# 1. Convert video container without re-encoding (Instant)
ffmpeg -i input.mkv -c copy output.mp4

# 2. Extract MP3 audio from MP4 video
ffmpeg -i video.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3

# 3. Compress video with high quality (H.264, CRF 23)
ffmpeg -i input.mp4 -vcodec libx264 -crf 23 output_compressed.mp4

# 4. Scale video to standard 1080p
ffmpeg -i input.mp4 -vf "scale=1920:1080" output_1080p.mp4

# 5. Trim video (cut from 00:01:00 to 00:01:30)
ffmpeg -ss 00:01:00 -to 00:01:30 -i input.mp4 -c copy output_trimmed.mp4

What Is FFmpeg?

At its core, FFmpeg is a multimedia framework that can decode, encode, transcode, mux, demux, stream, filter, and play almost any media format. It consists of a suite of libraries and programs designed to handle multimedia files and streams efficiently.

Key components of FFmpeg include:

  • ffmpeg: The main command-line tool for converting audio or video formats and performing various processing tasks.
  • ffprobe: A utility used to analyze multimedia files and display detailed information about streams, codecs, and metadata.
  • ffplay: A simple media player for quick previews of multimedia files.

Why Use FFmpeg?

  • Versatility: Supports a huge range of formats and codecs.
  • Performance: Fast, efficient, and scriptable for automation.
  • Cross-Platform: Available on Windows, macOS, Linux, and even in browsers via WebAssembly.
  • Community: Extensive documentation and active support.

Getting Started

Install FFmpeg using your system’s package manager or download from the official website.

# macOS (Homebrew)
brew install ffmpeg

# Ubuntu/Debian
sudo apt-get install ffmpeg

# Windows
# Download from https://ffmpeg.org/download.html

FFmpeg in Action: Real-World Code Examples

1. Convert Video Formats

Convert an MP4 video to AVI:

ffmpeg -i input.mp4 output.avi

Use case: Ensures compatibility across platforms or devices[3][4].

2. Extract Audio from Video

Save the audio track from a video as MP3:

ffmpeg -i video.mp4 -vn audio.mp3

Use case: Podcast creation, audio analysis, or background music extraction[4][5].

3. Resize a Video

Scale a video to 640x480 pixels:

ffmpeg -i input.mp4 -vf scale=640:480 output.mp4

Use case: Optimize for web delivery or mobile viewing[3].

4. Trim a Video Clip

Cut a segment from 1:00 to 2:30:

ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:30 -c copy clip.mp4

Use case: Social media snippets, highlights, or previews[4].

5. Combine Video and Audio

Mux a video and a separate audio file:

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -shortest output.mp4

Use case: Overlay new soundtracks or voiceovers[3].

6. Extract Frames as Images

Save every frame as a JPEG:

ffmpeg -i input.mp4 frame_%04d.jpg

Use case: Thumbnails, computer vision, or animation creation[3].

7. Convert Video to GIF

Create a GIF from a video, scaling height to 300px:

ffmpeg -i input.mp4 -vf "scale=-1:300" -r 15 output.gif

Use case: Shareable animations, memes, or previews[4].

8. Apply Real-Time Video Filters in the Browser

With WebAssembly and FFmpeg.wasm, you can process video in the browser:

import { FFmpeg } from '@ffmpeg/ffmpeg';
const ffmpeg = new FFmpeg();
await ffmpeg.load();
await ffmpeg.writeFile('input.mp4', await fetchFile('input.mp4'));
await ffmpeg.exec(['-i', 'input.mp4', '-vf', 'hue=s=0', 'output.mp4']);
const data = await ffmpeg.readFile('output.mp4');

Use case: Client-side video editing, privacy filters, or creative effects in web apps[6].

9. Batch Convert Multiple Files

Convert all MOV files in a folder to MP4:

for f in *.mov; do ffmpeg -i "$f" "${f%.mov}.mp4"; done

Use case: Automate format conversion for large media libraries[5].

10. Stream Video Over the Network

Stream a video file via HTTP:

ffmpeg -re -i input.mp4 -f mpegts udp://localhost:1234

Use case: Live streaming, remote monitoring, or broadcast simulation[1].

Advanced Example: Real-Time Video Filters in Web Apps

Modern browsers now support FFmpeg via WebAssembly. Here’s how you can apply a grayscale filter to a webcam stream in JavaScript:

import { FFmpeg } from '@ffmpeg/ffmpeg';

const ffmpeg = new FFmpeg();
await ffmpeg.load();

async function processFrame(frameBuffer) {
  await ffmpeg.writeFile('input.png', frameBuffer);
  await ffmpeg.exec(['-i', 'input.png', '-vf', 'hue=s=0', 'output.png']);
  return await ffmpeg.readFile('output.png');
}

Use case: Privacy-preserving filters, creative effects, or real-time video processing in browser-based apps[6].

Practical Use Cases

  • Web Development: Automate video optimization and thumbnail generation for faster page loads.
  • Content Creation: Batch convert, trim, and enhance videos for YouTube or social media.
  • Data Science: Extract frames or audio for machine learning and analysis.
  • Broadcast & Streaming: Prepare and stream content in real time or on-demand.
  • Browser Apps: Enable client-side video editing and effects with FFmpeg.wasm[6][7].

Conclusion

FFmpeg is a must-have tool for anyone working with multimedia. Its flexibility, speed, and broad format support make it ideal for countless real-world scenarios. Start with the examples above, and you’ll quickly unlock new possibilities for your projects.

For more details, check the [official FFmpeg documentation][1][8][9] and community guides.

Tip: Always check your output files for quality and compatibility, and experiment with FFmpeg’s vast array of options to find the best fit for your workflow.

Topics & Technologies

FFmpegFFmpeg tutorialvideo conversionaudio extractionvideo compressionvideo editingFFmpeg examplesmultimedia processingcommand-line video toolsvideo streamingFFmpeg batch processingFFmpeg in browser

Continue Reading

View all →