# 功能:對視頻文件進行剪切。
# 剪切指定長度的視頻,選擇要裁剪的視頻,選擇開始時間點和停止時間點即可。
# 將處理后的視頻保存為output.avi文件
import cv2 # OpenCV import tkinter.filedialog # Python文件對話框 def samllVideoGif(_videoPath,_videoGifPath): # _videoPath = "C:\\Users\\sswc\\Desktop\\gai2\\public\\showPdf\\6f3db1f9-e247-4aa5-bca7-93bae62a0079.mp4" # _videoGifPath = "C:\\Users\\sswc\\Desktop\\gai2\\public\\showPdf\\6f3db1f9-e247-4aa5-bca7-93bae62a0079.avi" cap = cv2.VideoCapture(_videoPath) # 打開視頻文件 frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) # 獲得視頻文件的幀數 fps = cap.get(cv2.CAP_PROP_FPS) # 獲得視頻文件的幀率 width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # 獲得視頻文件的幀寬 height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # 獲得視頻文件的幀高 # 創建保存視頻文件類對象 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(_videoGifPath, fourcc, fps, (int(width), int(height))) # 計算視頻長度/s video_length = frames / fps # print('start and stop must < %.1f' % video_length) # 提示用戶輸入變量的范圍 # start = float(input('Input an start time/s:')) # stop = float(input('Input an stop time/s:')) # print('start and stop must < %.1f' % video_length) # 提示用戶輸入變量的范圍 start = 0 stop = 10 # 設置幀讀取的開始位置 cap.set(cv2.CAP_PROP_POS_FRAMES, start * fps) pos = cap.get(cv2.CAP_PROP_POS_FRAMES) # 獲得幀位置 while (pos <= stop * fps): ret, frame = cap.read() # 捕獲一幀圖像 out.write(frame) # 保存幀 pos = cap.get(cv2.CAP_PROP_POS_FRAMES) cap.release() out.release()