PyDub で複数の音声ファイルを操作する
PyDub を使って単一ファイルを変換する方法は見てきましたが、異なるファイル形式が混在するフォルダがあったらどうすればよいでしょうか?
この演習では、good-afternoon の音声ファイルについて、.mp3、.m4a、.aac の各バージョンを含む folder を用意しました。
各ファイルを PyDub で開き、音声認識 API と互換性のある .wav 形式としてエクスポートしていきます。
この演習はコースの一部です
Pythonで学ぶ音声言語処理
演習の手順
from_file()関数にaudio_fileを渡します。export()を使って、wav_filenameを".wav"形式でエクスポートします。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Loop through the files in the folder
for audio_file in folder:
# Create the new .wav filename
wav_filename = os.path.splitext(os.path.basename(audio_file))[0] + ".wav"
# Read audio_file and export it in wav format
AudioSegment.from_file(____).____(out_f=wav_filename,
format=____)
print(f"Creating {wav_filename}...")