mirror of
https://github.com/iluvcapra/wavinfo.git
synced 2025-12-31 17:00:41 +00:00
Make it possible to pass file handles or in memory wav data to wavinfo. Some of the fields for the __repr__ where not available for files. For these the url member is set to "about:blank", and the self.path to the representation of the incoming object. The formating in __repr__ turned out to be broken, with fixing that we also had to fix the test on __repr__.
This commit is contained in:
@@ -12,7 +12,7 @@ class TestWaveInfo(TestCase):
|
|||||||
def test_sanity(self):
|
def test_sanity(self):
|
||||||
for wav_file in all_files():
|
for wav_file in all_files():
|
||||||
info = wavinfo.WavInfoReader(wav_file)
|
info = wavinfo.WavInfoReader(wav_file)
|
||||||
self.assertEqual(info.__repr__(), 'WavInfoReader(%s, %s, %s)'.format(wav_file, 'latin_1', 'ascii'))
|
self.assertEqual(info.__repr__(), 'WavInfoReader({}, latin_1, ascii)'.format(os.path.abspath(wav_file)))
|
||||||
self.assertIsNotNone(info)
|
self.assertIsNotNone(info)
|
||||||
|
|
||||||
def test_fmt_against_ffprobe(self):
|
def test_fmt_against_ffprobe(self):
|
||||||
|
|||||||
@@ -27,43 +27,57 @@ class WavInfoReader:
|
|||||||
"""
|
"""
|
||||||
Create a new reader object.
|
Create a new reader object.
|
||||||
|
|
||||||
:param path: A filesystem path to the wav file you wish to probe.
|
:param path:
|
||||||
|
A filesystem path to the wav file you wish to probe or a
|
||||||
|
file handle to an open file.
|
||||||
|
|
||||||
:param info_encoding: The text encoding of the INFO metadata fields.
|
:param info_encoding:
|
||||||
latin_1/Win CP1252 has always been a pretty good guess for this.
|
The text encoding of the INFO metadata fields.
|
||||||
|
latin_1/Win CP1252 has always been a pretty good guess for this.
|
||||||
|
|
||||||
:param bext_encoding: The text encoding to use when decoding the string
|
:param bext_encoding:
|
||||||
fields of the Broadcast-WAV extension. Per EBU 3285 this is ASCII
|
The text encoding to use when decoding the string
|
||||||
but this parameter is available to you if you encounter a weirdo.
|
fields of the Broadcast-WAV extension. Per EBU 3285 this is ASCII
|
||||||
|
but this parameter is available to you if you encounter a weirdo.
|
||||||
"""
|
"""
|
||||||
absolute_path = os.path.abspath(path)
|
|
||||||
|
|
||||||
#: `file://` url for the file.
|
|
||||||
self.url = pathlib.Path(absolute_path).as_uri()
|
|
||||||
|
|
||||||
# for __repr__()
|
|
||||||
self.path = absolute_path
|
|
||||||
self.info_encoding = info_encoding
|
self.info_encoding = info_encoding
|
||||||
self.bext_encoding = bext_encoding
|
self.bext_encoding = bext_encoding
|
||||||
|
|
||||||
with open(path, 'rb') as f:
|
if hasattr(path, 'read'):
|
||||||
chunks = parse_chunk(f)
|
self.get_wav_info(path)
|
||||||
|
self.url = 'about:blank'
|
||||||
|
self.path = repr(path)
|
||||||
|
else:
|
||||||
|
absolute_path = os.path.abspath(path)
|
||||||
|
|
||||||
self.main_list = chunks.children
|
#: `file://` url for the file.
|
||||||
f.seek(0)
|
self.url = pathlib.Path(absolute_path).as_uri()
|
||||||
|
|
||||||
#: :class:`wavinfo.wave_reader.WavAudioFormat`
|
# for __repr__()
|
||||||
self.fmt = self._get_format(f)
|
self.path = absolute_path
|
||||||
|
|
||||||
#: :class:`wavinfo.wave_bext_reader.WavBextReader` with Broadcast-WAV metadata
|
with open(path, 'rb') as f:
|
||||||
self.bext = self._get_bext(f, encoding=bext_encoding)
|
self.get_wav_info(f)
|
||||||
|
|
||||||
#: :class:`wavinfo.wave_ixml_reader.WavIXMLFormat` with iXML metadata
|
def get_wav_info(self, wavfile):
|
||||||
self.ixml = self._get_ixml(f)
|
chunks = parse_chunk(wavfile)
|
||||||
|
|
||||||
#: :class:`wavinfo.wave_info_reader.WavInfoChunkReader` with RIFF INFO metadata
|
self.main_list = chunks.children
|
||||||
self.info = self._get_info(f, encoding=info_encoding)
|
wavfile.seek(0)
|
||||||
self.data = self._describe_data()
|
|
||||||
|
#: :class:`wavinfo.wave_reader.WavAudioFormat`
|
||||||
|
self.fmt = self._get_format(wavfile)
|
||||||
|
|
||||||
|
#: :class:`wavinfo.wave_bext_reader.WavBextReader` with Broadcast-WAV metadata
|
||||||
|
self.bext = self._get_bext(wavfile, encoding=self.bext_encoding)
|
||||||
|
|
||||||
|
#: :class:`wavinfo.wave_ixml_reader.WavIXMLFormat` with iXML metadata
|
||||||
|
self.ixml = self._get_ixml(wavfile)
|
||||||
|
|
||||||
|
#: :class:`wavinfo.wave_info_reader.WavInfoChunkReader` with RIFF INFO metadata
|
||||||
|
self.info = self._get_info(wavfile, encoding=self.info_encoding)
|
||||||
|
self.data = self._describe_data()
|
||||||
|
|
||||||
def _find_chunk_data(self, ident, from_stream, default_none=False):
|
def _find_chunk_data(self, ident, from_stream, default_none=False):
|
||||||
top_chunks = (chunk for chunk in self.main_list if type(chunk) is ChunkDescriptor and chunk.ident == ident)
|
top_chunks = (chunk for chunk in self.main_list if type(chunk) is ChunkDescriptor and chunk.ident == ident)
|
||||||
@@ -146,4 +160,4 @@ class WavInfoReader:
|
|||||||
yield 'info', key, info_dict[key]
|
yield 'info', key, info_dict[key]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'WavInfoReader(%s, %s, %s)'.format(self.path, self.info_encoding, self.bext_encoding)
|
return 'WavInfoReader({}, {}, {})'.format(self.path, self.info_encoding, self.bext_encoding)
|
||||||
|
|||||||
Reference in New Issue
Block a user