Skip to content

Instantly share code, notes, and snippets.

@trueroad
Last active January 21, 2024 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueroad/f6bb538f0cf5a99880ae3cd4c4272d94 to your computer and use it in GitHub Desktop.
Save trueroad/f6bb538f0cf5a99880ae3cd4c4272d94 to your computer and use it in GitHub Desktop.
Show zipped sound_file_parameters.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Show zipped sound_file_parameters.
https://gist.github.com/trueroad/
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.
複数の ZIP された FLAC や WAV を入力として、
サンプリングレート、フレーム数、チャンネル数、秒数と
ファイル名をタブ区切りで出力する。
"""
from pathlib import Path
import sys
from typing import Final, IO
import zipfile
import soundfile as sf # type: ignore[import-untyped]
EXTENSIONS: Final[list[str]] = ['.flac', '.wav']
def main() -> None:
"""Do main."""
# コマンドラインで指定された ZIP ファイル名でループ
zip_filename: str
for zip_filename in sys.argv[1:]:
sys.stdout.flush()
print(f'*** {zip_filename} ***', file=sys.stderr, flush=True)
# ZIP ファイル
zf: zipfile.ZipFile
with zipfile.ZipFile(zip_filename, 'r') as zf:
# ZIP ファイル内の FLAC や WAV ファイル名
sound_filename: str
for sound_filename in zf.namelist():
if Path(sound_filename).suffix.lower() not in EXTENSIONS:
# 拡張子が指定のものではないのでスキップ
continue
# サウンドファイルを開く
zsound: IO[bytes]
with zf.open(sound_filename, 'r') as zsound:
# サウンドファイルを読み込む
zsf: sf.SoundFile
with sf.SoundFile(zsound) as zsf:
# 出力
print(f'{zsf.samplerate}\t{zsf.frames}\t'
f'{zsf.channels}\t'
f'{float(zsf.frames) / zsf.samplerate}\t'
f'{sound_filename}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment