将.wav文件转换为.mp3

时间:2017-10-07 21:09:04

标签: python audio file-type

我有一个.wav文件,我想将其转换为.mp3文件。我做了一个python程序来执行此操作,它编译,运行并创建.mp3文件,但是当我尝试播放该文件时,我收到一条错误消息。由于我创建它的方式,我认为该文件是不可读的,但我无法弄清楚原因。

这是我的计划:

with open('pcm-test-1.wav', 'rb') as f:
    data = f.read()

a = [255,254,0,192]
b = []

for i in range (223,len(data),2):
    a.append(ord(data[i]))

a.append(ord('T'))
a.append(ord('A'))
a.append(ord('G'))
for i in range(0,124):
    a.append(0)

for i in a:
    b.append(chr(a[i]))

string = ''.join(b)

new = open('newFile.mp3','wb')
new.write(string)

我在python中做过这个,但任何语言的实现都可以。谢谢!

2 个答案:

答案 0 :(得分:0)

你最好的选择是用LAME做到这一点。安装完成后,请执行以下操作:

import sys
inputName = sys.argv[1]
outputName = sys.argv[2]

infile = open(inputName,"r",encoding="utf-8")
outfile = open(outputName,"w",encoding="utf-8")
result = []
for line in infile:

   for c in line:
       if c not in result:
           result.append(c)
           outfile.write(c.strip())

          if(c == ' '):
             pass
          else:
             outfile.write(' ')

outfile.write('\n')

答案 1 :(得分:0)

如果您对外部库开放,pydub可以使用最少量的代码轻松完成此任务。

from pydub import AudioSegment
from pydub.playback import play

#read wav file to an audio-segment
song = AudioSegment.from_wav("your_wave_file.wav")

#export audio segment to mp3
song.export("your_wave_file.mp3", format="mp3")

#play audio-segment
play(song)