Skip to content

Instantly share code, notes, and snippets.

@jyotendra
Created October 15, 2019 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyotendra/142be92498d971a79bfb88a9ecac7fe1 to your computer and use it in GitHub Desktop.
Save jyotendra/142be92498d971a79bfb88a9ecac7fe1 to your computer and use it in GitHub Desktop.
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