将一个文件夹里面的视频批量转换为图片并保存在同一目录下

import os
import cv2

cut_frame = 1  # 多少帧截一次,自己设置就行
save_path = "/media/gpu/00758835-1a92-4611-8ae4-1518102a53d6/coding/pythonProject/pic"

for root, dirs, files in os.walk(r"/media/gpu/00758835-1a92-4611-8ae4-1518102a53d6/coding/pythonProject/videos"):  # 这里就填文件夹目录就可以了
    for file in files:
        # 获取文件路径
        if ('.mp4' in file):
            path = os.path.join(root, file)
            video = cv2.VideoCapture(path)
            video_fps = int(video.get(cv2.CAP_PROP_FPS))
            print(video_fps)
            current_frame = 0
            while (True):
                ret, image = video.read()
                current_frame = current_frame + 1
                if ret is False:
                    video.release()
                    break
                if current_frame % cut_frame == 0:
                    # cv2.imwrite(save_path + '/' + file[:-4] + str(current_frame) + '.jpg',
                    #             image)  # file[:-4]是去掉了".mp4"后缀名,这里我的命名格式是,视频文件名+当前帧数+.jpg,使用imwrite就不能有中文路径和中文文件名
                    cv2.imencode('.jpg', image)[1].tofile(save_path + '/' + file[:-4] + str(current_frame) + '.jpg') #使用imencode就可以整个路径中可以包括中文,文件名也可以是中文
                    print('正在保存' + file + save_path + '/' + file[:-4] + str(current_frame))
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值