opencv videocapture

opencv videocapture

Guide

VideoCapture

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import datetime
import cv2
from moviepy.editor import VideoFileClip
import numpy as np

def get_video_info(video_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return

frame_number = cap.get(cv2.CAP_PROP_FRAME_COUNT)
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = int(cap.get(cv2.CAP_PROP_FPS))

cap.release() # release video capture

print("fps = ",fps)
print("frame_number = ",frame_number)
size = (w,h)
print("size = ",size)

# frame number MAY BE WRONG ! so video time may be also wrong!
duration = int(frame_number / fps)
print("seconds=",duration)
video_time = str(datetime.timedelta(seconds = duration))
print("video_time=",video_time)

print("-----------------------using VideoFileClip------------------")
clip = VideoFileClip(video_path)
duration = clip.duration
print("video duration is "+ str(duration) + " seconds")
video_time = str(datetime.timedelta(seconds = int(duration)))
print("video_time=",video_time)

def clip_video():
clip = VideoFileClip("1.mp4")
starting_point = 120 # start at second minute
end_point = 420 # record for 300 seconds (120+300)
subclip = clip.subclip(starting_point, end_point)
subclip.write_videofile("/path/to/new/video.mp4")

Reference

History

  • 20191006: created.
赞赏