如何用FFmpeg提取视频每第5帧并按指定规则命名?有无其他参数?
Hey there! Let's break down how to get exactly what you want—extracting every 5th frame from your video, named like frame_0000.jpg, frame_0005.jpg, frame_0010.jpg, etc.—without messing with the frame rate (-r) parameter.
The Best Approach: Use the select Filter + -frame_pts Flag
The select filter lets you pick frames based on their original sequence number (not time), which is perfect for grabbing every 5th frame. Combine it with -frame_pts to retain the original frame number in your filenames.
Here's the command:
ffmpeg -i foo.avi -vf "select='not(mod(n,5))'" -frame_pts 1 -s WxH frame_%04d.jpg
Let's unpack each part:
-vf "select='not(mod(n,5))'": This filter selects frames where the original frame number (n, starting at 0) is divisible by 5.mod(n,5)calculates the remainder whennis divided by 5;not()inverts the result, so we only keep frames where the remainder is 0 (i.e., frames 0, 5, 10, 15...).-frame_pts 1: This tells FFmpeg to use the original frame's PTS (Presentation Time Stamp) as the number in the filename. For most standard videos, the PTS matches the frame number exactly (starting at 0), so this ensures your filenames get the correct 0, 5, 10... numbering.-s WxH: ReplaceWxHwith your desired resolution (e.g.,1920x1080).frame_%04d.jpg: The output filename template—%04densures the number has 4 leading zeros (so 0 becomes0000, 5 becomes0005, etc.).
What If -frame_pts Doesn't Match Frame Numbers?
If your video has non-standard PTS values (uncommon for most regular videos), you can use a more explicit filter chain to pass the original frame number into the filename. That said, the -frame_pts method is the simplest and most reliable for nearly all use cases, so it's your best first bet.
Why This Beats Adjusting FPS
Unlike using -r to set a lower frame rate, the select filter works directly with frame sequence numbers. You don't have to calculate the target frame rate based on your original video's FPS, which avoids potential timing drift or missed frames that can happen when messing with frame rate parameters.
内容的提问来源于stack exchange,提问作者jafar ali

