컴퓨터/파이썬 (Python)

[Python] wav 파일 자르기

COMKONG 2023. 3. 21. 22:36
반응형
import wave

# 원하는 시간 단위 (초)
start = 1 # 시작 시간, seconds
end = 300 # 끝나는 시간, seconds

# file to extract the snippet from
with wave.open('test.wav', "rb") as infile:
    # get file data
    nchannels = infile.getnchannels()
    sampwidth = infile.getsampwidth()
    framerate = infile.getframerate()
    # set position in wave to start of segment
    infile.setpos(int(start * framerate))
    # extract data
    data = infile.readframes(int((end - start) * framerate))

# write the extracted data to a new file
with wave.open('output.wav', 'w') as outfile:
    outfile.setnchannels(nchannels)
    outfile.setsampwidth(sampwidth)
    outfile.setframerate(framerate)
    outfile.setnframes(int(len(data) / sampwidth))
    outfile.writeframes(data)

wav 파일의 원하는 시간 위치로 자를 수 있는 코드

 

반응형