36 lines
877 B
Python
36 lines
877 B
Python
import sys
|
|
from mutagen import File
|
|
import json
|
|
|
|
def print_metadata(file_path):
|
|
try:
|
|
audio = File(file_path)
|
|
meta = {}
|
|
|
|
if audio is None:
|
|
print(json.dumps({"error": "unsupported file"}))
|
|
return
|
|
|
|
for key, value in audio.items():
|
|
if type(value) is list and len(value) == 1:
|
|
value = value[0]
|
|
meta[key] = value
|
|
|
|
if audio.info:
|
|
meta["info"] = {}
|
|
for key, value in vars(audio.info).items():
|
|
meta["info"][key] = value
|
|
|
|
print(json.dumps(meta))
|
|
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e)}))
|
|
exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python script.py <path_to_audio_file>")
|
|
sys.exit(1)
|
|
|
|
file_path = sys.argv[1]
|
|
print_metadata(file_path)
|