0

I am trying to create a new file recording every time this program runs and also convert those .wav files to .mp3. When I run this, it only creates a output.wav and output0.mp3 file and then when I run it again, no further files are created. Also the output0.mp3 that was converted is 0KB and cannot be played.

I do not get an error but it seems its not grabbing the output.wav properly that was originally created. I am running Python 3.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")

EDIT:

Updated while loop to:

#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
wovano
  • 4,543
  • 5
  • 22
  • 49
Student1860
  • 138
  • 2
  • 17
  • You should probably move your increment, `i +=1`, to the end. Your while is checking "output0.wav" then it immediately increments then your src and dst end up being output1.wav and output1.mp3. Also your write file should probably be inside the while loop before it increments and it's not actually writing anything, it just opens the file. – Hayden May 12 '22 at 15:31
  • I moved `i +- 1` to the end of the while and put `write("output%s.mp3" % i, "w")` above it but now there is no `.mp3` file being created at all and `output.wav` is only created and being overwritten – Student1860 May 12 '22 at 15:51
  • It looks that your code is not even entering while loop. You are creating file output.wav and checking for file output0.wav in while that do not exist. Also the writefile = open("output%s.mp3" % i, "w") - is outside loop and hence it will just create an empty file once. – Pankaj May 12 '22 at 16:09

2 Answers2

0

"create a new file recording every time this program runs " - To what I understand you just need to check for existing files and get a counter to reach +1 then the last file. Once you get that just create/convert file based on that.

I am not familiar with working of sound module, but in general below should be the code structure.

## 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")
Pankaj
  • 2,692
  • 2
  • 6
  • 18
  • This gives the error `FileNotFoundError: [Errno 2] No such file or directory: 'output0.wav'` – Student1860 May 12 '22 at 16:45
  • Yes, it just needs to use target or destination. Source is same, i.e. output.wav. Edited my suggestion. – Pankaj May 12 '22 at 16:48
  • Unfortunately the conversion fails and I get thrown an error. I know you mentioned that you are not familiar with those innerworkings of Pydub so probably the reason – Student1860 May 12 '22 at 16:54
0

SOLUTION: Updated my while loop and changed the conversion method

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)
Student1860
  • 138
  • 2
  • 17