Spaces:
Sleeping
Sleeping
| 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)) | |