我是 ffprobe 的新手,我的目标是获取视频 fps 并存储到 java 程序中。我的代码存储 xml 文件,但我想直接存储 int fps=30;
ffprobe -v quiet -print_format xml -show_format -show_streams "/video/small/small.avi" > "/video/small/test.xml"
这是我的代码。
这将打印视频 FPS:
ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate file.mp4
您也可以简单地运行它,以获得视频 FPS,这将在 linux 机器上运行。
ffprobe -v quiet -show_streams -select_streams v:0 INPUT |grep "r_frame_rate"
我发现用另一种方法计算 fps 是..
String query = "ffmpeg -i foo.avi 2>&1 | sed -n 's/.*, \\(.*\\) fp.*/\\1/p' > fps.txt";
String[] command = {"gnome-terminal", "-x", "/bin/sh", "-c", query};
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
Thread.sleep(2000);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fps.txt")));
output = br.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
总之谢谢朋友。
接受的答案建议使用stream=r_frame_rate
. 如果您只需要稍微四舍五入的结果,这没关系。(30/1 而不是 ~29.7)
ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format csv="p=0" input.mp4 | read frames &&
ffprobe -i input.mp4 -show_entries format=duration -v quiet -of csv="p=0" | read duration &&
echo $(($frames/$duration))
>> 29.970094916135743
文件期限:
ffprobe -i input.mp4 -show_entries format=duration -v quiet -of csv="p=0"`
>> 15.367000
文件总帧数:
ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format csv input.mp4
>> 461
获取视频 FPS 并将其打印到标准输出:看到@geo-freak 的答案并将其添加以仅获取帧速率(删除额外的文本)。
ffprobe -v quiet -show_streams -select_streams v:0 input.mp4 |grep "r_frame_rate" | sed -e 's/r_frame_rate=//'
@o_ren 的回答似乎更合理。
执行相同操作的 Python 函数:
def get_video_frame_rate(filename):
result = subprocess.run(
[
"ffprobe",
"-v",
"error",
"-select_streams",
"v",
"-of",
"default=noprint_wrappers=1:nokey=1",
"-show_entries",
"stream=r_frame_rate",
filename,
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
result_string = result.stdout.decode('utf-8').split()[0].split('/')
fps = float(result_string[0])/float(result_string[1])
return fps