diff --git a/docs/changelog.rst b/docs/changelog.rst index 7b6ed64..8db1602 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,14 @@ Changelog Upcoming -------- +v0.16.2 +------- + +- Add ``raise_on_unsupported_wav`` parameter to ``MediaFile.__init__`` to + optionally raise ``FileTypeError`` for WAV files containing non-PCM audio + streams (``WAVE_FORMAT_MPEGLAYER3``, ``WAVE_FORMAT_ADPCM``, + ``WAVE_FORMAT_ALAW``, ``WAVE_FORMAT_MULAW``). + v0.16.1 ------- diff --git a/mediafile/__init__.py b/mediafile/__init__.py index fb90e11..5d1bfcc 100644 --- a/mediafile/__init__.py +++ b/mediafile/__init__.py @@ -142,7 +142,7 @@ class MediaFile: """ @loadfile() - def __init__(self, filething, id3v23=False): + def __init__(self, filething, id3v23=False, raise_on_unsupported_wav=False): """Constructs a new `MediaFile` reflecting the provided file. `filething` can be a path to a file (i.e., a string) or a @@ -152,6 +152,9 @@ def __init__(self, filething, id3v23=False): By default, MP3 files are saved with ID3v2.4 tags. You can use the older ID3v2.3 standard by specifying the `id3v23` option. + + If `raise_on_unsupported_wav` is True, a `FileTypeError` is raised for WAV + files containing non-PCM audio streams that cannot be tagged correctly. """ self.filething = filething @@ -196,7 +199,7 @@ def __init__(self, filething, id3v23=False): 0x0055: "WAVE_FORMAT_MPEGLAYER3", } audio_fmt = getattr(self.mgfile.info, "audio_format", 0x0001) - if audio_fmt in _unsupported_wav_formats: + if raise_on_unsupported_wav and audio_fmt in _unsupported_wav_formats: raise FileTypeError(self.filename, _unsupported_wav_formats[audio_fmt]) self.type = "wav" else: diff --git a/test/test_mediafile_edge.py b/test/test_mediafile_edge.py index 20c6a67..044e14b 100644 --- a/test/test_mediafile_edge.py +++ b/test/test_mediafile_edge.py @@ -179,7 +179,7 @@ def test_mpeglayer3_wav_raises_filetypeerror(self): # and cannot be tagged correctly fn = os.path.join(_common.RSRC, b"mpeglayer3.wav") with pytest.raises(mediafile.FileTypeError): - mediafile.MediaFile(fn) + mediafile.MediaFile(fn, raise_on_unsupported_wav=True) class SideEffectsTest(unittest.TestCase):