How to ensure a process has finished before a new script line executes

Hi, New here so hopefully I am following the forum protocols ok. Hello everyone.

I need to ensure a process to create a wav/mp3 file has completed before another line of my code is executed. Let me explain.

I am creating a number of very short videos using Moviepy and have been using a TTS (gTTS) import to create a couple of sentences in speech from a txt script, that get saved as a wav or mp3 file (say temp.wav) and then this is used later in my program when the temp.wav and Moviepy Video Files…etc are composited together, to create a final MP4 file.

Pseudo code to do this is as follows:
1 Check if temp.wav exists already. If so delete it. (so no chance of a video using a prior existing speech file).
2 Create a new 'temp.wav ’ file based on the current txt script.
3 Use the ‘temp.wav’ file together with some music mp3 files to create a composited audio file that then gets used later in my prog to composite with video to create my final MP4.

A certain % of the time I get an error. When 3 is executed it states ‘temp.wav doesn’t exist’

This sporadic problem exists between 2 and 3 above roughly 60-70% of the time I get the error. I have established that whilst 2 is in progress the Python back end is executing 3. This then gives an error, ’ can’t find the temp.wav’ file - which of course is being created at that time and has not yet been saved to file.

Approaches I have taken to resolve are:
1 Putting delays/wait lines between lines 2 and 3. No luck, the error appears at the same frequency.
2 Trying to use a ram memory drive to save the temp file to speed up processing. No luck the error appears at the same frequency.
3. Using a memory buffer to save the file and pass between 2 and 3. Again no luck.

It is clear that the Python back end that is processing the Py interpretation is going from step 2 to step 3 and just trying to execute 3 regardless of whether 2 has finished or not.

What approaches can be taken to ensure 2 has completed before Python tries to read any file in 3 (that won’t exist until 2 has concluded).

  1. Some kind of loop that keeps trying without error until a file exists. (this seems like it could create an infinite loop for whatever reason, so want to avoid).
    2… Threading - (not too sure if relevant?)
  2. Ability to intervene in the compiler/interpreter process to ensure one process ends before another can continue. This would seem to be the definitive way to go but have no clue how to go about.
  3. Divine inspiration (still waiting)

Help - :slight_smile:

What is the code you use to create the wave file, is it blocking or not?

1 Like

Hi, I’m using gTTS to create the voice .wav file. key lines are as follows:

    tts = gTTS(text=text,tld="co.uk", lang=lang_code, slow=False)
    audio_duration =len(text)*0.2
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
...
    engine.setProperty('voice', voice.id)
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-35)
    engine.setProperty('volume', vol)
    engine.save_to_file(text , temporaryttsvoicefile)  ******************
...
   engine.runAndWait()

The line with the **** is where the file is saved (temporaryttsvoicefile is a path to a temp file.wav). There is some surrounding code choosing the voice/volume etc. text is a ‘quote’ of 3-10 sentences. Creating an audio file of 20-30 secs ish.

Prior to this I have run.
file_to_delete = Path(temporaryttsvoicefile)
file_to_delete.unlink()…to make sure the existing wav file doesn’t accidently get used again.

Then I pick up the file (or sometimes I do, depending on the error) with the following code.

    audio_clip = AudioFileClip(temporaryttsvoicefile, buffersize=math.inf) 
           *****************problem line above********************
    Read_clip  =(audio_clip).set_start(vid_read_time_start)
    Sound_clip =(AudioFileClip(full_audio_path) 
                 .set_duration(vid_len).audio_fadeout(0.25).set_start(0.0))

As you see I’m pulling the tempttsvoicefile wav and using movie py to create a sound clip ( pulling in some music mp3s.) I then do some processing on the volumes etc to make sure the tts clip can be heard and composite them together.
The******** is the problem line where I get 'can’t find file error). The buffersize=math.inf doesn’t really do anything. I added that just in case it may help but it didn’t change anything). I have tried surrounding virtually every line above with a delay of up to 30 secs but the compiler/interpreter doesn’t heed these and I get the error anyway. (I guess the compiler/interpreterr does heed them but adds them for my benefit, in the background it races ahead and tries to execute the problem line. Hope this helps.

[quote=“R j Arno, post:4, topic:57959, full:true, username:rjamjb”]
Here is the actuall error - arrising in the ffmpeg_reader.py module…

OSError: MoviePy error: the file O:\Temp\trialtts.wav could not be found!

Please check that you entered the correct path.

In the stack trace the last line from my code is …
" line 6545, in main
audio_clip = AudioFileClip(temporaryttsvoicefile, buffersize=math.inf) "

Oh and by the way I do have a workaround solution. As I know what the txt lines are going to be in advance I can create a whole batch of small temp.wav files in advance and then use these, one at a time as needed. The code can then delete any used files at some later stage. I’ll probably move on and do this as I can’t fathom how to track what the complier may/may not be doing and I’ve ran out of other ideas - except for the ‘threading’ approach… Which I may take anther look at (not really used it previously so am a bit ignorant on how it may help). Although that would seem to be a workaround anyway.


What i do I don’t think

I would love it if this had any relevance to my post but it’s beating me what this means.

12h

Hi, I’m using gTTS to create the voice .wav file. key lines are as follows:

    tts = gTTS(text=text,tld="co.uk", lang=lang_code, slow=False)
    audio_duration =len(text)*0.2
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
...
    engine.setProperty('voice', voice.id)
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-35)
    engine.setProperty('volume', vol)
    engine.save_to_file(text , temporaryttsvoicefile)  ******************
...
   engine.runAndWait()

The line with the **** is where the file is saved (temporaryttsvoicefile is a path to a temp file.wav). There is some surrounding code choosing the voice/volume etc. text is a ‘quote’ of 3-10 sentences. Creating an audio file of 20-30 secs ish.

Prior to this I have run.
file_to_delete = Path(temporaryttsvoicefile)
file_to_delete.unlink()…to make sure the existing wav file doesn’t accidently get used again.

Then I pick up the file (or sometimes I do, depending on the error) with the following code.

    audio_clip = AudioFileClip(temporaryttsvoicefile, buffersize=math.inf) 
           *****************problem line above********************
    Read_clip  =(audio_clip).set_start(vid_read_time_start)
    Sound_clip =(AudioFileClip(full_audio_path) 
                 .set_duration(vid_len).audio_fadeout(0.25).set_start(0.0))

As you see I’m pulling the tempttsvoicefile wav and using movie py to create a sound clip ( pulling in some music mp3s.) I then do some processing on the volumes etc to make sure the tts clip can be heard and composite them together.
The******** is the problem line where I get 'can’t find file error). The buffersize=math.inf doesn’t really do anything. I added that just in case it may help but it didn’t change anything). I have tried surrounding virtually every line above with a delay of up to 30 secs but the compiler/interpreter doesn’t heed these and I get the error anyway. (I guess the compiler/interpreterr does heed them but adds them for my benefit, in the background it races ahead and tries to execute the problem line. Hope this helps.

Hi Solved. Very stupid issue caused by myself. Humble pie(Py?) duly eaten. I’d moved from using gTTS to the pyttsx3 TTS. However in stripping back the gTTS code I’d inadvertently left behind a couple of lines relating to the gTTS. (I actually commented on it in one of my answers as if it were relevant code). I think I just had a blind spot as the line/d was actually part of original working code. A couple of the ‘variables’ relating to voices (2men 1woman) were therefore being utilised by the old code left behind then double utilised in my new code. However, what a dumb thing to do I feel like an idiot. I reckon that I have spent over 16 hours directly and probably nearer 20 in total on this one issue. Doh!!!

I’m actually now thinking that I don’t think!!!