-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMetadata.py
68 lines (59 loc) · 2.33 KB
/
Metadata.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
import os, re, subprocess
from soundfile import SoundFile
import scikits.audiolab
_find_sampling_rate = re.compile('.* ([0-9:]+) Hz,', re.MULTILINE )
_find_channels = re.compile(".*Hz,( .*?),", re.MULTILINE)
_find_duration = re.compile('.*Duration: ([0-9:]+)', re.MULTILINE )
def timestamp_to_seconds( ms ):
"Convert a hours:minutes:seconds string representation to the appropriate time in seconds."
a = ms.split(':')
assert 3 == len( a )
return float(a[0]) * 3600 + float(a[1]) * 60 + float(a[2])
def seconds_to_min_sec( secs ):
"Return a minutes:seconds string representation of the given number of seconds."
mins = int(secs) / 60
secs = int(secs - (mins * 60))
return "%d:%02d" % (mins, secs)
def get_mp3_metadata(audio_path):
"Determine length of tracks listed in the given input files (e.g. playlists)."
ffmpeg = subprocess.check_output(
'ffmpeg -i "%s"; exit 0' % audio_path,
shell = True,
stderr = subprocess.STDOUT )
# Get sampling rate
match = _find_sampling_rate.search( ffmpeg )
assert(match)
sampling_rate = int(match.group( 1 ))
# Get channels
match = _find_channels.search( ffmpeg )
assert(match)
channels = match.group( 1 )
channels = (2 if channels.__contains__("stereo") else 1)
# Get duration
match = _find_duration.search( ffmpeg )
assert(match)
duration = match.group( 1 )
duration = timestamp_to_seconds(duration)
return sampling_rate, channels, duration
def get_audio_metadata(audioPath, sphereType=False):
'''
Returns sampling rate, number of channels and duration of an audio file
:param audioPath:
:param sphereType:
:return:
'''
ext = os.path.splitext(audioPath)[1][1:].lower()
if ext=="aiff" or sphereType: # SPHERE headers for the TIMIT dataset
audio = scikits.audiolab.Sndfile(audioPath)
sr = audio.samplerate
channels = audio.channels
duration = float(audio.nframes) / float(audio.samplerate)
elif ext=="mp3": # Use ffmpeg/ffprobe
sr, channels, duration = get_mp3_metadata(audioPath)
else:
snd_file = SoundFile(audioPath, mode='r')
inf = snd_file._info
sr = inf.samplerate
channels = inf.channels
duration = float(inf.frames) / float(inf.samplerate)
return int(sr), int(channels), float(duration)