python+openCV對視訊進行擷取的實現
阿新 • • 發佈:2020-11-28
使用cv2對視訊進行切割
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) # 視訊檔案的幀數 duration = frame_number / fps # 視訊總幀數/幀速率 是時間/秒【總共有多少秒的視訊時間】 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物件一些常用的引數,注意:字元順序不能弄混
cv2.VideoWriter_fourcc('I','4','2','0'),該引數是YUV編碼型別,檔名字尾為.avi
cv2.VideoWriter_fourcc('P','I','M','I'),該引數是MPEG-1編碼型別,檔名字尾為.avi
cv2.VideoWriter_fourcc('X','V','D'),該引數是MPEG-4編碼型別,檔名字尾為.avi
cv2.VideoWriter_fourcc('F','L','1'),該引數是Flash視訊,檔名字尾為.flv
到此這篇關於python+openCV對視訊進行擷取的實現的文章就介紹到這了,更多相關openCV視訊擷取內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!