6月2日 00:00
How to use FFmpeg for video transcoding? What are the important encoding parameters?
Video transcoding is one of the most commonly used features of FFmpeg, involving the conversion of encoders, decoders, and container formats.
Basic Transcoding Commands
bash# Basic format conversion ffmpeg -i input.mp4 output.avi # Specify encoder ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4 # Specify encoding parameters ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Important Parameters
Video Encoding Parameters
-c:v: Specify video encoder (e.g., libx264, libvpx-vp9)-crf: Constant Rate Factor (0-51, lower value means higher quality, default 23)-preset: Encoding speed preset (ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow)-b:v: Video bitrate (e.g., 2M, 1500k)
Audio Encoding Parameters
-c:a: Specify audio encoder (e.g., aac, mp3, libopus)-b:a: Audio bitrate (e.g., 128k, 192k)-ar: Sample rate (e.g., 44100, 48000)
Hardware Accelerated Transcoding
bash# NVIDIA GPU acceleration ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.mp4 # Intel QSV acceleration ffmpeg -hwaccel qsv -i input.mp4 -c:v h264_qsv output.mp4
Common Scenarios
- Reduce video quality to decrease file size
bashffmpeg -i input.mp4 -crf 28 -preset slow output.mp4
- Extract video stream
bashffmpeg -i input.mp4 -an -c:v copy output.mp4
- Adjust resolution
bashffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
When transcoding, you need to select appropriate encoders and parameters based on the target platform and device to achieve a balance between quality and file size.