-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.py
152 lines (116 loc) · 2.92 KB
/
api.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
__all__ = [
'determine_format',
'load',
'loads',
]
import os
from io import (
BufferedReader,
FileIO,
)
from tbm_utils import DataReader
from .exceptions import (
FormatError,
UnsupportedFormat,
)
from .formats import (
FLAC,
MP3,
WAVE,
ID3v2,
MP3StreamInfo,
OggOpus,
OggVorbis,
)
def determine_format(data):
"""Determine the format of a filepath, file-like object, or bytes-like object.
Parameters:
data (bytes-like object, str, os.PathLike, or file-like object):
A bytes-like object, filepath, path-like object
or file-like object of an audio file.
Returns:
Format: An appropriate audio format class if supported, else None.
"""
# Only convert if not already a DataReader.
# Otherwise ``find_mpeg_frames`` caching won't work.
if not isinstance(data, DataReader):
try:
data = DataReader(data)
except AttributeError:
return None
data.seek(0, os.SEEK_SET)
d = data.peek(36)
if (
d.startswith(b'OggS')
and b'OpusHead' in d
):
return OggOpus
if (
d.startswith(b'OggS')
and b'\x01vorbis' in d
):
return OggVorbis
if d.startswith(b'fLaC'):
return FLAC
if d.startswith(b'RIFF'):
return WAVE
if d.startswith(b'ID3'):
ID3v2.parse(data)
if data.peek(4) == b'fLaC':
return FLAC
try:
MP3StreamInfo.find_mpeg_frames(data)
except FormatError:
return None
else:
return MP3
def load(f):
"""Load audio metadata from a filepath or file-like object.
Parameters:
f (str, os.PathLike, or file-like object):
A filepath, path-like object or file-like object of an audio file.
Returns:
Format: An audio format object of the appropriate type.
Raises:
FormatError: If the audio file is not valid.
UnsupportedFormat: If the audio file is not of a supported format.
ValueError: If ``f`` is not a valid str, path-like object,
file-like object, or is unreadable.
"""
if (
not isinstance(f, (os.PathLike, str))
and not (
isinstance(f, BufferedReader)
and isinstance(f.raw, FileIO)
)
):
raise ValueError("Not a valid filepath or file-like object.")
data = DataReader(f)
parser_cls = determine_format(data)
if parser_cls is None:
raise UnsupportedFormat("Supported format signature not found.")
else:
data.seek(0, os.SEEK_SET)
return parser_cls.parse(data)
def loads(b):
"""Load audio metadata from a bytes-like object.
Parameters:
b (bytes-like object): A bytes-like object of an audio file.
Returns:
Format: An audio format object of the appropriate type.
Raises:
FormatError: If the audio file is not valid.
UnsupportedFormat: If the audio file is not of a supported format.
ValueError: If ``b`` is not a valid bytes-like object.
"""
try:
memoryview(b)
except TypeError:
raise ValueError("Not a valid bytes-like object.")
data = DataReader(b)
parser_cls = determine_format(data)
if parser_cls is None:
raise UnsupportedFormat("Supported format signature not found.")
else:
data.seek(0, os.SEEK_SET)
return parser_cls.parse(data)