视频解码opencv、ffmpeg、decord三种方式速度对比

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

# -*- 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视频解码速度最快。
欢迎大家测试代码,有问题请提出宝贵意见。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值