Spaces:
Sleeping
Sleeping
File size: 1,569 Bytes
ea97e04 e3bdc52 ea97e04 e3bdc52 ea97e04 e3bdc52 ea97e04 e3bdc52 | 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 | import librosa
import os
import sys
import json
from datetime import datetime
def extract_metadata(file_path):
"""
Extrai metadados reais de um arquivo de áudio usando librosa.
"""
try:
# Carrega apenas os metadados (duration) sem ler todo o áudio se possível
# librosa.get_duration é eficiente
duration = librosa.get_duration(path=file_path)
# Para taxa de amostragem e canais, carregamos um pequeno trecho
y, sr = librosa.load(file_path, sr=None, duration=0.1)
channels = 1 if len(y.shape) == 1 else y.shape[0]
# Informações do arquivo
file_stats = os.stat(file_path)
creation_time = datetime.fromtimestamp(file_stats.st_ctime).strftime('%Y-%m-%d %H:%M:%S')
file_format = os.path.splitext(file_path)[1].replace('.', '').upper()
metadata = {
"format": file_format,
"sample_rate": sr,
"channels": channels,
"duration_seconds": round(duration, 2),
"encoder": "Librosa Forensic Parser",
"creation_time": creation_time
}
return metadata
except Exception as e:
print(f"Erro ao extrair metadados: {e}")
return {
"format": "Unknown",
"error": str(e)
}
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Uso: python metadata_extractor.py <path_to_audio>")
sys.exit(1)
path = sys.argv[1]
meta = extract_metadata(path)
print(json.dumps(meta, indent=2))
|