Frame Rate Calculator
By T. H. Wright
Published:
Last Updated:
When I began 3D printing, I watched instructional and introductory videos to tell me more about the process, the art, the product, the machine, and the rest of the lot. The videos often included time lapses of the featured print and I thought it would be a perfect addition to Redemption Studio. When I published A Dice Tower Gift, Part Two, I mentioned I would provide the scripts I wrote in order to produce the videos. All in all, I constructed six scripts which I used to learn FFMPEG, Montage, and several other programs, not to mention Bash’s getopts, data validation, arrays and more. It felt rewarding by the end of the process! Those scripts are provided in the Bash Scripts downloads.
The six scripts I’ve posted are titled as follows.
- Frame Rate Calculator
- FFMPEG: Convert Images to MP4
- FFMPEG: Convert Video File to Images
- FFMPEG: Juxtapose 2 Videos Horizontally
- FFMPEG: Juxtapose 2 Videos Vertically
- FFMPEG: Montage 2x2 Grid Video
I debated between the MIT, Apache 2.0, or the GPL V3 licenses when publishing this code and I opted for the GPL V3 on the basis of keeping the code available to the community. I’m not a legal expert and the other two each seem to have their merits, but for the purpose of these projects the GPL V3 seemed appropriate.
This first one is quite simple. All it does is calculate frame rates of a video file. Depending on the file I give the command, I sometimes receive feedback of 0/0 and I am not sure why that is other than the file format. (If I ever get around to it, I’ll research why that is.)
Here’s the meat of the script. (The rest is just for formatting and accepting command line interface (CLI) input.)
ffprobe -v 0 -of compact=p=0 -select_streams 0 -show_entries stream=r_frame_rate $FILE
Most of what I’m sharing can be read in ffprobe’s manual (man) page. I couldn’t find what the r_frame_rate was in the man page, so I found it in the documentation for ffmpeg. You can read about it here: r_frame_rate.
The -v 0
flag disables the output of ffprobe. Since the script is meant to function as a sort of frontend for media tools, this is desirable.
The -of compact=p=0
option sets our “writer” which determines our output and through p=0
—which specifies the format—we disable that output. Sometimes we are just told too much.
The next portion of our command is the option -select_streams 0
. Common to the FFMPEG tools is the manipulation of streams and for our purposes, we tell ffprobe we want to manipulate the first stream. (In computing, 0 is the first number we count with due to binary.)
Our next command option is -show_entries stream=r_frame_rate
. This pulls specific information from our stream, which happens to be what we want to know: “the lowest framerate with which all timestamps can be represented accurately”.1
After this, we give ffprobe our file through a Bash variable.
There isn’t anything masterfully scripted in here, even the above command. The remainder of the script prettifies the format. I have noticed I don’t get output all the time, but I didn’t write any data validation into this script and I can’t vouch for what will happen if you run it on a non-mp4 file. Removing our flags to add output to our command could be of use as we may get error messages which would explain what is happening and why we are receiving no output. Take the script, modify it, and see if you can get videos with no output to produce some!
Hopefully this helps you out as your work to create your own videos. I hope to write up some information on the other scripts as well but in the meantime, feel free to access them and review them yourself.