Skip to content

Detailed Explanation of FFmpeg Audio and Video Formats: Savoring Media Like a Gourmet

Tip: If FFmpeg is already installed on your computer, you can use it directly in the command line. If not installed, you can download pre-compiled FFmpeg toolkits. For example, in the Windows pre-packaged FFmpeg directory of pyVideoTrans, the ffmpeg.exe file is included. You can navigate to that folder, type cmd in the address bar, and press Enter to execute ffmpeg commands. For instance, type ffmpeg -h to view FFmpeg help information.

Additionally, for convenience, you can add the FFmpeg installation directory to your system environment variables. The specific method is: Right-click "This PC," select "Properties," then click "Advanced system settings." In the "System Properties" window, click the "Environment Variables" button. In the "System variables" section, find the variable named "Path," select it, and click "Edit." In the pop-up window, click "New," then browse to the FFmpeg installation directory and add it. This way, you can open a CMD or PowerShell window anywhere and directly execute ffmpeg commands, including the examples below.

1. Video Formats: The "Universal Language" and "Local Dialects" of the Video World

Video formats are like different languages; they all convey video information, but their encoding and compression methods vary. Choosing the right video format is like choosing the right language for communication, allowing you to better express your ideas.

  1. MP4 (.mp4): The "Mandarin" of the Video World, King of Compatibility

    • Characteristics: MP4 is the most widely used and compatible video format, supported by almost all devices and platforms. Like Mandarin, it works everywhere. Thus, MP4 is the preferred format for video sharing.

    • Encoding: Common video encodings for MP4 are H.264 (AVC) and H.265 (HEVC). H.264 is time-tested with excellent compatibility, while H.265 is a more advanced encoding that compresses file sizes smaller while maintaining the same quality.

    • FFmpeg Usage:

      • Convert other formats to H.264-encoded MP4:

        bash
        ffmpeg -i input.avi -c:v libx264 output.mp4
      • Convert other formats to H.265-encoded MP4:

        bash
        ffmpeg -i input.avi -c:v libx265 output.mp4
        • -c:v: Specifies the video encoder. libx264 is the H.264 encoder, libx265 is the H.265 encoder. FFmpeg supports various encoders; choose as needed.
        • -preset slow: Controls encoding speed and quality. Slower encoding usually yields better quality. slow balances quality and speed. Other options range from ultrafast (fastest, lower quality) to veryslow (slowest, best quality). Typically, medium and slow are common choices.
        • -crf 23: Controls video quality, usually ranging from 18-28. Lower values mean better quality but larger file sizes. This is crucial for libx264 and libx265 to balance quality and size.
    • Use Cases: Online videos, mobile devices, general storage. MP4 is suitable for almost all video scenarios, whether uploading to video platforms or watching on phones.

  2. AVI (.avi): The Classic "Dialect," Historic but Somewhat Outdated

    • Characteristics: AVI is a historic video format with relatively low compression efficiency, often resulting in larger file sizes. Like photos from old cameras, classic but possibly less sharp than modern ones.

    • Encoding: AVI can contain various video encodings like DivX, Xvid. It acts as a container for different codecs.

    • FFmpeg Usage:

      • Convert to other formats: Due to low compression, it's often converted to modern formats like MP4.

        bash
        ffmpeg -i input.avi -c:v libx264 output.mp4
      • Repair AVI files: Use FFmpeg to attempt repairs if AVI files are corrupted.

        bash
        ffmpeg -i input.avi -c copy -copyts output.avi
        • -c copy: Copies video and audio streams directly without re-encoding, very fast. Useful if only the container is damaged but the encoding is intact.
  3. MKV (.mkv): The Powerful "Universal Container," All-Encompassing

    • Characteristics: MKV is a highly flexible format that can hold multiple video, audio, and subtitle tracks. Like a powerful "shipping container," it bundles various elements, earning the name "universal container."

    • Encoding: MKV supports various video encodings like H.264, H.265, and audio encodings like AAC, MP3. It's highly inclusive, supporting almost all common codecs.

    • FFmpeg Usage:

      • Extract video or audio from MKV files:

        bash
        ffmpeg -i input.mkv -c:v copy -an video.mp4   # Extract video
        ffmpeg -i input.mkv -c:a copy -vn audio.aac   # Extract audio
        • -an: Removes audio.
        • -vn: Removes video.
      • Merge video and audio into an MKV file:

        bash
        ffmpeg -i video.mp4 -i audio.aac -c copy output.mkv
  4. MOV (.mov): Apple's "Official Language," High-Quality Choice

    • Characteristics: MOV is a video format developed by Apple, commonly used in QuickTime player and macOS systems. It often offers high video quality, akin to Apple's reputation for refinement.

    • Encoding: Common video encoding for MOV is H.264.

    • FFmpeg Usage:

      • Convert to and from other formats: Similar to MP4, MOV can be easily converted.

        bash
        ffmpeg -i input.mov -c:v libx264 output.mp4
  5. WebM (.webm): The Internet's "Universal Language," Open Source and Free

    • Characteristics: WebM is an open-source, free video format designed for web videos, ideal for HTML5 playback without plugins.

    • Encoding: Common WebM video encodings are VP8 and VP9, with VP9 offering higher compression and being recommended.

    • FFmpeg Usage:

      • Convert other formats to VP9-encoded WebM:

        bash
        ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 output.webm
        • -b:v 0: Uses variable bitrate (VBR) encoding based on quality, adjusting bitrate according to content complexity to maintain quality while minimizing size. -crf also applies here.

