Skip to content

Instantly share code, notes, and snippets.

@casebeer
Last active November 13, 2023 23:03
Show Gist options
  • Save casebeer/bd14b50bcc9e0f13a63700a505d0737c to your computer and use it in GitHub Desktop.
Save casebeer/bd14b50bcc9e0f13a63700a505d0737c to your computer and use it in GitHub Desktop.
Parse ffprobe keyframe output
'''
# Parse ffprobe keyframe output
Run with
ffprobe -loglevel error -show_entries packet=pts_time,flags -of csv=print_section=0 -i <input file or stream> | python kf.py
See https://stackoverflow.com/a/18088156
'''
import sys
def delta_ms(timestamps):
it = iter(timestamps)
previous = next(it)
for timestamp in it:
yield int(1000 * (timestamp - previous)) # milliseconds
previous = timestamp
def parse_keyframes(lines):
for line in (raw.strip() for raw in lines):
if len(line) == 0:
continue
#print(f"line ({len(line)}): {line}")
ts, flags = line.split(',')
if 'K' in flags:
# confirmed it's a keyframe
yield float(ts) # seconds
def main():
for delta in delta_ms(parse_keyframes(sys.stdin)):
print(f"{delta} ms")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment