【问题标题】:get video fps using FFProbe使用 FFProbe 获取视频 fps
【发布时间】:2015-03-03 18:46:11
【问题描述】:

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

这是我的代码。

【问题讨论】:

    标签: java ffmpeg ffprobe


    【解决方案1】:

    这将打印视频 FPS:

    ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate file.mp4
    

    【讨论】:

    • 是的,它打印 FPS,但仅作为精确分数。幸运的是,你可以把这个分数放入计算器中得到十进制值。例如,如果此命令的答案是 30000/1001,您可以将其放入计算器中,得到 29.97002997,四舍五入到大约 30 FPS。
    • 您可以将“-show_entries stream=r_frame_rate”替换为“-show_entries stream”以获取所有可用值的列表,并使用“-of json/flat/...”获取数据如果需要,采用更易解析的格式
    • 我注意到 r_frame_rate 会返回误导性的数字! avg_frame_rate 更正确。
    • 要获取文件夹中所有视频的 FPS 统计信息,请使用 ls *.mp4 | xargs -n 1 -- ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate | sort | uniq -c
    【解决方案2】:

    您也可以简单地运行它,以获取视频 FPS,这将在 linux 机器上运行。

    ffprobe -v quiet -show_streams -select_streams v:0 INPUT |grep "r_frame_rate"
    

    【讨论】:

    • 不需要grep。只需使用 ffprobe 本身,如接受的答案所示。
    【解决方案3】:

    我发现用另一种方法计算 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();
        }
    

    还是谢谢朋友们。

    【讨论】:

      【解决方案4】:

      获取视频 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
      

      【讨论】:

        【解决方案5】:

        接受的答案建议使用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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-11-23
          • 1970-01-01
          • 2018-04-12
          • 2019-11-11
          • 2022-11-10
          • 2021-03-29
          • 2019-03-26
          相关资源
          最近更新 更多