在做视频理解和视频相关的项目都绕不开视频解码的过程,本文对比了三种视频解码的方法,仅供参考。

# -*- coding: utf-8 -*-
# @Time    : 2020年9月30日16:01:40
# @Author  : liyang
# @function: 视频解码 videotoarray

import decord
import cv2
import ffmpeg
import numpy as np
import time
import pydub

pydub.AudioSegment.converter = r"E:\\ffmpeg20190916\\bin\\ffmpeg.exe"

def videoToArrayByCV2(video_path, height, width):
	try:
		cap = cv2.VideoCapture(video_path)
		frames = []
		while cap.isOpened():
			ret, frame = cap.read()
			if isinstance(frame, np.ndarray):
				new_frame = cv2.resize(frame, (height, width))
				frame = cv2.cvtColor(new_frame, cv2.COLOR_BGR2RGB)
				frames.append(frame)
			else:
				break
		cap.release()
		video_tensor = np.array(frames)
		return video_tensor
	except Exception as e:
		raise Exception('Can\'t load video {}\n{}'.format(video_path, e.message))


def videoToArrayByFFmpeg(video_path, height, width):
	try:
		out, _ = (
			ffmpeg
				.input(video_path)
				.output('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(height, width))
				.run(capture_stdout=True, quiet=True)
		)
		video_tensor = (
			np
				.frombuffer(out, np.uint8)
				.reshape([-1, height, width, 3])
		)
		return video_tensor
	except Exception as e:
		raise Exception('Can\'t load video {}\n{}'.format(video_path, str(e)))


def videoToArrayByDecord(video_path, height, width):
	try:
		vr = decord.VideoReader(video_path, ctx=decord.cpu(0), width=width, height=height, num_threads=10)
		video_tensor = vr.get_batch(range(0, len(vr))).asnumpy()
		return video_tensor
	except Exception as e:
		raise Exception('Can\'t load video {}\n{}'.format(video_path, str(e)))


if __name__ == '__main__':
	video_path = '../video/DF5DC4676883EC33-200.mp4'
	height = 224
	width = 224

	print("-------------opencv----------------")

	start_time_cv = time.time()
	video_tensor_cv2 = videoToArrayByCV2(video_path, height, width)
	end_time_cv = time.time()
	print ("opencv cost time:" + str(end_time_cv - start_time_cv) + "s.")
	print ("opencv array shape:" + str(video_tensor_cv2.shape))

	print("-------------ffmpeg----------------")

	start_time_ffmpeg = time.time()
	video_tensor_ffmpeg = videoToArrayByFFmpeg(video_path, height, width)
	end_time_ffmpeg = time.time()
	print ("ffmpeg cost time:" + str(end_time_ffmpeg - start_time_ffmpeg) + "s.")
	print ("ffmpeg array shape:" + str(video_tensor_ffmpeg.shape))

	print("-------------decord----------------")

	start_time_decord = time.time()
	video_tensor_decord = videoToArrayByDecord(video_path, height, width)
	end_time_decord = time.time()
	print ("decord cost time:" + str(end_time_decord - start_time_decord) + "s.")
	print ("decord array shape:" + str(video_tensor_decord.shape))

解码结果:
在这里插入图片描述
测试结果是decord视频解码速度最快。
欢迎大家测试代码,有问题请提出宝贵意见。

GitHub 加速计划 / opencv31 / opencv
77.38 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
c3747a68 Added Universal Windows Package build to CI. 7 天前
9b635da5 - 7 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