【Opencv3】视频操作 读写avi,mp4,flv

1. 读视频文件

import cv2

# This is a demo of running face recognition on a video file and saving the results to a new video file.
#
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Open the input movie file
input_movie = cv2.VideoCapture("D:/work_test/object_predict.avi")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

pre_file = '12_28_62_'
f_index = 0

while True:
    # Grab a single frame of video
    ret, frame = input_movie.read()

    # Quit when the input video file ends
    if not ret:
        break
    
    frame_resize = cv2.resize(frame, (640, 360), interpolation=cv2.INTER_AREA)
    # Write the resulting image to the output video file
    #print("Writing frame {} / {}".format(frame_number, length))
    cv2.imwrite("D:/work_test/op/"+pre_file+str(f_index)+".jpg", frame_resize)
    f_index += 1

# All done!
input_movie.release()
cv2.destroyAllWindows()
都文件avi和mp4没有任何区别,这里就不多说

读完的文件保存到一个目录下面

2. 写avi文件

import os
import cv2

# This is a demo of running face recognition on a video file and saving the results to a new video file.
#
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
length = 30

# Create an output movie file (make sure resolution/frame rate matches input video!)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('output2_8.avi', fourcc, length, (1920, 1080))

frame_number = 0

file_path = 'D:/work_test/face_recognition-master/examples/2_8/'

for i in range(3127):
    # Grab a single frame of video
    frame = cv2.imread(file_path+"12_28_62_"+str(i)+".jpg")
    frame_number += 1

    # Write the resulting image to the output video file
    print("Writing frame {} / {}".format(frame_number, length))
    output_movie.write(frame)

# All done!
cv2.destroyAllWindows()
将一个目录下面的写到avi目录文件中,我们注意到

fourcc = cv2.VideoWriter_fourcc(*'XVID')
用来指定格式的

opencv3支持的avi格式有

I420:  未压缩YUV颜色编码

PIMI:  MPEG-1编码

XVID: MPEG-4编码

3. 写mp4

import os
import cv2

# This is a demo of running face recognition on a video file and saving the results to a new video file.
#
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
length = 20

# Create an output movie file (make sure resolution/frame rate matches input video!)
#fourcc = cv2.VideoWriter_fourcc(*'MP4V')
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
output_movie = cv2.VideoWriter('output2_9.mp4', fourcc, length, (640, 360))

frame_number = 0

file_path = 'D:/work_test/face_recognition-master/examples/2_1/'

for i in range(3127):
    # Grab a single frame of video
    frame = cv2.imread(file_path+"12_28_62_"+str(i)+".jpg")
    frame_number += 1

    # Write the resulting image to the output video file
    print("Writing frame {} / {}".format(frame_number, length))
    output_movie.write(frame)

# All done!
cv2.destroyAllWindows()

这里我们看到

fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
其实以下三种写法是等价的

fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
由于mp4对于avi的压缩比比较好,所有常用mp4,这里我们实验的数据是avi 80.7M, mp4是50.4M,效果非常明显

4. 写flv文件,这里我们没有实验相应的代码,直接列出格式:FLV1

  • 13
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要使用OpencvSharp打开MP4视频并进行操作,可以按照以下步骤进行: 1. 首先,确保已正确安装并配置了OpencvSharp库。可以从OpencvSharp的官方网站下载并按照说明进行安装。 2. 导入相应的命名空间,并创建一个VideoCapture对象来读取视频文件。例如: ```csharp using OpenCvSharp; // 打开视频文件 var capture = new VideoCapture("video.mp4"); ``` 3. 检查VideoCapture对象是否成功打开了视频文件。可以使用IsOpened方法来检查。如果成功打开了视频文件,则可以继续进行后续操作。例如: ```csharp // 检查是否成功打开了视频文件 if (!capture.IsOpened()) { Console.WriteLine("无法打开视频文件。"); return; } ``` 4. 在一个循环中,从VideoCapture对象中读取每一帧,并对每一帧进行处理。可以使用Read方法来读取下一帧。例如: ```csharp // 读取视频帧 using (var frame = new Mat()) { while (capture.Read(frame)) { // 在这里对每一帧进行操作 // 可以使用frame变量进行处理,例如显示、保存或进行图像处理操作 } } ``` 5. 处理完所有帧后,释放VideoCapture对象并关闭视频文件。例如: ```csharp // 释放VideoCapture对象并关闭视频文件 capture.Release(); ``` 通过以上步骤,就可以使用OpencvSharp打开MP4视频并进行操作了。可以根据实际需求来对视频帧进行处理,如显示到窗口、保存为图像文件或进行图像处理操作等。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值