I am doing some image operations on a numpy array via OpenCV. These images are then being baked out to jpeg, then ingested into FFMPEG to make a video. However, baking it out to a file is not very efficient. I would like to stream this directly into FFMPEG. In theory, it would look something like this:
p = Popen(['/usr/local/bin/ffmpeg', '-s', '1920x1080',
'-pix_fmt', 'yuvj420p',
'-y',
'-f', 'image2pipe',
'-vcodec', 'mjpeg',
'-r', self.fps,
'-i', '-',
'-r', self.fps,
'-f', 'mp4',
'-vcodec', 'libx264',
'-preset', 'fast',
# '-crf', '26',
'output/{}.mp4'.format(self.animation_name)], stdin=PIPE)
image_resize = cv.resize(self.original_image, (0, 0), fx=zoom, fy=zoom)
M = np.float32([[1, 0, x_total], [0, 1, y_total]])
image_offset = cv.warpAffine(image_resize, M, (self.original_image_width, self.original_image_width))
image = image_offset[0:self.output_raster_height, 0:self.output_raster_width].copy()
cv.imwrite(p.stdin, image) # this doesn't actually work, but that's the idea...
I've been able to achieve this with Pillow using this setup:
p = Popen(['/usr/local/bin/ffmpeg', '-s', '1920x1080',
'-pix_fmt', 'yuvj420p',
'-y',
'-f', 'image2pipe',
'-vcodec', 'mjpeg',
'-r', self.fps,
'-i', '-',
'-r', self.fps,
'-f', 'mp4',
'-vcodec', 'libx264',
'-preset', 'fast',
# '-crf', '26',
'output/{}.mp4'.format(self.animation_name)], stdin=PIPE)
image_resize = self.original_image.resize((resize_width, resize_height), resample=PIL.Image.BICUBIC)
image_offset = ImageChops.offset(image_resize, xoffset=int(x_total), yoffset=int(y_total))
image = image_offset.crop((0, 0, self.output_raster_width, self.output_raster_height))
image.save(p.stdin, 'JPEG')
So, my question is:
How would I write an OpenCV Jpeg buffer into the p.stdin object like it's being done in the Pillow version?