Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
Upcoming
--------

- Add ``raise_on_unsupported_wav`` parameter to ``MediaFile.__init__`` to
Comment thread
elainec2024 marked this conversation as resolved.
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
-------

Expand Down
7 changes: 5 additions & 2 deletions mediafile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 audio_fmt in _unsupported_wav_formats and raise_on_unsupported_wav:
Comment thread
elainec2024 marked this conversation as resolved.
Outdated
raise FileTypeError(self.filename, _unsupported_wav_formats[audio_fmt])
self.type = "wav"
else:
Expand Down
2 changes: 1 addition & 1 deletion test/test_mediafile_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading