[파이썬, Python] Wave 파일을 PCM 파일로 바꿔 저장하는 예제 2.


예제 1에서 약간의 upgrade...

파일명으로 .wav를 입력할 경우, 

현재 폴더의 모든 .wav 파일에 대해서 .pcm 파일을 생성하는 예제.


#!/usr/bin/python

import sys

import os.path


ext = None


def convert_wave_to_pcm(filename):

file = open(filename, 'rb')

byteBuffer = bytearray(file.read())

file.close()


fn_ext = os.path.splitext(filename)

if fn_ext[1] == '.wav':

out_filename = fn_ext[0] + '.pcm'

else:

out_filename = fn_ext[0] + fn_ext[1] + '.pcm'

print 'Out file name: %s' % out_filename

out_file = open(out_filename, 'wb')

out_file.write(byteBuffer[44:])

out_file.close()


if len(sys.argv) is 1:

YesNo = raw_input('Do you want to convert *.wav to *.pcm? (Yes or No): ')

if 'yes' in YesNo:

filename = '.wav'

print('Convert *.wav to *.pcm !!!')

else:

print('Please try it again with a filename or .wav !!!')

exit(0)

else:

filename = sys.argv[1]


while True:

fn_ext = os.path.splitext(filename)

if (not fn_ext[1]) & fn_ext[0].count('.'):

if fn_ext[0] == '.':

print 'Input file name is invalid!!!'

exit(0)

else:

print 'Input file name has the only ext!!!'

ext = fn_ext[0]

break

else:

try:

convert_wave_to_pcm(filename)

break

except:

print '[Error] No such file: %s' % filename

filename = raw_input('Please type file name: ')


if ext is not None:

folder = os.getcwd()

print 'folder: %s' % folder

for filename in os.listdir(folder):

print filename

if filename.endswith(ext):

convert_wave_to_pcm(filename)


raw_input('Press Enter to exit')

exit(0)



wave2pcm_v1.py


반응형

+ Recent posts