1

I am currently making a python script that records for a specific amount of time that the user inputs, and then encrypts it with a password.

But when I start recording, it records forever, and I have to stop the script running to stop recording.

from sounddevice import rec, wait
from scipy.io.wavfile import write
from getpass import getpass
from pyAesCrypt import encryptFile
from os import remove

fs = 44100  # Sample rate
while True:
    try:
        seconds = int(input("Enter how long to record for: "))
    except ValueError:
        print("Must be an integer!")
    else:
        break
myrecording = rec(int(seconds * fs), samplerate=fs, channels=2)  # Record file
wait()  # Wait until recording is finished
write('output.wav', fs, myrecording)
password = getpass("Enter password to encrypt file with: ")
encryptFile("output.wav", "output.ewf", password)
remove("output.wav")
print("Saved file as output.ewf")

is my code.

I have no idea why it stopped working. Thanks

itsmii
  • 11
  • 4
  • Are you sure you're using wait correctly? Looks like you should maybe be trying a conditional like if myrecording.wait(): then the rest of your code. *edit, from here: https://python-sounddevice.readthedocs.io/en/0.3.12/usage.html – d6stringer Sep 02 '21 at 21:37
  • Yes, sd.wait() should work If the recording was already finished, this returns immediately; if not, **it waits and returns as soon as the recording is finished.** – itsmii Sep 03 '21 at 12:12
  • I just ran your code w/ no errors. It seems to be running as expected. Have you managed to figure out what's wrong or is there something else happening? Did it work for a while then stop? – d6stringer Sep 03 '21 at 16:32
  • Yup. Once I imported pyAesCrypt and used it, it suddenly stopped working. – itsmii Sep 03 '21 at 18:46

1 Answers1

0

For the googlers out there, don't worry, it's fixed.

If you use PyCharm, then make sure to edit configuration and check Emulate terminal in output console

I think the problem was getpass.

itsmii
  • 11
  • 4