用ffmpeg获取帧数 [英] Fetch frame count with ffmpeg
问题描述
有人知道如何使用ffmpeg从视频文件中提取总帧数吗? ffmpeg的渲染输出显示当前帧,我需要帧计数来计算进度百分比.
Does anyone know how to fetch the number of total frames from a video file using ffmpeg? The render output of ffmpeg shows the current frame and I need the frame count to calculate the progress in percent.
推荐答案
注意:MP4/M4V/M4A/MOV中存在编辑列表会影响您的帧号.请参见下面的编辑列表.
ffprobe:查询容器
Note: The presence of an edit list in MP4/M4V/M4A/MOV can affect your frame number. See Edit lists below.
ffprobe: Query the container
ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 input.mp4
- 这是一种快速的方法.
- 并非所有格式(例如Matroska)都将报告导致输出
N/A
的帧数.请参阅下面列出的其他方法. - This is a fast method.
- Not all formats (such as Matroska) will report the number of frames resulting in the output of
N/A
. See the other methods listed below.
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 input.mkv
- 这是一种缓慢的方法.
- 添加
-skip_frame nokey
选项以仅计算关键帧. - This is a slow method.
- Add the
-skip_frame nokey
option to only count key frames.
如果您没有ffprobe
,则可以改用ffmpeg
:
If you do not have ffprobe
you can use ffmpeg
instead:
ffmpeg -i input.mkv -map 0:v:0 -c copy -f null -
- 这是一种快速的方法.
- 请参阅控制台输出结尾附近的
frame=
. - 添加
-discard nokey
输入选项(在-i
之前)以仅计算关键帧. - This is a somewhat fast method.
- Refer to
frame=
near the end of the console output. - Add the
-discard nokey
input option (before-i
) to only count key frames. -
-v error
这将隐藏信息"输出(版本信息等),这使解析变得更加容易. -v error
This hides "info" output (version info, etc) which makes parsing easier.- FFprobe Documentation
- FFmpeg Wiki: FFprobe Tips
使用-ignore_editlist 1
输入选项忽略MP4/M4V/M4A/MOV编辑列表.默认值为不.忽略编辑列表.
Ignore the MP4/M4V/M4A/MOV edit list with the -ignore_editlist 1
input option. Default is to not ignore the edit list.
-count_frames
计算每个流的帧数,并在相应的流部分中报告.
-count_frames
Count the number of frames per stream and report it in the corresponding stream section.
-select_streams v:0
仅选择视频流.
-show_entries stream=nb_frames
或-show_entries stream=nb_read_frames
仅显示nb_frames
或nb_read_frames
的条目.
-of default=nokey=1:noprint_wrappers=1
将输出格式(也称为书写器")设置为default
,不要打印每个字段的键(nokey=1
),也不要打印节的页眉和页脚(-of csv=p=0
.
-of default=nokey=1:noprint_wrappers=1
Set output format (aka the "writer") to default
, do not print the key of each field (nokey=1
), and do not print the section header and footer (noprint_wrappers=1
). There are shorter alternatives such as -of csv=p=0
.
著名的mediainfo
工具可以输出帧数:
The well known mediainfo
tool can output the number of frames:
mediainfo --Output="Video;%FrameCount%" input.avi
MP4Box
用于MP4/M4V/M4A文件.
MP4Box
For MP4/M4V/M4A files.
MP4Box
可以显示帧数:
MP4Box -info input.mp4
请参考相关视频流的输出中的Media Info
行:
Refer to the Media Info
line in the output for the video stream in question:
Media Info: Language "Undetermined (und)" - Type "vide:avc1" - 2525 samples
在此示例中,视频流具有2525帧.
In this example the video stream has 2525 frames.
用于MP4/M4V/M4A/MOV文件.
For MP4/M4V/M4A/MOV files.
boxdumper
是l-smash的简单工具.它将输出大量信息.在stsz
样本大小框部分下,请参考sample_count
以获取帧数.在此示例中,输入具有1900个视频帧:
boxdumper
is a simple tool from l-smash. It will output a large amount of information. Under the stsz
sample size box section refer to sample_count
for the number of frames. In this example the input has 1900 video frames:
boxdumper input.mp4
...
[stsz: Sample Size Box]
position = 342641
size = 7620
version = 0
flags = 0x000000
sample_size = 0 (variable)
sample_count = 1900
- 请注意,一个文件可能包含多个
stsz
原子. - Be aware that a file may have more than one
stsz
atom.
这篇关于用ffmpeg获取帧数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!