Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created December 2, 2023 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapmanjacobd/e8282dbc1d54a49950b1faeab645f9ae to your computer and use it in GitHub Desktop.
Save chapmanjacobd/e8282dbc1d54a49950b1faeab645f9ae to your computer and use it in GitHub Desktop.
class FFProbe:
def __init__(self, path, *args):
args = ["ffprobe", "-show_format", "-show_streams", "-show_chapters", "-of", "json", *args, path]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
raise RuntimeError(out, err)
d = json.loads(out.decode("utf-8"))
self.streams = d.get("streams")
self.chapters = d.get("chapters")
self.format = d.get("format")
self.video_streams = []
self.audio_streams = []
self.subtitle_streams = []
self.other_streams = []
for stream in self.streams:
if stream["codec_type"] == "video":
self.video_streams.append(stream)
elif stream["codec_type"] == "audio":
self.audio_streams.append(stream)
elif stream["codec_type"] == "subtitle":
self.subtitle_streams.append(stream)
else:
self.other_streams.append(stream)
self.has_video = len(self.video_streams) > 0
self.has_audio = len(self.audio_streams) > 0
self.duration = None
try:
self.duration = float(self.format["duration"])
except Exception:
pass
try:
self.duration = float(self.video_streams[0]["duration"])
except Exception:
pass
try:
self.duration = float(self.audio_streams[0]["duration"])
except Exception:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment