본문 바로가기

카테고리 없음

[python] soundfile로 음성파일 duration 구하기

 

import soundfile as sf

def get_wav_duration(file_path):
    try:
        # WAV 파일 열기
        audio_data, samplerate = sf.read(file_path)

        # 오디오 데이터의 길이를 초 단위로 계산
        duration = len(audio_data) / float(samplerate)
        return duration

    except Exception as e:
        print(f"오류 발생: {e}")
        return None

# WAV 파일 경로 지정
wav_file_path = 'test.wav'

# 함수 호출하여 WAV 파일의 총 재생 시간 가져오기
wav_duration = get_wav_duration(wav_file_path)

if wav_duration:
    print(f"WAV 파일의 총 재생 시간: {wav_duration} 초")