FINALLY: A Bash Progress Indicator for ffmpeg that WORKS

I decided to write a progress indicator for ffmpeg video conversions. This was much much much harder than I ever anticipated. I was in for a tough time. Annoyingly, for what ever reason, ffmpeg doesn’t use Carriage Returns and Line Feeds in the expected way when it outputs its output during a conversion, resulting in two days of stress for a noobie bashophile like me.

Eventually, after hours of googling, I came across the solution. This script should be a very accurate indicator of the progress of a ffmpeg video conversion. It currently only tells you how far the conversion has progressed as a percentage, but I will soon add an ETA and time to it as well (now added, see update below). It does this, by looking at the number of frames your input video file has, and then reading what frame ffmpeg is currently on every ten seconds, and printing out the percentage.

You have to put in your own ffmpeg command if you want, mine is just for converting .mpg DVB-T recordings from MythTV to mkv files.
To use the script, call the script with the full path of the file to be converted afterwards, e.g.: “./ffmpeg-progress.sh /home/prupert/FIVE.mpg” and it gives you something like this:

UPDATE:

I have now added an ETA function, which, as far as I can tell, should be pretty accurate, I’ll be testing it tomorrow to see how well it works. It is based on the remaining number of frames to process and the current fps that ffmpeg is working at. I also plan to convert the script to python, if possible, to make it cross platform and give it a GUI. It will also be called YAFF (Yet Another Ffmpeg Frontend).

Essentially, the script turns ffmpeg output from this:

frame=42992 fps= 21 q=29.0 size= 217692kB time=1717.72 bitrate=1038.2kbits/s dup=12 drop=0
frame=43002 fps= 21 q=29.0 size= 217692kB time=1718.12 bitrate=1038.0kbits/s dup=12 drop=0
frame=43018 fps= 21 q=29.0 size= 217756kB time=1718.76 bitrate=1037.9kbits/s dup=12 drop=0
frame=43036 fps= 21 q=29.0 size= 217788kB time=1719.48 bitrate=1037.6kbits/s dup=12 drop=0
frame=43050 fps= 21 q=29.0 size= 217820kB time=1720.04 bitrate=1037.4kbits/s dup=12 drop=0
frame=43062 fps= 21 q=29.0 size= 217852kB time=1720.52 bitrate=1037.3kbits/s dup=12 drop=0
frame=43078 fps= 21 q=29.0 size= 217884kB time=1721.16 bitrate=1037.0kbits/s dup=12 drop=0
frame=43090 fps= 21 q=29.0 size= 217916kB time=1721.64 bitrate=1036.9kbits/s dup=12 drop=0
frame=43106 fps= 21 q=29.0 size= 217948kB time=1722.28 bitrate=1036.7kbits/s dup=12 drop=0
Last message repeated 2 times
[mpeg2video @ 0x8f7ebf0]ac-tex damaged at 15 0
[mpeg2video @ 0x8f7ebf0]concealing 90 DC, 90 AC, 90 MV errors
frame=43122 fps= 21 q=29.0 size= 217981kB time=1722.92 bitrate=1036.4kbits/s dup=12 drop=0
frame=43140 fps= 21 q=29.0 size= 218045kB time=1723.64 bitrate=1036.3kbits/s dup=12 drop=0
frame=43155 fps= 21 q=29.0 size= 218077kB time=1724.24 bitrate=1036.1kbits/s dup=12 drop=0
frame=43168 fps= 21 q=29.0 size= 218141kB time=1724.76 bitrate=1036.1kbits/s dup=12 drop=0
frame=43182 fps= 21 q=29.0 size= 218205kB time=1725.32 bitrate=1036.1kbits/s dup=12 drop=0
frame=43194 fps= 21 q=29.0 size= 218237kB time=1725.80 bitrate=1035.9kbits/s dup=12 drop=0
frame=43207 fps= 21 q=29.0 size= 218269kB time=1726.32 bitrate=1035.8kbits/s dup=12 drop=0

In to this:

/home/rupert/Downloads/Village of the Damned.mpg..mpg has 172447 frames, now converting
ffmpeg PID = 24868
ffmpeg: 43062 of 172447 frames, progress: 24% and ETA: 1h 42m 41s

Which must be a good thing in anyone’s book 😉

The script is hosted at github, enjoy:

https://github.com/pruperting/code_snippets/blob/master/ffmpeg_progress.sh

 

Creative Commons License
YAFF by Rupert Plumridge is licensed under a Creative Commons Attribution-Non-Commercial-Share Alike 2.0 UK: England & Wales License

Advertisements

14 thoughts on “FINALLY: A Bash Progress Indicator for ffmpeg that WORKS

  1. Hey, I tried it, it works, very nice, thank you for the effort, I’ll recommend it further.

    You know what could be improved though (take it as a constructive feedback): error handling when th ffmpeg fails for whatever reason.

    Thanks again, great work

  2. Hi, glad you like it. Constructive criticism is always welcome! Sadly, error handling with this script is very difficult. As you might have seen from the comments in the code, FFmpeg’s command line output is non-standard and varies in it’s format. Thus it is very hard to code to catch all the unusual things that FFmpeg might do to its output.

    I do however plan to add better error handling to the script when I get a moment. Currently, the script works well for the same type of video file, but for varied video files, it seems to have problems.

  3. Hi,

    Regarding error handling. At least when I convert files to different formats I always see “muxing overhead” when it’s successful. If so then one could say, if not “muxing overhead” then there was an error. Question, am I right about that? Does that string only show up upon sucessful completion of an ffmpeg process? Thanks.

  4. Hi Lee

    I am not too sure to be honest. I am pretty sure that it does always print that on successful conversion, to indicate how intensive the conversion process was. So if you are looking for a reliable way to determine that the conversion was successful, I am pretty sure that should work. Oh, and nice to read a comment with good grammar, so rare on the internet these days.

  5. media:/data/procesing# ./progress.sh /data/uploads/1/26/0cd40af2784ec948a13a4e5044d83a0e06102010134540_.mp4
    /data/uploads/1/26/0cd40af2784ec948a13a4e5044d83a0e06102010134540_.mp4 has 1604 frames, now converting
    ./progress.sh: line 88: syntax error near unexpected token `>’
    ./progress.sh: line 88: `trans “$1” &>> ~/ffmpeg.log &’
    media:/data/procesing#

  6. Dear Sir,

    I am an Ubuntu 10.04 user and I found a script to install ffmpeginv1.4.sh. the script worked just fine although I have two stupid question.

    1) Do I have to run the scrip for every user account on my system?
    the reason I am asking because, on admin account my video editor (Kdenlive) sees the codecs but on my guest account, the editor does not see them.

    2) How can I make sure that the codecs are installed for sure?

    I shall wait for your replies.

  7. Hi,

    While doing some project with ffmpeg, I googled for “FFMPEG progress output” and found your website. I already have some kind of progress functionality but I would like to integrate an estimated time counter.

    Any idea how to do this?

    The code I have so far is in PHP:

    <?php

    $content = @file_get_contents("temp/movie.txt");

    if($content){
    //get duration of source
    preg_match("/Duration: (.*?), start:/", $content, $matches);

    $rawDuration = $matches[1];

    //rawDuration is in 00:00:00.00 format. This converts it to seconds.
    $ar = array_reverse(explode(":", $rawDuration));
    $duration = floatval($ar[0]);
    if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
    if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;

    //get the time in the file that is already encoded
    preg_match_all("/time=(.*?) bitrate/", $content, $matches);

    $rawTime = array_pop($matches);

    //this is needed if there is more than one match
    if (is_array($rawTime)){$rawTime = array_pop($rawTime);}

    //rawTime is in 00:00:00.00 format. This converts it to seconds.
    $ar = array_reverse(explode(":", $rawTime));
    $time = floatval($ar[0]);
    if (!empty($ar[1])) $time += intval($ar[1]) * 60;
    if (!empty($ar[2])) $time += intval($ar[2]) * 60 * 60;

    //calculate the progress
    $progress = round(($time/$duration) * 100);

    echo "Duration: " . $duration . "”;
    echo “Current Time: ” . $time . “”;
    echo “Progress: ” . $progress . “% “;
    }

    ?>

  8. Hi, it seems like FFMPEG has changed, such that the script doesn’t quite work any more – I imagine the outputs from FFMPEG have changed.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.