Programster's Blog

Tutorials focusing on Linux, programming, and open-source

FFMPEG - Output Progress To File

I often use ffmpeg to convert videos on my local machine, but I need to move over to setting up a server running a website that I can upload videos to and have it perform the encoding. One of the biggest problems I've had though is figuring out an easy way for my website to check the progress of an encoding job. Luckily this can easily be resolved by adding a parameter to the ffmpeg command, telling it to output progress to a file, which can be easily parsed by a script.

Steps

If you add the following to any of your ffmpeg commands:

-progress progress-log.txt

... then FFmpeg will keep appending details about its progress to that file as it goes along. It does this with a set of name value pairs with each value being on its own line. A single progress update "chunk" is as follows:

frame=584
fps=52.40
stream_0_0_q=0.0
bitrate=N/A
total_size=762
out_time_us=0
out_time_ms=0
out_time=00:00:00.000000
dup_frames=0
drop_frames=0
speed=   0x
progress=continue

When an encoding is finished, the progress value will be set to end. Don't forget that the contents of the file is not overwritten, but appended to, so it will become a very long file like so:

frame=7149
fps=48.44
stream_0_0_q=0.0
bitrate=N/A
total_size=762
out_time_us=0
out_time_ms=0
out_time=00:00:00.000000
dup_frames=0
drop_frames=0
speed=   0x
progress=continue
frame=7206
fps=48.66
stream_0_0_q=0.0
bitrate=N/A
total_size=762
out_time_us=0
out_time_ms=0
out_time=00:00:00.000000
dup_frames=0
drop_frames=0
speed=   0x
progress=continue
frame=7233
fps=48.76
stream_0_0_q=0.0
bitrate=N/A
total_size=762
out_time_us=0
out_time_ms=0
out_time=00:00:00.000000
dup_frames=0
drop_frames=0
speed=   0x
progress=end

Script - Get Progress Percentage

If you are creating a script (php/python etc) to get the progress of the encoding, then I would recommend getting the number of frames there are in the video before the encoding with:

INPUT_FILE="input.mp4"

ffprobe \
  -v error \
  -select_streams v:0 \
  -count_packets \
  -show_entries stream=nb_read_packets \
  -of csv=p=0 \
  $INPUT_FILE

That should output an integer like:

7233

Then divide the frame counter in the progress output by this and multiply by 100 to get the percentage.

E.g.

7206 / 7233 * 100 = 99.63%

References

Last updated: 19th August 2021
First published: 19th August 2021

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch