Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparing speed of reading video frames via a subprocess, or via PyAV
2013-10-05 10:19:59 mikeboers@maxwell: ~/Documents/Python/PyAV
$ time python video_frames_via_pipe.py
real 0m0.564s
user 0m0.729s
sys 0m0.241s
2013-10-05 10:20:01 mikeboers@maxwell: ~/Documents/Python/PyAV
$ time python video_frames_via_pyav.py
real 0m0.220s
user 0m0.457s
sys 0m0.059s
import subprocess
import numpy
proc = subprocess.Popen(['ffmpeg',
"-i", "sandbox/640x360.mp4",
"-f", "image2pipe",
"-pix_fmt", "rgb24",
"-vcodec", "rawvideo",
"-"],
stdout=subprocess.PIPE,
)
while True:
raw_image = proc.stdout.read(640 * 360 * 3)
if not raw_image:
break
image = numpy.fromstring(raw_image, dtype='uint8').reshape((640, 360, 3))
import Image
import av
def frame_iter(video):
streams = [s for s in video.streams if s.type == b'video']
streams = [streams[0]]
for packet in video.demux(streams):
for frame in packet.decode():
yield frame
video = av.open('sandbox/640x360.mp4')
frames = frame_iter(video)
for frame in frames:
img = Image.frombuffer("RGBA", (frame.width, frame.height), frame.to_rgba(), "raw", "RGBA", 0, 1)
@yangcha

This comment has been minimized.

Copy link

yangcha commented May 26, 2018

Is this comparison fair enough? In video_frames_via_pipe.py, line 18:

image = numpy.fromstring(raw_image, dtype='uint8').reshape((640, 360, 3))

it will make a copy of the data. Instead, frombuffer will not make a copy(similar to what you did in video_frames_via_pyav.py, line 16):

image = numpy.frombuffer(raw_image, dtype='uint8').reshape((640, 360, 3))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.