#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Check parameters of all zipped FLAC files as expected. https://gist.github.com/trueroad/adc36da36aeb1217e7acf63d98c448a2 Copyright (C) 2024 Masamichi Hosoda. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from pathlib import Path import sys from typing import Final, IO, Optional import zipfile import soundfile as sf # type: ignore[import-untyped] EXPECTED_PARAMETER_SAMPLERATE: Final[list[int]] = [48000, 44100] EXPECTED_PARAMETER_CHANNELS: Final[list[int]] = [1] EXPECTED_PARAMETER_FRAMES_MIN: Final[Optional[int]] = 1 EXPECTED_PARAMETER_FRAMES_MAX: Final[Optional[int]] = None def main() -> None: """Do main.""" zip_filename: str for zip_filename in sys.argv[1:]: print(f'{zip_filename}') zf: zipfile.ZipFile with zipfile.ZipFile(zip_filename, 'r') as zf: contents_filename: str for contents_filename in zf.namelist(): if Path(contents_filename).suffix.lower() != '.flac': continue zef: IO[bytes] with zf.open(contents_filename, 'r') as zef: zsf: sf.SoundFile with sf.SoundFile(zef) as zsf: if zsf.samplerate not in \ EXPECTED_PARAMETER_SAMPLERATE: print(f'Unexpected: {contents_filename}: ' f'samplerate = {zsf.samplerate}') if zsf.channels not in \ EXPECTED_PARAMETER_CHANNELS: print(f'Unexpected: {contents_filename}: ' f'channels = {zsf.channels}') ex_min: Optional[int] = \ EXPECTED_PARAMETER_FRAMES_MIN ex_max: Optional[int] = \ EXPECTED_PARAMETER_FRAMES_MAX if ((ex_min is not None and zsf.frames < ex_min) or (ex_max is not None and zsf.frames > ex_max)): print(f'Unexpected: {contents_filename}: ' f'nframes = {zsf.frames}') if __name__ == '__main__': main()