我正在通过OpenCV对numpy数组进行一些图像操作。然后将这些图像烘焙到jpeg,然后摄入FFMPEG以制作视频。但是,将其烘焙到文件并不是非常有效。我想直接将其流式传输到FFMPEG中。从理论上讲,它看起来像这样:
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...
我已经能够使用此设置通过Pillow实现此目的:
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')
所以,我的问题是:
我如何将一个OpenCV Jpeg缓冲区编写到p.stdin对象中,就像在Pillow版本中一样?