ffmpeg writes to a temp file and then removes the original to avoid corruption while it is processing, and now uses WAV PCM as the audio format to try to maximise the quality

This commit is contained in:
WORD559 committed 2018-07-05 21:35:04 +01:00
1 parent 15a006cc5b
commit dd51daf550
1 file changed
+5 -3
+5 -3
View File
@@ -857,11 +857,11 @@ if __name__ == '__main__':
# Now to make the video in ffmpeg # Now to make the video in ffmpeg
print "Exporting video with ffmpeg..." print "Exporting video with ffmpeg..."
export_command = ["ffmpeg","-framerate",str(fps),"-start_number","1","-i","{path}/frame %03d.png".format(path=tempdir),"-i","{path}/sounds/BGM.wav".format(path=tempdir),"-c:v","libx264","-shortest","-y",out_file] export_command = ["ffmpeg","-framerate",str(fps),"-start_number","1","-i","{path}/frame %03d.png".format(path=tempdir),"-i","{path}/sounds/BGM.wav".format(path=tempdir),"-c:v","libx264","-c:a","pcm_s16le","-shortest","-y",out_file]
if not os.path.isfile(tempdir+"/sounds/BGM.wav"): if not os.path.isfile(tempdir+"/sounds/BGM.wav"):
print "No background music. Adding silent track..." print "No background music. Adding silent track..."
#has_bgm = False #has_bgm = False
export_command = ["ffmpeg","-framerate",str(fps),"-start_number","1","-i","{path}/frame %03d.png".format(path=tempdir),"-f","lavfi","-i","anullsrc=r=8192:cl=mono","-c:v","libx264","-shortest","-y",out_file] export_command = ["ffmpeg","-framerate",str(fps),"-start_number","1","-i","{path}/frame %03d.png".format(path=tempdir),"-f","lavfi","-i","anullsrc=r=8192:cl=mono","-c:v","libx264","-c:a","pcm_s16le","-shortest","-y",out_file]
else: else:
#has_bgm = True #has_bgm = True
pass pass
@@ -875,7 +875,7 @@ if __name__ == '__main__':
# The second concatenates the silent track and the sound effect, producing a sound file that can be mixed into the video's audio track so that it plays at the correct time. # The second concatenates the silent track and the sound effect, producing a sound file that can be mixed into the video's audio track so that it plays at the correct time.
SFX_command = ["ffmpeg","-i","{path}/silence.wav","-i","{path}/sounds/{sfx}.wav","-filter_complex","[0:a] [1:a] concat=n=2:v=0:a=1","-y","{path}/sfx.wav"] SFX_command = ["ffmpeg","-i","{path}/silence.wav","-i","{path}/sounds/{sfx}.wav","-filter_complex","[0:a] [1:a] concat=n=2:v=0:a=1","-y","{path}/sfx.wav"]
# The third mixes the silence+sound effect into the video file # The third mixes the silence+sound effect into the video file
merge_command = ["ffmpeg","-i",out_file,"-i","{path}/sfx.wav","-filter_complex","[0:a][1:a] amix=inputs=2:duration=longest","-c:v","copy","-y",out_file] merge_command = ["ffmpeg","-i",out_file,"-i","{path}/sfx.wav","-filter_complex","[0:a][1:a] amix=inputs=2:duration=longest","-c:a","pcm_s16le","-c:v","copy","-y","{path}/temp_out.mkv"]
## for sfx in ["SFX1","SFX2","SFX3"]: ## for sfx in ["SFX1","SFX2","SFX3"]:
## if not os.path.isfile("{path}/sounds/{sfx}.wav".format(path=tempdir,sfx=sfx)): ## if not os.path.isfile("{path}/sounds/{sfx}.wav".format(path=tempdir,sfx=sfx)):
@@ -902,6 +902,8 @@ if __name__ == '__main__':
subprocess.call([i.format(path=tempdir,sfx=sfx,length=length) for i in silence_command],stdout=null,stderr=null) subprocess.call([i.format(path=tempdir,sfx=sfx,length=length) for i in silence_command],stdout=null,stderr=null)
subprocess.call([i.format(path=tempdir,sfx=sfx) for i in SFX_command],stdout=null,stderr=null) subprocess.call([i.format(path=tempdir,sfx=sfx) for i in SFX_command],stdout=null,stderr=null)
subprocess.call([i.format(path=tempdir) for i in merge_command],stdout=null,stderr=null) subprocess.call([i.format(path=tempdir) for i in merge_command],stdout=null,stderr=null)
os.remove(out_file)
shutil.move("{path}/temp_out.mkv".format(path=tempdir),out_file)
print "Done!" print "Done!"
time.sleep(sleep_time) # optional sleep -- in case you want to slow things down for HDD strain or reliability or something time.sleep(sleep_time) # optional sleep -- in case you want to slow things down for HDD strain or reliability or something