반응형
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 파일의 원하는 시간 위치로 자를 수 있는 코드
반응형
'컴퓨터 > 파이썬 (Python)' 카테고리의 다른 글
[Python] 문자열 특정 구간 자르기 (0) | 2023.06.08 |
---|---|
[Python] 폴더 내 파일 명 추출해서 txt 파일로 저장하기 (0) | 2023.05.17 |
[Python] wav 파일 Speech-to-Text 파이썬 코드 (0) | 2023.03.21 |
[Python] image to csv (0) | 2022.05.18 |
[Python] 이미지 흑백 변환 (0) | 2022.05.18 |