2. Audio Formats: Let Your Ears Enjoy "Heavenly Music," Choose the Best Sound

Audio formats determine sound quality, file size, and compatibility. Like choosing different audio equipment, different formats offer varied listening experiences.

  1. MP3 (.mp3): The "Mandarin" of Audio, Playable Everywhere

    • Characteristics: MP3 is a highly compressed, small-sized, and widely compatible audio format supported by almost all devices. Like audio's "Mandarin," it plays anywhere, but it loses some quality.

    • FFmpeg Usage:

      • Convert other formats to MP3:

        bash
        ffmpeg -i input.wav -c:a libmp3lame -q:a 4 output.mp3
        • -c:a: Specifies the audio encoder. libmp3lame is a common MP3 encoder.
        • -q:a: Specifies audio quality, ranging from 0-9. Lower values mean higher quality but larger files, controlling MP3 compression.
  2. AAC (.aac): The Upgraded MP3, Better Quality, Smaller Size

    • Characteristics: AAC offers better quality and higher compression than MP3. It's commonly used as the audio codec for MP4 videos, like high-definition photos with richer details.

    • FFmpeg Usage:

      • Convert other formats to AAC:

        bash
        ffmpeg -i input.wav -c:a aac -b:a 128k output.aac
        • -b:a: Specifies audio bitrate. 128kbps is a common choice; higher bitrates mean better quality but larger files.
  3. WAV (.wav): Preserving "Original Flavor," Quality First

    • Characteristics: WAV is a lossless format that retains all audio information, offering the best quality. Like unprocessed raw photos, it keeps all details but has the largest file size.

    • FFmpeg Usage:

      • Convert to and from other formats: WAV is often used as high-quality source audio for conversions.

        bash
        ffmpeg -i input.mp3 output.wav
  4. FLAC (.flac): The Master of Lossless Compression, Balancing Size and Quality

    • Characteristics: FLAC is a lossless compression format that maintains quality while reducing file size, making it a favorite for audiophiles. Like losslessly compressed photos, it preserves details and shrinks size.

    • FFmpeg Usage:

      • Convert other formats to FLAC:

        bash
        ffmpeg -i input.wav -c:a flac output.flac
  5. OGG (.ogg): The Open-Source "Rising Star," Ideal for Web Streaming

    • Characteristics: OGG is an open-source, free audio format with good quality, suitable for web streaming without licensing. Like open-source software, it's free and powerful, commonly using Vorbis encoding.

    • FFmpeg Usage:

      • Convert other formats to OGG with Vorbis encoding:

        bash
        ffmpeg -i input.wav -c:a libvorbis -q:a 5 output.ogg

3. Common FFmpeg Parameters: Simplifying Complexity, Mastering Core Media Processing

FFmpeg's power lies in its flexibility, controlled by various parameters. Mastering these lets you cook up delicious media dishes like a skilled chef.

  • -i input.xxx: Specifies the input file path.
  • output.xxx: Specifies the output file path.
  • -c:v: Specifies the video encoder, e.g., libx264, libx265, libvpx-vp9. Controls compression and compatibility.
  • -c:a: Specifies the audio encoder, e.g., libmp3lame, aac, libvorbis. Controls audio quality and size.
  • -b:v: Specifies video bitrate, e.g., 2000k, 5M. Higher bitrates mean better quality and larger files.
  • -b:a: Specifies audio bitrate, e.g., 128k, 192k. Higher bitrates mean better quality and larger files.
  • -r: Specifies frame rate (fps). Controls video smoothness; e.g., 24fps for movies, 30fps for TV.
  • -s: Specifies resolution, e.g., 1280x720 (720p), 640x480 (480p). Higher resolution means clearer video.
  • -ss: Specifies start time, e.g., 00:00:10 for 10 seconds in. Used for clipping.
  • -t: Specifies duration, e.g., 00:00:05 for 5 seconds. Used for clipping.
  • -vn: Disables video; processes audio only.
  • -an: Disables audio; processes video only.
  • -filter:v: Adds video filters, e.g., scale=640:480 to change resolution. FFmpeg offers rich filters for various effects.
  • -filter:a: Adds audio filters for effects like volume adjustment or noise reduction.
  • -threads: Specifies thread count to speed up encoding by utilizing CPU performance.

