概述
1,截取视频,抽帧&&快速抽帧
# -*- coding: utf-8 -*-
import cv2 as cv
def calcute(minute, second, fps):
return minute * fps * 60 + second * fps
def cut_video(src, dst, start, end):
# 读取视频
video = cv.VideoCapture(src)
fps = video.get(cv.CAP_PROP_FPS) # 帧率,每秒多少帧
minute = int(fps) * 60 # 每分钟的帧数
# 保存视频
fourcc = cv.VideoWriter_fourcc(*'XVID') # 保存视频的编码
out = cv.VideoWriter(dst, fourcc, fps, (1920, 1080))
# 读取视频
i = 0
while (video.isOpened()):
ret, frame = video.read() # 读取图片
# 读取区间
if start <= i and i <= end: # 区间内
out.write(frame)
# 显示分钟数
if i % minute == 0:
now_minute = i // minute
print("pass {} minutes".format(now_minute))
if not ret: # 条件终止
break
i += 1 # 遍历视频
video.release()
out.release()
# 方法二,快
i = 0
while (video.isOpened()):
ret = video.grab()
# 每个5帧取一帧
if i % int(fps) == 0:
ret, frame = video.retrieve()
i += 1
if __name__ == '__main__':
src = r"D:1.mp4"
dst = r"D:videocut_video1.mp4"
start = calcute(minute=24, second=27, fps=25)
end = calcute(minute=29, second=0, fps=25)
cut_video(src, dst, start, end)
2,截图图片
# -*- coding: utf-8 -*-
import cv2 as cv
import os
def cut_video(src, dst, index):
# 读取视频
video = cv.VideoCapture(src)
fps = video.get(cv.CAP_PROP_FPS) # 帧率,每秒多少帧
minute = int(fps) * 60 # 每分钟的帧数
# 读取视频
i = 0 # 视频索引
while (video.isOpened()):
ret, frame = video.read() # 读取图片
# 读取视频
if i % 25 == 0: # 区间内,每5帧取一帧
img_path = dst + "/{}.jpg".format(index)
print(img_path)
cv.imwrite(img_path, frame, [int(cv.IMWRITE_JPEG_QUALITY), 95]) # 默认95
index += 1 # 增加图片索引
if not ret: # 条件终止
break
i += 1 # 视频索引
video.release()
return index
def video2jpg(src, dst, index=0):
# 读取视频文件
for file in os.listdir(src):
full_path = src + "/" + file
# 视频 -> jpg
index = cut_video(full_path, dst, index)
index += 1
if __name__ == '__main__':
src = r"D:datacuttedday02"
dst = r"D:day02" # 保存路径不能出现中文字符
index = 0
# 将文件里所有视频转为图片
video2jpg(src, dst, index)
3.获取视频流格式
def get_video_format(fourcc):
decoded_codec_format = chr(fourcc & 0xFF) + chr((fourcc & 0xFF00) >> 8) +
chr((fourcc & 0xFF0000) >> 16) + chr((fourcc & 0xFF000000) >> 24)
return decoded_codec_format
four = cap.get(cv2.CAP_PROP_FOURCC)
fourcc = get_video_format(int(four))
4.ffmpeg
ffmpeg读取视频流
最后
以上就是无私发带为你收集整理的【opencv】视频流的读取的全部内容,希望文章能够帮你解决【opencv】视频流的读取所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复