亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python+openCV對視頻進(jìn)行截取的實(shí)現(xiàn)

 更新時間:2020年11月27日 09:58:35   作者:ChristmasBoy  
這篇文章主要介紹了python+openCV對視頻進(jìn)行截取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用cv2對視頻進(jìn)行切割

import cv2


def clip_video(source_video, target_video, start_time, end_time):
  cap = cv2.VideoCapture(source_video)
  if not cap.isOpened():
    logger_warning('video is not opened')
  else:
    success, frame = cap.read()
    f_shape = frame.shape
    f_height = f_shape[0] # 原視頻圖片的高度
    f_width = f_shape[1]
    fps = cap.get(5) # 幀速率
    frame_number = cap.get(7) # 視頻文件的幀數(shù)
    duration = frame_number / fps # 視頻總幀數(shù)/幀速率 是時間/秒【總共有多少秒的視頻時間】
    if start_time > duration or end_time > duration:
      return
    start_time = fps * float(start_time)
    end_time = fps * float(end_time)
    # AVI格式編碼輸出 XVID
    four_cc = cv2.VideoWriter_fourcc(*'H264')
    video_writer = cv2.VideoWriter(target_video, four_cc, fps, (int(f_width), int(f_height)))
    num = 0
    while True:
      success, frame = cap.read()
      if int(start_time) <= int(num) <= int(end_time):
        if success:
          video_writer.write(frame)
        else:
          break
      num += 1
      if num > frame_number:
        break
    cap.release()

VideoWriter_fourcc編碼格式:

fourcc意為四字符代碼(Four-Character Codes),顧名思義,該編碼由四個字符組成,下面是VideoWriter_fourcc對象一些常用的參數(shù),注意:字符順序不能弄混
cv2.VideoWriter_fourcc('I', '4', '2', '0'),該參數(shù)是YUV編碼類型,文件名后綴為.avi
cv2.VideoWriter_fourcc('P', 'I', 'M', 'I'),該參數(shù)是MPEG-1編碼類型,文件名后綴為.avi
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'),該參數(shù)是MPEG-4編碼類型,文件名后綴為.avi
cv2.VideoWriter_fourcc('T', 'H', 'E', 'O'),該參數(shù)是Ogg Vorbis,文件名后綴為.ogv
cv2.VideoWriter_fourcc('F', 'L', 'V', '1'),該參數(shù)是Flash視頻,文件名后綴為.flv

到此這篇關(guān)于python+openCV對視頻進(jìn)行截取的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)openCV視頻截取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論