4. -c copy: The Magic of Stream Copying, Experience "Instant Transfer" Speed

  • -c copy: This parameter copies the original video or audio streams directly without re-encoding. Like copying files, it's very fast and preserves quality, but requires specific conditions.

When can you use -c copy? Like a puzzle, pieces must match in shape and size to fit.

-c copy relies on "copying," so it only works when input and output formats are compatible and you don't need to change the encoding. Specifically:

  1. Container format change, internal encoding unchanged: Like changing the box but keeping the contents.

    • E.g., Extract video stream from input.mp4 to output.mkv if the video is already H.264 and you just want it in an MKV container.

    • Or convert MP4 to TS, just changing the container.

      bash
      ffmpeg -i input.mp4 -c copy output.ts
  2. Extract video or audio streams: Like cutting a piece from a cake, just taking a part without altering it.

    • Extract audio from a video file without changing the encoding (e.g., both AAC).

    • E.g., Extract H.264 video from input.mkv to output.mp4, and output.mp4 supports H.264 well.

      bash
      ffmpeg -i input.mkv -c:v copy -an output.mp4   # Extract video
      ffmpeg -i input.mkv -c:a copy -vn output.aac   # Extract audio
  3. Repair files: Like fixing a damaged box; the contents are fine, just need repackaging (if damage is container-level).

    bash
    ffmpeg -i input.avi -c copy -copyts output.avi

    -copyts copies timestamps, helping fix timeline issues. Timestamps are like a video's "diary" of frame times.

When can't you use -c copy? Like trying to fit a square peg in a round hole—it won't work.

  1. Need to change encoding: Like translating languages, conversion is necessary.

    • E.g., Convert H.264 to H.265, or MP3 to AAC.
  2. Change resolution, frame rate, etc.: Like resizing a photo, reprocessing is required.

    • Any modification to video or audio content requires encoders and parameters, not -c copy.
  3. Input and output formats are incompatible: Like connecting different plug types, a converter is needed.

    • Rare, but some formats may not allow direct stream copying.

Common -c copy combinations:

  • Video:

    • MP4 <-> MOV (if both H.264) – Like using the same language.
    • MKV <-> MP4/MOV/AVI (extract or encapsulate, encoding unchanged) – Like moving items in and out of a container.
    • TS <-> MP4/MKV (encoding unchanged, common for live streams).
  • Audio:

    • AAC <-> MP4/MOV/MKV (extract or encapsulate)
    • MP3 <-> MP4/MOV/MKV (extract or encapsulate)
    • WAV -> WAV (repair)

Examples:

  • Correct use of -c copy:

    bash
    # Extract H.264 video from MKV to MP4
    ffmpeg -i input.mkv -c:v copy -an output.mp4
    
    # Encapsulate AAC audio into MP4 container
    ffmpeg -i input.aac -vn -c:a copy output.mp4
  • Incorrect use of -c copy:

    bash
    # Error! Trying to convert H.264 to H.265; can't use copy
    ffmpeg -i input.mp4 -c:v libx265 output.mp4 -c copy # Wrong, don't add -c copy

5. Practical Exercises: Common FFmpeg Tasks to Elevate Your Media Processing Skills

  1. Video Format Conversion: Convert videos between formats, like translating languages for broader understanding.

    bash
    ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4
  2. Video Trimming: Extract highlights from videos, like editing a movie to emphasize the best parts.

    bash
    ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:05 -c copy output.mp4
  3. Extract Audio: Pull audio from videos, like extracting accompaniment from a song for remixing.

    bash
    ffmpeg -i input.mp4 -vn -c:a copy output.aac
  4. Merge Video and Audio: Combine video and audio into one file, like mixing food for easy consumption.

    bash
    ffmpeg -i video.mp4 -i audio.aac -c copy output.mkv
  5. Adjust Video Resolution: Change video size, like resizing a photo for different devices.

    bash
    ffmpeg -i input.mp4 -vf scale=640:480 output.mp4
    • -vf is equivalent to -filter:v