FFmpeg is an open-source command-line tool for handling video, audio, and other multimedia files and streams. It supports vrious tasks, from video conversion to advanced encoding settings to make it an essential tool for video professionals.

Basic Video Conversion

FFmpeg allows users to convert video files between formats, adjusting quality or other output settings.

Convert Video to MP4

This command converts an AVI file to MP4 format. The input file is input.avi, and the output file is output.mp4.

code
ffmpeg -i input.avi -c:v libx264 output.mp4
FFmpeg AVI to MP4 Conversion Output Using libx264 Encoder

Video Codec Selection

FFmpeg provides support for a variety of video codecs. Selecting the appropriate codec optimizes video quality and file size.

Encode to H.264

This command encodes the video using the H.264 codec (libx264). The input video file is input.mp4, and the output is also an MP4 file encoded with H.264.

code
ffmpeg -i input.mp4 -c:v libx264 output.mp4
FFmpeg H.264 Encoding Output Using libx264 Codec
Banner

Encode to HEVC (H.265)

This command converts the video to the HEVC (H.265) codec to provide a better compression than H.264 at the same quality level.

code
ffmpeg -i input.mp4 -c:v libx265 output.mp4
FFmpeg HEVC (H.265) Encoding Output Using libx265 Codec

Encode to VP9 (for Web)

Converts the video to VP9 format, commonly used for web-based streaming on platforms like YouTube.

code
ffmpeg -i input.mp4 -c:v libvpx-vp9 output.webm
FFmpeg VP9 Encoding Output for Web-Based Streaming (libvpx-vp9 Codec)

Bitrate Control

Bitrate is a key factor in determining video quality and file size. Users can use FFmpeg to specify a target bitrate during encoding.

Set Constant Bitrate (CBR)

The video is encoded with a constant bitrate of 1 Mbps (-b:v 1M). This is useful for streaming platforms where consistent bandwidth is required.

code
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M output.mp4
Constant Bitrate (CBR) Encoding Output at 1 Mbps Using libx264

Set Variable Bitrate (VBR)

The CRF (Constant Rate Factor) value controls the video quality for variable bitrate encoding. Lower CRF values (e.g., 18) result in better quality and larger file sizes, while higher values (e.g., 28) reduce file size at the cost of quality. A value of 23 is considered a good default.

code
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
Variable Bitrate (VBR) Encoding Output with CRF 23 Using libx264

Resolution and Scaling

Scaling videos is useful for optimizing content for different devices and platforms.

Scale Video to a Specific Resolution

This command scales the video to 1280x720 resolution (HD). Use this for reducing video size or preparing for specific device requirements.

code
ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4
Scaling Video to 1280x720 Resolution Output

Auto-Scaling to Maintain Aspect Ratio

The -1 in the scale option automatically adjusts the height to maintain the aspect ratio while scaling the width to 1280px.

code
ffmpeg -i input.mp4 -vf "scale=1280:-1" output.mp4
Auto-Scaling Video While Preserving Aspect Ratio

Audio Settings

Audio settings such as codec, bitrate, and sample rate must be configured to match the requirements of the target playback environment.

Change Audio Codec (AAC)

This command converts the audio codec to AAC with a bitrate of 128kbps. AAC is widely used in modern video formats for its efficiency.

code
ffmpeg -i input.mp4 -c:a aac -b:a 128k output.mp4
Converting Audio Codec to AAC with 128 kbps Bitrate

Change Audio Sample Rate

This sets the audio sample rate to 44.1 kHz, which is standard for most audio playback systems.

code
ffmpeg -i input.mp4 -ar 44100 output.mp4
Changing Audio Sample Rate to 44.1 kHz

Frame Rate and Frame Rate Conversion

FFmpeg provides tools to set or adjust the frame per second rate (FPS) for compatibility with different platforms.

Set Frame Rate

This command sets the video frame rate to 30 FPS for most online streaming platforms.

code
ffmpeg -i input.mp4 -r 30 output.mp4
Setting Frame Rate to 30 FPS Using FFmpeg

Convert Frame Rate

This converts the video to 25 FPS. Use this for adjusting videos to match different regional broadcast standards (e.g., PAL).

code
ffmpeg -i input.mp4 -filter:v "fps=25" output.mp4
Converting Frame Rate to 25 FPS Using fps Filter

Video Filters and Effects

FFmpeg includes a wide range of video filters to apply visual effects and corrections.

Apply Grayscale Filter

This command converts the video to grayscale using the hue=s=0 filter to removes the color saturation.

code
ffmpeg -i input.mp4 -vf "hue=s=0" output.mp4
Applying Grayscale Filter Using FFmpeg

Crop Video

This command crops the video to a resolution of 640x480, starting from the top-left corner (0,0).

code
ffmpeg -i input.mp4 -vf "crop=640:480:0:0" output.mp4
Cropping Video to 640x480 from Top-Left Corner Using FFmpeg

Video Conversion to Streaming Formats

FFmpeg supports various streaming protocols for live streaming or on-demand services.

Create an HLS Stream

This command generates an HLS stream from a video file. It breaks the video into 10-second segments and generates a playlist.m3u8 file for streaming.

code
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 5 -hls_segment_filename "segment_%03d.ts" playlist.m3u8
Generating HLS Stream with FFmpeg and Playlist Output

Trimming and Seeking

FFmpeg supports precise trimming and seeking to extract parts of a video without re-encoding (or with re-encoding if needed).

Trim Video from Start Time to Duration

This command trims 10 seconds starting from 00:00:05, without re-encoding the video.

code
ffmpeg -ss 00:00:05 -t 10 -i input.mp4 -c copy output.mp4
Trimming Video from Start Time Using FFmpeg

Trim Video Between Two Time Points

This command trims the video from 00:00:10 to 00:00:30, without re-encoding the video.

code
ffmpeg -ss 00:00:10 -to 00:00:30 -i input.mp4 -c copy output.mp4
Trimming Video Between Two Time Points Using FFmpeg

Remuxing (Container Change Without Re-encoding)

Remuxing allows changing the container format without altering the video/audio streams to offer fast and lossless conversion.

Convert MKV to MP4 Without Re-encoding

This command changes the container format from MKV to MP4 without re-encoding the video or audio.

code
ffmpeg -i input.mkv -c copy output.mp4
Remuxing MKV to MP4 Without Re-encoding Using FFmpeg

Codec Listing and Exploration

For discovering available codecs and understanding what FFmpeg can work with, use the following:

List All Supported Codecs

This command lists all codecs supported by FFmpeg, both video and audio

code
ffmpeg -codecs
Listing All Supported Codecs

List All Supported Formats

This command lists all the formats (containers) supported by FFmpeg.

code
ffmpeg -formats
Listing All Supported Formats

File Size Limiting

Constrain the output file size using the -fs flag to prepare videos for limited storage or upload requirements.

Limit Output File Size to 50 MB

This command limits the output file size to 50 MB. It may involve reducing the quality or bitrate of the video.

code
ffmpeg -i input.mp4 -fs 50M output.mp4
Limiting Output File Size to 50MB

What"s Next?

Want to integrate FFmpeg into your production pipeline? Use Cincopa"s API to trigger FFmpeg-based transcodes on upload, manage codec presets, batch process asset libraries, and generate ABR-ready renditions. Automate format conversions, scaling, and filtering at scale while embedding FFmpeg logic directly into your backend workflows.