きっかけ
友達が飲み会での会話を録音したんだけど、細切れに録音したので結合しなければならなくなった。しかしMacで結合するのにAudacityとかGarageBandとかがあるっぽいけど20個もファイルがあるとめちゃくちゃ重くなったり、アプリケーション自体が落ちたりしてイライラしたんで、Pythonでやってみた。
方法
Pythonの標準ライブラリを使えばできます。
waveモジュールを使えば簡単にwavファイルを操作できます。ヘッダ情報も簡単に取得/書込できるので便利です。
コードはこんな感じで。下のコードは「1.WAV〜20.WAVを結合して99.WAVにする」というスクリプトです。
import wave def join_waves(inputs, output): ''' inputs : list of filenames output : output filename ''' try: fps = [wave.open(f, 'r') for f in inputs] fpw = wave.open(output, 'w') fpw.setnchannels(fps[0].getnchannels()) fpw.setsampwidth(fps[0].getsampwidth()) fpw.setframerate(fps[0].getframerate()) for fp in fps: fpw.writeframes(fp.readframes(fp.getnframes())) fp.close() fpw.close() except wave.Error, e: print e except Exception, e: print 'unexpected error -> ' + str(e) if __name__ == '__main__': inputs = [str(n) + '.WAV' for n in range(1,21)] output = '99.WAV' join_waves(inputs, output)