Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This gist describes serialising a video, frame by frame, and then reconstructing the image using opencv and ffmpeg
import subprocess as sp
import cv2 as cv
import numpy as np
import ffmpeg
probe = ffmpeg.probe('/home/jyotendra/Videos/pandas.mp4')
video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
width = int(video_info['width'])
height = int(video_info['height'])
num_frames = int(video_info['nb_frames'])
command = ["ffmpeg", "-i", "/home/jyotendra/Videos/pandas.mp4", "-f", "image2pipe", "-pix_fmt", "rgb24", "-vcodec", "rawvideo", "-"]
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
for i in [*range(300)]:
raw_image = pipe.stdout.read(width * height * 3)
# print("img" + str(raw_image) + "\n")
image = np.fromstring(raw_image, dtype='uint8')
if image.size > 0:
image = image.reshape((height, width, 3))
# image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
cv.imwrite("test"+str(i)+".jpg", image)
# print(image)
pipe.stdout.flush()
import subprocess as sp
import cv2 as cv
import numpy as np
import ffmpeg
probe = ffmpeg.probe('/home/jyotendra/Videos/pandas.mp4')
video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
width = int(video_info['width'])
height = int(video_info['height'])
num_frames = int(video_info['nb_frames'])
command = ["ffmpeg", "-i", "/home/jyotendra/Videos/pandas.mp4", "-vf", "scale=420:360", "-f", "image2pipe", "-pix_fmt", "rgb24", "-vcodec", "rawvideo", "-"]
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
for i in [*range(300)]:
raw_image = pipe.stdout.read(420 * 360 * 3)
# print("img" + str(raw_image) + "\n")
image = np.fromstring(raw_image, dtype='uint8')
if image.size > 0:
image = image.reshape((360, 420, 3))
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
cv.imwrite("test"+str(i)+".jpg", image)
# print(image)
pipe.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment