Skip to content

Instantly share code, notes, and snippets.

@adriankeenan
Last active December 12, 2020 22:49
Show Gist options
  • Save adriankeenan/6469378 to your computer and use it in GitHub Desktop.
Save adriankeenan/6469378 to your computer and use it in GitHub Desktop.
Attain FFMPEG progress via regex
#store track duration in seconds
duration_flt = -1
#read each line of output from FFMPEG popen object
for line in output:
#no duration yet
if duration_flt == -1:
#get duration via regex match
dur_patern = re.compile("Duration: [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2}")
dur_matches = dur_patern.findall(line)
if duration_flt == -1 and len(dur_matches) > 0:
#store duration as seconds
duration_flt = str_to_secs(dur_matches[0])
#duration found, get current time position of encode
else:
#get current time via regex match
time_pattern = re.compile("time=[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2}")
time_matches = time_pattern.findall(line)
if len(time_matches) > 0:
#store current encode point as seconds
last_time_match = str_to_secs(time_matches[0])
#get percentage complete
progress = 100 * (float(last_time_match)/duration_flt)
def str_to_secs(input):
text = re.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2}").findall(input)[-1]
blocks = re.compile("[0-9]{2}").findall(text)
h = int(blocks[0])
m = int(blocks[1])
s = int(blocks[2])
ms = int(blocks[3])
return h*60*60 + m*60 + s + ms*0.1
@adminy
Copy link

adminy commented Dec 12, 2020

I have done similar work, made it in a js module here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment