Can you "stream" images to ffmpeg to construct a video, instead of saving them to disk? Can you "stream" images to ffmpeg to construct a video, instead of saving them to disk? python python

Can you "stream" images to ffmpeg to construct a video, instead of saving them to disk?


Ok I got it working. thanks to LordNeckbeard suggestion to use image2pipe. I had to use jpg encoding instead of png because image2pipe with png doesn't work on my verision of ffmpeg. The first script is essentially the same as your question's code except I implemented a simple image creation that just creates images going from black to red. I also added some code to time the execution.

serial execution

import subprocess, Imagefps, duration = 24, 100for i in range(fps * duration):    im = Image.new("RGB", (300, 300), (i, 1, 1))    im.save("%07d.jpg" % i)subprocess.call(["ffmpeg","-y","-r",str(fps),"-i", "%07d.jpg","-vcodec","mpeg4", "-qscale","5", "-r", str(fps), "video.avi"])

parallel execution (with no images saved to disk)

import Imagefrom subprocess import Popen, PIPEfps, duration = 24, 100p = Popen(['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'mjpeg', '-r', '24', '-i', '-', '-vcodec', 'mpeg4', '-qscale', '5', '-r', '24', 'video.avi'], stdin=PIPE)for i in range(fps * duration):    im = Image.new("RGB", (300, 300), (i, 1, 1))    im.save(p.stdin, 'JPEG')p.stdin.close()p.wait()

the results are interesting, I ran each script 3 times to compare performance:serial:

12.906232118612.896506071112.9360799789

parallel:

8.677976846698.571393966678.38926696777

So it seems the parallel version is faster about 1.5 times faster.


imageio supports this directly. It uses FFMPEG and the Video Acceleration API, making it very fast:

import imageiowriter = imageio.get_writer('video.avi', fps=fps)for i in range(frames_per_second * video_duration_seconds):    img = createFrame(i)    writer.append_data(img)writer.close()