Skip to content

Instantly share code, notes, and snippets.

@0xquad
Last active March 18, 2017 04:52
Show Gist options
  • Save 0xquad/a22c8a7a6223b551ab326f03f6894a21 to your computer and use it in GitHub Desktop.
Save 0xquad/a22c8a7a6223b551ab326f03f6894a21 to your computer and use it in GitHub Desktop.
Parse ffprobe output and print results in predictable format
#!/usr/bin/env python3
#
# Parse ffprobe output and print results in a predictable format
# for other tools to use.
#
# Copyright (c) 2017, Alexandre Hamelin <alexandre.hamelin gmail.com>
import sys
import re
import subprocess
class MediaFile:
def __init__(self):
self.chapters = []
class Chapter:
def __init__(self):
self.id = None
self.start = 0
self.end = 0
self.title = None
if __name__ == '__main__':
mediafile = sys.argv[1]
output = subprocess.getoutput('ffmpeg -i {}'.format(mediafile))
for line in output.splitlines():
line = line.strip()
if line.startswith('Input #'):
pass
elif line.startswith('Duration:'):
pass
elif line.startswith('Chapter #'):
try:
id, start, end = re.search(
r'#(\d+:\d+): start (\S+), end (\S+)$', line).groups()
except AttributeError:
print('Invalid Chapter line: {}'.format(line)
raise SystemExit(1)
ch = Chapter()
ch.id = id
ch.start = float(start)
ch.end = float(end)
media.chapters.append(ch)
elif line.startswith('title '):
title = re.search(r'title\s+: (.*)', line).group(1)
media.chapters[-1].title = title
elif line.startswith('Stream #'):
stream_info = re.search(
r'#(\d+:\d+)(?:\((...)\))?: (\S+): (\S+) (.*)', line).groups()
meta major_brand mp42
meta minor_version 512
chapter 0:0 start=0 end=20 title=TITLE-0-0
chapter 0:1 start=20 end=112.64 title=TITLE-0-1
stream 0:0 v c:h264~yuv420p~640x352~699kb/s~25fps language=eng default
stream 0:1 a c:aac~44100hz~stereo~131kb/s language=eng default
stream 0:2 v c:mjpeg~yuvj520p~405x600
stream 0:3 a c:aac~44100hz~stereo~122kb/s language=eng
stream 0:4 s c:mov_text~640x52 language=eng default
stream 0:5 s c:mov_text~640x52 language=fra default
stream 0:6 v c:mjpeg~yuvj420p~640x352~1kb/s~0.0036fps language=eng
stream 0:7 d c:bin_data~text language=eng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment