Skip to content

Instantly share code, notes, and snippets.

@Krowemoh
Last active September 25, 2022 20:09
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 Krowemoh/552da533dd76393e4e0253ab798a3b73 to your computer and use it in GitHub Desktop.
Save Krowemoh/552da533dd76393e4e0253ab798a3b73 to your computer and use it in GitHub Desktop.
Get the video and audio codec information for a file
#!/usr/bin/env python3
import os
import subprocess
import sys
if len(sys.argv) < 2:
print("Incorrect number of arguments.")
sys.exit(1)
if not os.path.exists(sys.argv[1]):
print("File does not exist.")
sys.exit(1)
filename = sys.argv[1]
extension = os.path.splitext(filename)[1]
dev_null = open('/dev/null', 'w')
result = subprocess.run(["ffprobe", "-show_streams", filename], stdout=subprocess.PIPE, stderr=dev_null)
output = result.stdout.decode("utf8")
output = output.split("\n")
streams = []
stream = {}
for line in output:
line = line.strip()
if "[STREAM]" in line or "[/STREAM]" in line:
if stream != {}:
streams.append(stream)
stream = {}
if "=" in line:
k, v = line.split("=")
stream[k] = v
print("Ext : " + extension)
print("Video: " + streams[0]["codec_name"])
print("Audio: " + streams[1]["codec_name"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment