-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecinfo
More file actions
73 lines (61 loc) · 2.31 KB
/
codecinfo
File metadata and controls
73 lines (61 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
codecinfo: Quickly display codec and stream info for the first media file in a folder.
This script scans the current directory for the first .mkv or .mp4 file (alphabetically),
then uses ffprobe to print a concise summary of each track:
- For video: codec long name and resolution
- For audio: codec and number of channels
- For subtitles: codec (e.g., srt, ass, etc.)
Usage:
python3 codecinfo
Requirements:
- ffprobe (part of ffmpeg) must be installed and in your PATH.
Example output:
Showing codec info for: MyMovie.mkv
Video #0: H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (1920x1080)
Audio #1: aac (2ch)
Subtitle #2: srt
"""
import os
import sys
import subprocess
import json
from pathlib import Path
VIDEO_EXTS = ('.mkv', '.mp4')
def find_first_media_file():
for f in sorted(os.listdir()):
if f.lower().endswith(VIDEO_EXTS) and Path(f).is_file():
return f
return None
def show_codec_info(filename):
print(f"Showing codec info for: {filename}\n")
try:
result = subprocess.run([
"ffprobe", "-v", "error", "-show_streams", "-of", "json", filename
], capture_output=True, text=True, check=True)
data = json.loads(result.stdout)
for stream in data.get("streams", []):
idx = stream.get("index")
codec = stream.get("codec_name", "unknown")
if stream.get("codec_type") == "video":
codec_long = stream.get("codec_long_name", codec)
width = stream.get("width", "?")
height = stream.get("height", "?")
print(f"Video #{idx}: {codec_long} ({width}x{height})")
elif stream.get("codec_type") == "audio":
channels = stream.get("channels", "?")
print(f"Audio #{idx}: {codec} ({channels}ch)")
elif stream.get("codec_type") == "subtitle":
print(f"Subtitle #{idx}: {codec}")
except FileNotFoundError:
print("ffprobe not found. Please install ffmpeg.")
except subprocess.CalledProcessError:
print("Error running ffprobe.")
def main():
file = find_first_media_file()
if not file:
print("No media file (.mp4 or .mkv) found in this directory.")
sys.exit(1)
show_codec_info(file)
if __name__ == "__main__":
main()