视频转图片
视频转图片
输入文件夹路径,里面时mp4视频
输出的是每隔1秒提取到的视频帧
interval是每隔多少秒保存一张图片
python
import cv2
import os
img_count = 1
def save_frames(video_path, output_folder, interval):
# 创建保存图片的文件夹
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 打开视频文件
cap = cv2.VideoCapture(video_path)
frame_count = 0
global img_count
while cap.isOpened():
ret, frame = cap.read()
# 判断是否成功读取帧
if not ret:
break
frame_count += 1
# 每两秒保存一帧图片
if frame_count % (interval * cap.get(cv2.CAP_PROP_FPS)) == 0:
img_name = f"20240524_{img_count}.jpg"
img_path = os.path.join(output_folder, img_name)
cv2.imwrite(img_path, frame)
print('file saved:', img_path)
print(img_count)
img_count += 1
cap.release()
# 设置输入文件夹和输出文件夹路径
input_folder = "path"
output_folder = "path"
# 遍历文件夹中的所有.dav视频文件
for filename in os.listdir(input_folder):
if filename.endswith(".dav"):
video_path = os.path.join(input_folder, filename)
save_frames(video_path, output_folder, 1)
双目视频,提取单目
输入文件夹路径,里面时mp4视频
输出的是每隔1秒提取到的视频帧
img_name是视频帧名字的命名格式,自动编号
python
import cv2
import os
img_count = 1
def extract_left_eye_frame(video_path, output_folder):
global img_count
# 打开视频文件
cap = cv2.VideoCapture(video_path)
frame_rate = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
half_width = frame_width // 2
# img_count = 1
last_saved_time = 0.0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0 # 当前帧的时间戳(秒)
# 如果超过一秒钟才保存下一帧
if current_time - last_saved_time >= 1.0: # 1.0
# 提取左目图片
left_eye_frame = frame[:, :half_width]
# 提取右目图片
# right_eye_frame = frame[:,half_width:]
# 保存图片
img_name = f"20240422xiaomi_{img_count}.jpg"
img_path = os.path.join(output_folder, img_name)
cv2.imwrite(img_path, left_eye_frame)
print(f"Saved: {img_path}")
img_count += 1
last_saved_time = current_time
cap.release()
# 设置输入文件夹和输出文件夹路径
input_folder = "path"
output_folder = "path"
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历文件夹中的所有.mp4视频文件
for filename in os.listdir(input_folder):
if filename.endswith(".mp4"):
print(f"Processing: {filename}")
video_path = os.path.join(input_folder, filename)
extract_left_eye_frame(video_path, output_folder)