Understanding the Basic Syntax
The core structure of an FFmpeg command follows a predictable pattern that allows users to chain inputs and outputs efficiently. The general syntax begins with theffmpeg executable, followed by optional global options, then input specifications, and finally output specifications. Understanding this flow is critical because FFmpeg processes commands sequentially from left to right.
Inputs are identified using the -i flag, which tells FFmpeg where to read the source data. Outputs follow the input flags and specify the target file path and format. If you are converting multiple files, you can list multiple -i flags before the final output command. However, for simple single-file conversions, a straightforward linear command is usually sufficient.
For example, to convert a video file, you might use:
ffmpeg -i input.mp4 output.avi
This basic command instructs FFmpeg to read input.mp4 and write the result to output.avi. The tool automatically selects appropriate codecs based on the container extension, though explicit codec selection is often recommended for better control over the final file size and quality.
Encoding Images to Video
Converting static images into a video stream requires specific handling because images do not have a native duration. By default, FFmpeg assumes each image lasts for one second when creating a video sequence. You must provide a frame rate to determine how quickly these images transition. To create a video from a series of images, such asframe001.jpg, frame002.jpg, etc., you should use a wildcard pattern in the input filename. The -framerate option sets the playback speed, while -c:v specifies the video codec. Common choices include libx264 for high-quality H.264 encoding or mpeg4 for broader compatibility.
ffmpeg -framerate 30 -pattern_type glob -i '*.jpg' -c:v libx264 -pix_fmt yuv420p output.mp4
In this command, -pattern_type glob enables the use of shell-style wildcards, allowing FFmpeg to process all matching files in order. The -pix_fmt yuv420p parameter ensures pixel format compatibility with most players. If your images are PNGs, note that some older versions of FFmpeg may require additional steps to handle alpha channels correctly, but modern builds generally support them out of the box. For more details on handling specific image-to-video quirks, see Creating a Video Clip from an Image File using FFmpeg.
Encoding Video Files
When encoding existing video files, the primary goals are usually reducing file size or changing the container format without losing perceptible quality. The-b:v flag controls the video bitrate, which directly impacts file size and visual fidelity. A higher bitrate results in a larger file but better quality.
You can also change the audio codec simultaneously using the -c:a flag. For instance, converting to AAC audio is standard for MP4 containers. If you want to preserve the original audio quality, you can use -c:a copy to avoid re-encoding the audio stream entirely, which saves processing time and prevents generation loss.
ffmpeg -i source.mov -c:v libx264 -crf 23 -c:a aac -b:a 128k dest.mp4
Here, -crf 23 uses Constant Rate Factor for variable bitrate encoding, which is often superior to fixed bitrates for consistent quality. A CRF value between 18 and 28 is typical for good quality-to-size ratios. Lower values mean higher quality. Always test with a short segment first to ensure the settings meet your requirements.
Managing Output Directories
Saving processed frames or encoded files to a dedicated directory keeps your project organized. You can specify any valid path in the output argument. If the directory does not exist, FFmpeg will fail unless you create it beforehand using your operating system's tools. For batch processing, you can use scripts to loop through input files and generate corresponding outputs in a target folder. Ensure that the user running the FFmpeg command has write permissions to the destination directory. Permissions errors are common causes of failed encodings in automated workflows. If you encounter issues with video concatenation after encoding, refer to Fixing "Can't Concatenate Videos" Error in FFmpeg to resolve container incompatibilities.Frequently Asked Questions
How do I check if FFmpeg is installed correctly?
Run ffmpeg -version in your terminal to display the installed version number and build configuration. If the command returns "command not found," you need to install FFmpeg or add its binary path to your system environment variables.
What is the best codec for web video?
H.264 (libx264) is the most widely supported codec for web browsers and mobile devices. It offers an excellent balance between compression efficiency and compatibility, making it the default choice for MP4 uploads.
Can FFmpeg resize videos during encoding?
Yes, use the -vf scale=width:height filter to change resolution. For example, -vf scale=1280:720 resizes the video to HD dimensions while maintaining aspect ratio if you use :ih or similar scaling expressions.
Why is my encoded video black screen?
This often happens due to incorrect pixel format or missing keyframes. Ensure you are using -pix_fmt yuv420p and that the input file is not corrupted. Also, verify that the timestamp metadata is preserved using -copyts if necessary.
How do I extract audio from a video?
Use the -vn flag to disable video recording and specify an audio output file. For example, ffmpeg -i video.mp4 -vn audio.wav extracts the audio track into a WAV file without re-encoding if the format supports it.