-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_audio_info.py
70 lines (59 loc) · 2.22 KB
/
get_audio_info.py
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
import os
import json
import subprocess
from pathlib import Path
def get_audio_duration(file_path):
"""Get audio duration using ffprobe"""
try:
result = subprocess.run([
'ffprobe',
'-v', 'quiet',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
file_path
], capture_output=True, text=True)
duration = float(result.stdout.strip())
return int(duration) # Round down to nearest second
except Exception as e:
print(f"Error getting duration for {file_path}: {e}")
return None
def main():
# Get the directory where this script is located
script_dir = Path(os.path.dirname(os.path.abspath(__file__)))
audio_dir = script_dir / 'audios'
# Check if ffprobe is installed
try:
subprocess.run(['ffprobe', '-version'], capture_output=True)
except FileNotFoundError:
print("ffprobe not found. Please install ffmpeg:")
print("brew install ffmpeg")
return
# Check if audios directory exists
if not audio_dir.exists():
print(f"Audio directory not found at: {audio_dir}")
return
# Get all audio files
audio_files = []
supported_extensions = {'.mp3', '.wav', '.m4a', '.aac', '.ogg'}
for file in audio_dir.iterdir():
if file.suffix.lower() in supported_extensions:
duration = get_audio_duration(str(file))
if duration is not None:
audio_files.append({
"name": file.name,
"duration": duration
})
# Sort by name
audio_files.sort(key=lambda x: x['name'])
# Print formatted Python code
print("\nCopy this into offline_audio_slave.py:")
print("\nself.audio_files = [")
for audio in audio_files:
print(f" {{\"name\": \"{audio['name']}\", \"duration\": {audio['duration']}}}, # {audio['duration']} seconds")
print("]")
# Also save as JSON
with open(audio_dir / 'audio_info.json', 'w') as f:
json.dump(audio_files, f, indent=4)
print(f"\nAudio information also saved to: {audio_dir}/audio_info.json")
if __name__ == "__main__":
main()