无法正确增加变量并将 .wav 转换为 .mp3
Unable to properly increment variable and convert to .wav to .mp3
我正在尝试创建一个新的文件记录,每次此程序 运行s 并将这些 .wav 文件转换为 .mp3。当我 运行 这个时,它只会创建一个 output.wav
和 output0.mp3
文件,然后当我再次 运行 它时,不会创建更多文件。还有转换出来的output0.mp3
是0KB,不能播放
我没有收到错误,但它似乎没有正确获取最初创建的 output.wav
。我是运行宁Python3.7.
import os
import sounddevice as sd
from scipy.io.wavfile import write
from pydub import AudioSegment #for converting WAV to MP3
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording ) # Save as WAV file
#Increments file name by 1 so it writes a new file every time it runs
i = 0
while os.path.exists("output%s.wav" % i):
i += 1
# files for converting WAV to Mp3
src = ("output%s.wav" % i)
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
writefile = open("output%s.mp3" % i, "w")
编辑:
已将 while
循环更新为:
#Increments file name by 1 so it writes a new file every time it runs
i = 0
while os.path.exists("output%s.wav" % i):
# files for converting WAV to Mp3
src = ("output%s.wav" % i)
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
write("output%s.mp3" % i, "w")
i += 1
“每次运行此程序时创建一个新文件记录” - 据我了解,您只需要检查现有文件并获得一个计数器以达到 +1 然后是最后一个文件。一旦你得到那个 create/convert 基于那个的文件。
声音模块的工作原理我不是很熟悉,不过大体上应该是下面的代码结构。
## This will create new recording file called output.wav
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording ) # Save as WAV file
# Get the counter to reach the last file that has been created.
# For e.g. if last file generated was output5.wav then below loop will run 5 times
# and should change the value of i = 6.
i = 0
while os.path.exists("output%s.wav" % i):
i += 1
# Code for creating new file using value of 'i'
# Below code is outside of while loop and will run only once,
# as 1 file needs to be created per run of the program.
src = ("output.wav")
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
# Not sure if this is needed.
# Check working of sound module and what does sound.export do
writefile = open("output%s.mp3" % i, "w")
解决方案:更新了我的 while 循环并更改了转换方法
i = 0
while not os.path.exists("output.wav"):
i += 1
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output{0}.wav'.format(i), fs, myrecording ) # Save as WAV file
print("recording has finished")
datadir = str(Path(r"FilePathtoFolderWhereAudioFileIs"))
filetopen = datadir + "/" + 'output{0}.wav'.format(i)
sound = pydub.AudioSegment.from_wav(r"FilePathtoFolderWhereAudioFileIs""" + "\output{0}.wav".format(i))
sound.export(r"FilePathtoFolderWhereAudioFileIs""" + "\output{0}.mp3".format(i), format="mp3")
print("Converted wav to mp3")
time.sleep(3)
我正在尝试创建一个新的文件记录,每次此程序 运行s 并将这些 .wav 文件转换为 .mp3。当我 运行 这个时,它只会创建一个 output.wav
和 output0.mp3
文件,然后当我再次 运行 它时,不会创建更多文件。还有转换出来的output0.mp3
是0KB,不能播放
我没有收到错误,但它似乎没有正确获取最初创建的 output.wav
。我是运行宁Python3.7.
import os
import sounddevice as sd
from scipy.io.wavfile import write
from pydub import AudioSegment #for converting WAV to MP3
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording ) # Save as WAV file
#Increments file name by 1 so it writes a new file every time it runs
i = 0
while os.path.exists("output%s.wav" % i):
i += 1
# files for converting WAV to Mp3
src = ("output%s.wav" % i)
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
writefile = open("output%s.mp3" % i, "w")
编辑:
已将 while
循环更新为:
#Increments file name by 1 so it writes a new file every time it runs
i = 0
while os.path.exists("output%s.wav" % i):
# files for converting WAV to Mp3
src = ("output%s.wav" % i)
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
write("output%s.mp3" % i, "w")
i += 1
“每次运行此程序时创建一个新文件记录” - 据我了解,您只需要检查现有文件并获得一个计数器以达到 +1 然后是最后一个文件。一旦你得到那个 create/convert 基于那个的文件。
声音模块的工作原理我不是很熟悉,不过大体上应该是下面的代码结构。
## This will create new recording file called output.wav
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording ) # Save as WAV file
# Get the counter to reach the last file that has been created.
# For e.g. if last file generated was output5.wav then below loop will run 5 times
# and should change the value of i = 6.
i = 0
while os.path.exists("output%s.wav" % i):
i += 1
# Code for creating new file using value of 'i'
# Below code is outside of while loop and will run only once,
# as 1 file needs to be created per run of the program.
src = ("output.wav")
dst = ("output%s.mp3" % i)
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
# Not sure if this is needed.
# Check working of sound module and what does sound.export do
writefile = open("output%s.mp3" % i, "w")
解决方案:更新了我的 while 循环并更改了转换方法
i = 0
while not os.path.exists("output.wav"):
i += 1
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output{0}.wav'.format(i), fs, myrecording ) # Save as WAV file
print("recording has finished")
datadir = str(Path(r"FilePathtoFolderWhereAudioFileIs"))
filetopen = datadir + "/" + 'output{0}.wav'.format(i)
sound = pydub.AudioSegment.from_wav(r"FilePathtoFolderWhereAudioFileIs""" + "\output{0}.wav".format(i))
sound.export(r"FilePathtoFolderWhereAudioFileIs""" + "\output{0}.mp3".format(i), format="mp3")
print("Converted wav to mp3")
time.sleep(3)