Ffmpeg

Automate and integrate FFmpeg for powerful audio and video processing and conversion

FFmpeg is a command line multimedia framework for processing video, audio, and image files through encoding, decoding, transcoding, streaming, and filtering operations. It covers format conversion, codec selection, bitrate control, filter graphs, and batch processing workflows that enable automated media pipeline construction.

What Is This?

Overview

FFmpeg provides structured approaches to multimedia processing through command line operations. It handles converting between hundreds of audio and video formats with codec control, applying complex filter graphs for video scaling, cropping, overlays, and color correction, extracting audio tracks from video files or muxing separate streams together, generating thumbnails and preview clips from longer video content, streaming media over network protocols including RTMP, HLS, and DASH, and batch processing large media libraries with scripted automation.

Who Should Use This

This skill serves developers building media processing pipelines, content teams automating video post-production workflows, DevOps engineers integrating transcoding into CI/CD systems, and platform teams constructing media delivery infrastructure.

Why Use It?

Problems It Solves

Manual media conversion through GUI tools does not scale beyond a handful of files. Inconsistent encoding settings produce unpredictable output quality and file sizes across a library. Without automation, generating multiple output formats for different devices requires repetitive effort. Integrating media processing into application backends demands a programmable interface rather than point and click tools.

Core Highlights

Format flexibility supports conversion between virtually any audio or video format through a single tool. Filter graphs chain multiple video and audio transformations in a single pass without intermediate files. Hardware acceleration leverages GPU encoding through NVENC, VAAPI, or VideoToolbox for faster processing. Scriptable automation enables batch operations through shell scripts and process execution from any language.

How to Use It?

Basic Usage

ffmpeg -i input.mov -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4

ffmpeg -i video.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3

ffmpeg -i input.mp4 -vf "scale=-2:720" -c:a copy output_720p.mp4

ffmpeg -i input.mp4 -ss 00:01:30 -vframes 1 thumbnail.jpg

ffmpeg -f concat -safe 0 -i filelist.txt -c copy merged.mp4

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

Real-World Examples

import subprocess
import os
from pathlib import Path

class MediaProcessor:
    def __init__(self, output_dir="processed"):
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(exist_ok=True)

    def transcode_for_web(self, input_path, resolution=720):
        output = self.output_dir / f"{Path(input_path).stem}_web.mp4"
        cmd = [
            "ffmpeg", "-i", str(input_path),
            "-c:v", "libx264", "-preset", "medium",
            "-crf", "23",
            "-vf", f"scale=-2:{resolution}",
            "-c:a", "aac", "-b:a", "128k",
            "-movflags", "+faststart",
            str(output)
        ]
        subprocess.run(cmd, check=True, capture_output=True)
        return output

    def generate_hls(self, input_path, segment_duration=6):
        hls_dir = self.output_dir / "hls"
        hls_dir.mkdir(exist_ok=True)
        playlist = hls_dir / "playlist.m3u8"
        cmd = [
            "ffmpeg", "-i", str(input_path),
            "-c:v", "libx264", "-c:a", "aac",
            "-hls_time", str(segment_duration),
            "-hls_list_size", "0",
            "-hls_segment_filename",
            str(hls_dir / "segment_%03d.ts"),
            str(playlist)
        ]
        subprocess.run(cmd, check=True, capture_output=True)
        return playlist

    def extract_frames(self, input_path, fps=1):
        frames_dir = self.output_dir / "frames"
        frames_dir.mkdir(exist_ok=True)
        cmd = [
            "ffmpeg", "-i", str(input_path),
            "-vf", f"fps={fps}",
            str(frames_dir / "frame_%04d.jpg")
        ]
        subprocess.run(cmd, check=True, capture_output=True)
        return list(frames_dir.glob("*.jpg"))

processor = MediaProcessor()
processor.transcode_for_web("raw_footage.mov")
processor.generate_hls("raw_footage.mov")

Advanced Tips

Use two pass encoding for target bitrate accuracy when file size constraints matter more than encoding speed. Apply the faststart flag with movflags when producing MP4 files for web delivery so playback begins before the full download completes. Chain filter graphs using semicolons to process multiple streams in parallel within a single FFmpeg invocation.

When to Use It?

Use Cases

Use FFmpeg when building automated transcoding pipelines for user uploaded video, when generating adaptive streaming packages in HLS or DASH format, when extracting frames or thumbnails from video libraries for search indexes, or when normalizing audio levels across a podcast archive.

Related Topics

GStreamer pipeline construction, libav library integration, hardware accelerated encoding with NVENC, adaptive bitrate streaming with HLS, and container format specifications complement FFmpeg workflows.

Important Notes

Requirements

FFmpeg binary installed with codec libraries for target formats. Sufficient CPU or GPU resources for encoding workloads. Storage capacity for intermediate and output files during batch processing.

Usage Recommendations

Do: use CRF mode for quality driven encoding where file size is secondary to visual consistency. Validate input files with ffprobe before processing to catch corrupt or unsupported formats early. Apply the copy codec flag when trimming or remuxing to avoid unnecessary re-encoding.

Don't: re-encode media multiple times through lossy codecs as each generation degrades quality. Assume default encoding settings are optimal for all content types without testing output quality. Ignore return codes from FFmpeg subprocess calls in automation scripts.

Limitations

Command line syntax grows complex with advanced filter graphs, making errors difficult to debug. Encoding speed depends on codec, preset, and hardware availability. Some proprietary codecs require separate license agreements or third party compilation.