使用Python搭配ChatGPT寫一個縮時攝影軟體 好課程介紹: 小孩學Python程式的入門課:http://bit.ly/TeachYourKidsToCode 大人學Python的進階課:https://bit.ly/100DaysOfCodePython 參考影片:https://www.youtube.com/watch?v=w-X_EQ2Xva4
import cv2 import os # 設置輸入影片檔案的路徑 input_video = 'input_source.mp4' # 設置輸出影片檔案的路徑 output_video = 'output_timelapse.mp4' # 設置要跳過的幀數 frame_skip = 15 # 讀取輸入影片 cap = cv2.VideoCapture(input_video) # 確認影片是否正確讀取 if not cap.isOpened(): print('無法讀取影片檔案') exit() # 獲取影片的基本屬性 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 設置輸出影片的程式器和屬性 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_video, fourcc, frame_skip, (width, height)) # 循環處理每一幀並將其寫入輸出影片 frame_count = 0 while cap.isOpened(): ret, frame = cap.read() if not ret: break if frame_count % frame_skip == 0: out.write(frame) frame_count += 1 # 釋放資源並關閉視窗 cap.release() out.release() cv2.destroyAllWindows()