[Sox] 오디오 파일의 Sample Rate 변환

반응형

Sox를 사용하여 음성 파일의 Sampling rate를 16000, channel을 1로 변환한다.

 

https://sox.sourceforge.net/

 

SoX - Sound eXchange | HomePage

Welcome Welcome to the home of SoX, the Swiss Army knife of sound processing programs. SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also ap

sox.sourceforge.net

 

Bash

for i in *.mp3; do sox "$i" -r 16k -c 1 "$(basename -s .mp3 "$i").wav"; done;

한 폴더에 있는 여러 mp3 파일을 sample rate 16000, channel 1인 .wav 파일로 변경하는 커맨드 라인이다. 

 

python

import os
import subprocess
import re

# Set path to the directory where you want the converted files to be saved
output_dir = './wav/converted'

# Make sure output directory exists
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# Loop over each wav in the tsv file
for wav in open("wavs.tsv"):
    # Check if the file is a .wav file
    if wav.endswith('.wav'):
        # Get the name of the file without the path and extension
        wav_name = os.path.splitext(os.path.basename(wav))[0]
        # Set the output file path
        output_file = os.path.join(output_dir, f"{wav_name}.wav")
        # Run the SoX command using subprocess
        subprocess.run(['sox', wav, '-r', '16000', '-c', '1', output_file], check=True)

wavs.tsv에 한 줄에 wav의 경로가 있다고 했을 때 loop을 돌면서 converted 폴더에 변환된 wav들을 담는 코드이다.

 

반응형