如何将FFmpeg视频转码与切割命令合并为单一命令?
Great question! As someone who's worked with FFmpeg for media processing tasks, I totally get wanting to streamline this workflow instead of juggling multiple commands and intermediate files. The good news is you can absolutely combine both steps into a single FFmpeg command using its built-in segment muxer, which handles transcoding and automatic splitting based on file size (or duration) in one go.
Here's how to do it:
The core idea is to use FFmpeg's segment output format, which will split the transcoded stream into chunks once each reaches your specified maximum size. Here's the command tailored to your needs:
ffmpeg -i input.mp4 -codec:v mpeg2video -qscale:v 0 -f segment -segment_size 104857600 output_%03d.mp2
Let's break down the key parameters:
-f segment: Tells FFmpeg to use the segment muxer for output, which enables automatic splitting of the transcoded stream.-segment_size 104857600: Sets the maximum size per chunk (in bytes). In this example, that's 100MB (since 1MB = 1024*1024 = 1048576 bytes; multiply your desired limit by this value to get the correct number). Adjust this to match your "specific size" requirement.output_%03d.mp2: The output filename template. The%03dwill be replaced with a 3-digit sequential number (e.g.,output_000.mp2,output_001.mp2, etc.), ensuring your chunks stay ordered without overwriting each other.
Bonus: Splitting by duration instead of size
If you ever want to split by time (like your original 10-minute example), you can replace -segment_size with -segment_time 600 (where 600 equals 10 minutes in seconds):
ffmpeg -i input.mp4 -codec:v mpeg2video -qscale:v 0 -f segment -segment_time 600 output_%03d.mp2
Why this beats your original workflow:
- No intermediate files: You skip the step of transcoding the entire video to a huge MP2 (or splitting the original MP4 first then transcoding chunks). This saves disk space and cuts down on total processing time.
- Hands-off splitting: FFmpeg automatically cuts the stream as soon as each chunk hits your size limit—no manual file size checks or cut point calculations needed.
A quick tip for beginners:
Your -qscale:v 0 setting produces lossless MPEG-2 video, which results in very large files. If your size limit is small, this could lead to dozens of chunks. If quality allows, you can increase the -qscale:v value (e.g., to 2, 3, or 5) to compress the video more—higher numbers mean smaller files with slightly reduced quality.
内容的提问来源于stack exchange,提问作者Robin

