Pyrometheous
Pyrometheous

Reputation: 65

Python, grab frame number from FFMPEG

I'm using a script to compile an image sequence into a video.

def ffmpeg_image_sequence(image_sequence, video, fps, encoder):
        global task
        task = False
        path = os.path.dirname(os.path.realpath(image_sequence))
        image_format = {
            'png': '\%06d.png',
            'iff': '\%06d.tiff',
            'tif': '\%06d.tif',
            'jpg': '\%06d.jpg'
        }
        sequence = path + image_format[image_sequence[-3:]]
        output_options = {
            'crf': 20,
            'preset': 'slow',
            'movflags': 'faststart',
            'pix_fmt': 'yuv420p',
            'c:v': encoder,
            'b:v': '20M'
        }
        try:
            (
                ffmpeg
                    .input(sequence, framerate=fps)
                    .output(video, **output_options)
            ).run()
        except ffmpeg.Error as e:
            warning(str(e))
        task = True

I'm using wxPython for the GUI and ffmpeg for compiling the frames:

import wx
import ffmpeg

I'm running the above function in its own class so that it can be run in a different thread, allowing the statusbar to be updated with how much time has passed.

def wait(start, text):
    time.sleep(.05)
    time_running = round(time.time() - start)
    update_status_bar(main_window, text + ' (Time Elapsed: ' + seconds_to_str(time_running) + ')')

I'd like to replace this with ETA and percentage completed. The window that pops up when running the ffmpeg_image_sequence function displays the following information:

frame=1234 fps= 16 q=13.0 size= 56789kB time=00:38:48.36 bitrate=12345.6kbits/sec speed=0.681x

From what's printed to that window, I'd like to get the current frame= value to determine what percentage of the process is completed, then display something more like:

def wait(text, frame, number_of_frames):
    time.sleep(.05)
    current_frame = frame
    number_of_frames = number_of_files_in_dir - 1
    percentage = round(current_frame / number_of_frames, 1)
    update_status_bar(main_window, text + ' ' + percentage + '% ' + Completed)

With some math to calculate an ETA for completion as well.

So, is there a way to grab that output text from the ffmpeg and turn just that frame= number into a variable?

Upvotes: 0

Views: 745

Answers (1)

chsws
chsws

Reputation: 443

You can try to replicate this method by constructing your ffmpeg command outside of the ffmpeg package and running the process directly. You can replace the DUR_REGEX regex with one that captures the frame instead of the duration.

If you have no other need for the frame, you could leave it as it is. It already yields progress in percent.

Link courtesy of slhck here.

Upvotes: 2

Related Questions