| 123456789101112131415161718192021222324252627282930313233343536 |
- import cv2
- def get_frame_at_time(video_path, time_ms, output_image_path):
- """
- 获取视频指定毫秒数的帧
- :param video_path: 视频文件路径
- :param time_ms: 需要获取的时间点,单位为毫秒
- :param output_image_path: 保存帧的路径
- """
- cap = cv2.VideoCapture(video_path)
- # 设置视频的位置到指定的毫秒数 [5]
- cap.set(cv2.CAP_PROP_POS_MSEC, time_ms)
- # 读取帧 [2]
- ret, frame = cap.read()
- if ret:
- # 保存图像
- cv2.imwrite(output_image_path, frame)
- print(f"Frame at {time_ms}ms saved to {output_image_path}")
- else:
- print("Failed to read frame")
- cap.release()
- if __name__ == '__main__':
- video_path = r"C:\Code\ML\Video\直播数据\video\vortexcards.mp4"
- hours = 1
- minutes = 53
- seconds = 22
- time_in_ms = (hours * 3600 + minutes * 60 + seconds) * 1000
- # 示例:获取视频的第 5000 毫秒(5秒)的帧
- get_frame_at_time(video_path, time_in_ms, "frame.jpg")
|