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).
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?)
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.
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.
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.
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.
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.
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!!!