简体   繁体   English

FFprobe,如何以JSON格式以十进制形式获取动画gif的帧速率

[英]FFprobe, how to get frame rate of animated gif as a decimal in JSON format

When I used ffprobe against an animated gif, I get, among other things, this:当我对动画 gif 使用 ffprobe 时,除其他外,我得到以下信息:

> ffprobe.exe  foo.gif
. . . 
Stream #0:0: Video: gif, bgra, 500x372, 6.67 fps, 6.67 tbr, 100 tbn, 100 tbc

Great;伟大的; this tells me the frame rate is 6.67 frames per second.这告诉我帧速率是每秒 6.67 帧。 But I'm going to be using this in a program and want it in a parsed format.但是我将在程序中使用它并希望它以解析格式。 ffprobe does json, but when I use it: ffprobe 做 json,但是当我使用它时:

> ffprobe.exe -show_streams -of json foo.gif

The json shows: json 显示:

"r_frame_rate": "20/3",
"avg_frame_rate": "20/3",

But I want the decimal form 6.67 instead of 20/3.但我想要十进制形式 6.67 而不是 20/3。 Is there a way to have FFProbe produce its JSON output in decimal?有没有办法让 FFProbe 以十进制生成其 JSON 输出? I can't seem to find it in the docs.我似乎无法在文档中找到它。

My platform is Windows;我的平台是Windows; FFProbe is version N-68482-g92a596f. FFProbe 是版本 N-68482-g92a596f。

I did look into using ImageMagick, but the GIF file in question is corrupted (I'm working on a simple repair program);我确实考虑过使用 ImageMagick,但有问题的 GIF 文件已损坏(我正在开发一个简单的修复程序); IM's "identify" command halts on it, while FFMpeg & FFProbe handle it just fine. IM 的“识别”命令会停止,而 FFMpeg 和 FFProbe 处理得很好。

Addition: this is kind of academic now;另外:这现在有点学术性; I just used (in Python):我刚刚使用过(在 Python 中):

framerate_as_decimal = "%4.2f" % (float(fractions.Fraction(framerate_as_fraction)))

But I'm still kind of curious if there's an answer.但我还是有点好奇,如果有答案。

I know this is a bit old question but today I have tried to do the same and found two options:我知道这是一个有点老的问题,但今天我尝试做同样的事情并找到了两个选项:

  1. You can use the subprocess module in python and mediainfo: fps = float(subprocess.check_output('mediainfo --Inform="Video;%FrameRate%" input.mp4, shell=True)) here the returned value is a string, that's why I am converting it to float.你可以在python和mediainfo中使用subprocess模块​​: fps = float(subprocess.check_output('mediainfo --Inform="Video;%FrameRate%" input.mp4, shell=True))这里返回的值是一个字符串,就是为什么我将它转换为浮动。 Unfortunately I wasn't able to execute the same thing without the shell=True but perhaps I am missing something.不幸的是,我无法在没有shell=True的情况下执行相同的操作,但也许我遗漏了一些东西。
  2. Using ffprobe: ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4 here the problem is that the output is 50/1 or in your case 20/3 so you need to split the output by "/" and then to convert and divide the two elements of the list.使用 ffprobe: ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4这里的问题是输出是 50/1 或者在你的情况下是 20/3 所以您需要用“/”分割输出,然后转换和分割列表的两个元素。 Something like:就像是:

fps = subprocess.check_output(['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=avg_frame_rate', '-of', 'default=noprint_wrappers=1:nokey=1', 'input.mp4']) fps_lst = fps.split('/') fps_real = float(fps_lst[0]) / int(fps_lst[1])

So the normal commands for getting the frame rate are: ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4 and mediainfo --Inform="Video;%FrameRate%" input.mp4所以获取帧率的正常命令是: ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4mediainfo --Inform="Video;%FrameRate%" input.mp4

In Python, you can just use:在 Python 中,你可以只使用:

frame_rate_str = "15/3"
frame_rate = eval(frame_rate_str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM