OpenCV-切割視頻

import cv2
# 讀入視頻文件
video_capture = cv2.VideoCapture('C:\\Users\\user\\Desktop\\Bing_S01E60_Wellies.mp4')  

# FPS
fps = video_capture.get(5)
print(video_capture.isOpened())
print("fps", video_capture.get(5))
# 總幀數
print("COUNT", video_capture.get(7))
#視頻的寬和高
size = (int(video_capture.get(3)), int(video_capture.get(4)))
# 每隔10秒截取一段
c = 10
# 當前幀
frame_index = 0
# 當前截取的第幾段
flag = 0
success, bgr_image = video_capture.read()
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
v = cv2.VideoWriter('Bing_S01E60_Wellies' + str(frame_index // (fps * c)) + '.avi', fourcc, fps, size)
while success:  # 循環讀取視頻幀


    cv2.imshow('frame', bgr_image)
    if ((frame_index // (fps * c)) % 2 == 0):
        # print(frame_index // (fps * c), video_capture.get(0))

        if v.isOpened():
            v.write(bgr_image)

    if frame_index == (fps * c) * flag * 2 + (fps * c):
        if v.isOpened():
            v.release()

    if frame_index == (fps * c) * flag * 2:
        v = cv2.VideoWriter('Bing_S01E60_Wellies' + str(frame_index // (fps * c)) + '.avi', fourcc, fps, size)
        flag += 1
    success, bgr_image = video_capture.read()
    frame_index = frame_index + 1

video_capture.release()
v.release()

video_capture.get(propId)可以獲得視頻信息,propId 從0-18
propId爲以下值:

CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently unsupported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend cur- rently)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章