Home > Net >  ffmpeg save output to a python variable and do further ffmpeg operations without writing multiple fi
ffmpeg save output to a python variable and do further ffmpeg operations without writing multiple fi

Time:10-05

I want to convert a gif to a video with ffmpeg in python and add sound to the video. Afterwards I want to merge the video with another video. I'm able to run the shell codes with os.system() in python, however I would like to avoid writing multiple files during my steps.

Is there a way to save the ffmpeg output in a variable and work with the variable instead of writing multiple files for each step? I want only to write my final file which should be a merged video file, containing the video and the sound from video1 and another video2. I've found some ways like the subprocess library, but unfortunately was not able to code my desired output.

Here is the code I use and which works for converting a gif to mp4 and add sound. But as described I need to write 2 different files.

GIF to video:

os.system(f'ffmpeg -i {PATH_TO_GIF} -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4')

Add sound to video:

os.system(f"ffmpeg -i video.mp4 -i {PATH_TO_SOUNDFILE} -map 0:v -map 1:a -c:v copy -shortest video_with_sound.mp4")

CodePudding user response:

maybe something like

ffmpeg -i i.gif -i 1.wav -map 0:v -map 1:a -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -shortest vid.mp4

Or maybe somehow possible with a two-pass conversion and dev null. As a last resort, it is always possible to execute several commands at once and remove unnecessary

  • Related