PyDub로 여러 오디오 파일 다루기
PyDub로 단일 파일을 변환하는 방법은 살펴봤어요. 그렇다면 서로 다른 파일 형식이 섞여 있는 폴더가 있다면 어떻게 할까요?
이 연습 문제에서는 good-afternoon 오디오 파일의 .mp3, .m4a, .aac 버전을 담은 folder를 준비해 두었습니다.
각 파일을 PyDub로 열고 .wav 형식으로 내보내서 음성 인식 API와 호환되도록 만들어 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 음성 언어 처리
연습 안내
audio_file을from_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}...")