使用 FFProbe 获取视频 fps

get video fps using FFProbe

我是 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 是..

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();
    }

无论如何谢谢朋友们。

这将打印视频 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 并将其打印到标准输出: 看到@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

接受的答案建议使用 stream=r_frame_rate。如果您只需要稍微四舍五入的结果,这没关系。 (30/1 而不是 ~29.7)

对于精确且不取整的 FPS,将总帧数除以持续时间:

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