mercredi 11 février 2015

How to overwrite file with ffmpeg when opened with pygame.mixer.music

I have this interesting situation in which I want to convert a lot of audio fragments using ffmpeg via subprocess.check_call() and then play them using pygame.mixer.music.play(). But because there would be a lot of small files when converting all of them, I want to overwrite the file every time, calling it tmp.wav.


Converting goes as follows:



outfilename = "tmp.wav"
proc_args = ["ffmpeg"]
proc_args += ["-ss", str(begin / 1000)]
proc_args += ["-i", os.path.join(audiodir, infilename)]
proc_args += ["-to", str(duration / 1000)]
proc_args += ["-ar", str(AUDIORATE)] # sample frequency (audio rate)
proc_args += ["-y"]
proc_args += [outfilename]
DEVNULL = open(os.devnull, 'wb')
try:
subprocess.check_call(proc_args, stdout = DEVNULL, stderr = DEVNULL)
# print "Converting audio succeeded."
except subprocess.CalledProcessError as e:
print "Converting audio failed."
return 0.


Playing goes as follows:



pygame.mixer.music.play(outfilename)


Now, a problem arises. The first file is converted and played correctly. But when skipping to the next file, tmp.wav can't be overwritten. I think this is due to the fact that the file is still opened in the music module, but that doesn't say how to close the file. I already tried pygame.mixer.music.stop(), pygame.mixer.quit() and pygame.mixer.stop() before converting the new file, but none of them works.


How do I solve this problem?


Aucun commentaire:

Enregistrer un commentaire