Last active
September 25, 2022 20:09
-
-
Save Krowemoh/552da533dd76393e4e0253ab798a3b73 to your computer and use it in GitHub Desktop.
Get the video and audio codec information for a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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