日本語

Getting Conversion Progress from FFmpeg -progress

​​FFmpeg​command line,shell script,video

Adding -progress pipe:1 to an FFmpeg command makes it write the conversion's progress to standard output every 0.5 seconds, as name=value lines such as out_time_us=…. Dividing out_time_us by the video's length gives the percentage completed. You can use this to show a progress bar for a video conversion in your own script or app.

However, in my test environment I had to watch out for two points. The progress=end in the last block also appeared when I stopped the conversion partway. And although the name ends in ms, out_time_ms contained microseconds, not milliseconds.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and a virtual machine with 4 CPU cores. The source video was a 60-second MP4 (1280×720 30fps H.264, AAC) made from FFmpeg's test video and audio. The shell scripts ran in bash. I did not test FFmpeg 7 or later, Windows, or a TCP or HTTP URL as the destination for -progress.

Values output by -progress pipe:1

The description of the main ffmpeg options says that -progress sends machine-readable progress information to the specified URL. pipe:1 refers to standard output (see the pipe protocol description).

I added this option to a command that re-encodes the 60-second video to H.264 and AAC, and saved the output to a file. -nostats and -loglevel error suppress the usual display on standard error (explained in a later section).

bash
ffmpeg -y -i src.mp4 -c:v libx264 -preset medium -c:a aac -progress pipe:1 -nostats -loglevel error out.mp4 > progress1.txt

It finished in 14.3 seconds and wrote 348 lines. Each block is 12 lines, and there were 29 blocks. The third block was as follows:

text
frame=76
fps=74.50
stream_0_0_q=29.0
bitrate=1474.6kbits/s
total_size=786480
out_time_us=4266667
out_time_ms=4266667
out_time=00:00:04.266667
dup_frames=0
drop_frames=0
speed=4.18x
progress=continue

As the official documentation describes, the last line of each block is always progress, and its value is either continue or end. Of the 29 blocks, 28 were continue and the last one was end. The last block was frame=1800, out_time=00:00:59.989333, progress=end.

The documentation does not explain what each value means. I checked by reading the code that writes these lines (print_report) in fftools/ffmpeg.c in FFmpeg 6.1.1.

Name Meaning
frame Number of frames written to the output from the first video stream
fps Frames processed per second. This is an average since the start; it is 0 during the first second.
stream_0_0_q Video encoder quality value (output file 0, stream 0). It was -1.0 in the last block.
bitrate Bitrate, calculated as the output size so far divided by out_time
total_size Size of the output file written so far, in bytes
out_time_us Timestamp, in microseconds, of the packets handed to the muxer (the part that assembles the output file). It is the further-ahead of video and audio.
out_time_ms Same value as out_time_us
out_time Same timestamp, written as hours:minutes:seconds
dup_frames, drop_frames Number of frames duplicated and dropped to match the frame rate
speed out_time ÷ elapsed time. An average since the start, showing how many times faster than real time the processing is.

In the source code, out_time_ms writes exactly the same value as out_time_us. In the actual output, the two numbers matched in every block. If you divide by 1000 as if it were milliseconds, you will be off by a factor of 1000. Use out_time_us.

Showing the percentage completed

To get the percentage, divide out_time_us by the input length in microseconds. Get the input length with ffprobe. The following shell script prints one line each time a block ends (the progress line).

bash
#!/bin/bash
# Usage: ./progress2.sh input_file output_file
in="$1"; out="$2"
# Get the input length (seconds) with ffprobe and convert it to microseconds
dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$in")
dur_us=$(awk -v d="$dur" 'BEGIN{printf "%d", d*1000000}')

ffmpeg -y -i "$in" -c:v libx264 -preset medium -c:a aac \
  -progress pipe:1 -nostats -loglevel error "$out" |
while IFS='=' read -r key value; do
  case "$key" in
    out_time_us) t="$value" ;;
    speed) speed="${value// /}" ;;
    progress)
      # Right after start, the value can be N/A or negative, so only print when it is a number of 0 or more
      [[ "$t" =~ ^[0-9]+$ ]] && awk -v t="$t" -v d="$dur_us" -v s="$speed" -v p="$value" \
        'BEGIN{printf "%5.1f%%  速度 %s  %s\n", t*100/d, s, p}' ;;
  esac
done
# progress=end is also printed when stopped partway, so check success with the ffmpeg exit code
status=${PIPESTATUS[0]}
if [ "$status" -eq 0 ]; then echo "完了しました"; else echo "失敗しました(終了コード $status)"; fi

The speed value sometimes had a leading space, as in speed= 4.3x, so the script removes spaces with ${value// /}.

When run, it printed one line every 0.5 seconds and ended at 100.0% (some intermediate lines are omitted).

text
  0.1%  速度 12.6x  continue
  4.0%  速度 4.71x  continue
  7.7%  速度 4.59x  continue
 11.1%  速度 4.4x  continue
(略)
 93.9%  速度 4.3x  continue
 97.2%  速度 4.28x  continue
100.0%  速度 4.17x  end
完了しました

To estimate the remaining time, divide the remaining video length by speed. Because speed is an average since the start, it works as a rough estimate. For example, if a 60-second video is 48.8% done and speed is 4.15x, the remaining video is about 30.7 seconds long, which takes about 7.4 seconds.

Watch out for values right after the start

The script checks that out_time_us is a number of 0 or more because the first block is not always a number. When I encoded only video with audio removed (-an), the first block had out_time=N/A. When I copied without re-encoding (-c copy), the first block had a negative value: out_time_us=-21333 and out_time=-00:00:00.021333.

With -c copy, the frame, fps, and stream_0_0_q lines did not appear at all. In the source code, these three are written only when there is a video stream that goes through an encoder and filters. A script that depends on frame cannot get values with -c copy.

Percentages differed by 3 points between out_time and frame

Because the video is 60 seconds at 30 fps, you can also get the progress by dividing the frame count (1800). For the same conversion, I compared the percentage from out_time_us and the percentage from frame, by elapsed time since ffmpeg started.

Graph comparing progress by elapsed time when encoding a 60-second video, drawn as two lines. The light blue line, from out_time_us, stays about 3 points above the gray line, from frame, for most of the run. At 7.14 seconds, the values are 51.9% and 48.9%. Both reach 100% at about 15 seconds
Comparison of progress from out_time_us and frame (graph drawn from measured values in the test environment)

At 7.14 seconds, out_time_us gave 51.9% and frame gave 48.9%. Throughout most of the run, the out_time_us value was about 3 points ahead, and both reached 100% at the end.

This difference appeared only when there was audio. With -an (video only), out_time was roughly equal to the frame count divided by 30, for example out_time=00:00:13.966667 at frame=421. In the source code, out_time is the furthest-ahead of the timestamps of the packets handed to the muxer, checked separately for video and audio. Audio packets were handed to the muxer earlier than video packets, so out_time ran ahead by that amount.

Either way, both reach 100% at the end. out_time_us can be calculated without knowing the total frame count, and it still has values with -c copy, so I think it is easier to work with and prefer it.

progress=end does not mean the conversion succeeded

To check the output when a conversion is stopped partway, I started a conversion with -progress pipe:1 and sent SIGINT to ffmpeg after 5 seconds. This is the same signal sent when you press Ctrl+C in a terminal. The last block was as follows, and it contained progress=end.

text
frame=558
fps=106.58
stream_0_0_q=-1.0
bitrate=2880.2kbits/s
total_size=6728204
out_time_us=18688000
out_time_ms=18688000
out_time=00:00:18.688000
dup_frames=0
drop_frames=0
speed=3.57x
progress=end

The ffmpeg exit code was 255. The output file remained as a video 18.709 seconds long. In the source code, the progress value only indicates whether this is the final report, so continue and end are distinguished on that basis. It does not check whether the conversion finished. Even after SIGINT exits the conversion processing, the final report is still written.

Warning

If you treat progress=end as a sign that the conversion completed, you will also treat conversions that were stopped partway as complete. Check for success by whether the ffmpeg exit code is 0.

The script above decides success or failure from the exit code. As if pressing Ctrl+C in a terminal, I sent SIGINT to both the script and ffmpeg 3 seconds after starting the script. The output was as follows: it reached end at 21.1% and was treated as a failure.

text
  0.1%  速度 12x  continue
  4.1%  速度 4.84x  continue
  7.4%  速度 4.41x  continue
 10.7%  速度 4.21x  continue
 14.4%  速度 4.27x  continue
 18.1%  速度 4.29x  continue
 21.1%  速度 3.96x  end
失敗しました(終了コード 255)

Changing the output interval

You can change the output interval with -stats_period. According to the official documentation, the default is 0.5 seconds. With -stats_period 2, output was written every 2 seconds.

bash
ffmpeg -y -i src.mp4 -c:v libx264 -preset medium -c:a aac -progress pipe:1 -stats_period 2 -nostats -loglevel error out3.mp4

In the following output, the leading value is the elapsed time since ffmpeg started when each block was received.

text
経過  0.07秒  frame=0 out_time=00:00:00.085333 progress=continue
経過  2.07秒  frame=208 out_time=00:00:08.661333 progress=continue
経過  4.07秒  frame=459 out_time=00:00:17.045333 progress=continue
(略)
経過 14.08秒  frame=1670 out_time=00:00:57.408000 progress=continue
経過 15.03秒  frame=1800 out_time=00:00:59.989333 progress=end

The first block appeared right away, and after that, blocks came every 2 seconds. The final end block was written when the conversion finished, regardless of the interval.

Writing to a file and reading it from another program

If you give a file name to -progress, the output is written to that file. Blocks were not overwritten; they were appended to the end. I ran ffmpeg in the background and, every 2 seconds, read the last out_time and progress lines in the file.

bash
ffmpeg -y -i src.mp4 -c:v libx264 -preset medium -c:a aac -progress progress.txt -nostats -loglevel error out5.mp4 &
text
[2秒後] out_time=00:00:05.674667  progress=continue  行数=48
[4秒後] out_time=00:00:13.141333  progress=continue  行数=96
(略)
[16秒後] out_time=00:00:57.130667  progress=continue  行数=384
[18秒後] out_time=00:00:59.989333  progress=end  行数=408

The line count increased by 48 lines (4 blocks) every 2 seconds. The reader only needs to look at the last block in the file to know the progress at that moment.

Suppressing the usual output on standard error

Apart from -progress, ffmpeg also writes progress lines to standard error, such as frame= 1800 fps=355 q=-1.0 Lsize=…. If you display progress yourself, suppress these. To finish quickly, I used -preset ultrafast, and added -hide_banner (which hides the version banner at startup) to every conversion. Then I counted the lines starting with frame= that appeared on standard error.

Option added Lines starting with frame= on standard error Size of standard error
None 12 lines 4,390 bytes
-nostats 1 line 3,313 bytes
-loglevel error 0 lines 0 bytes

According to the official documentation, this display is the info-level log produced by -stats, and it is shown by default. To stop it, specify -nostats. Even with -nostats, the final line (the summary when the conversion finishes) was still printed. Setting -loglevel error suppresses all info-level logs, so this display disappeared too. In every case, the -progress output was written to standard output.

​Posted by 86