当前位置:   article > 正文

多线程视频抽帧 python_python读取视频帧多线程

python读取视频帧多线程

转载注明出处谢谢

# -*- coding: utf-8 -*-

import shutil
import numpy as np
import cv2
import os
from multiprocessing import Pool


def do_capture(src_path, path, name):
    output_path = path + os.sep + name
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    else:
        print("folder exists")


    cap = cv2.VideoCapture(src_path + os.sep + name)
    # videoFPS = int((cap.get(cv2.CAP_PROP_FPS)))
    # print(videoFPS)
    count = 1
    while True:
        ret, frame = cap.read()
        # show a frame
        if ret is True:
            # print(frame.shape)
            count = count + 1
            # print(count)
            if count % 12 == 0:  # 设置抽帧间隔,fps=25,每秒抽2帧
                cropped = frame[360:1080, 0:1920]  # 裁剪坐标为[y0:y1, x0:x1]
                output_name = os.path.join(output_path, str(int(count / 12)) + ".jpg")
                cv2.imwrite(output_name, cropped)
        else:
            break
    print("finished")
    cap.release()
    # cv2.destroyAllWindows()


if __name__ == "__main__":
    videos_src_path = r'E:\records\long'  # 提取图片的视频文件夹
    output_root_path = r'.\records_capture'  # 保存图片的路径

    # 视频列表
    videos_dir = list(filter(lambda x: x.endswith('mp4'), os.listdir(videos_src_path)))  # 筛选出mp4格式

    #多线程处理
    num_worker = 1
    file_list_arr = np.array_split(videos_dir, num_worker)
    p_no = 0
    pool = Pool(processes=num_worker)
    for video_name in videos_dir:  # 循环读取路径下的文件并筛选输出
        if os.path.splitext(video_name)[1] == ".mp4":  # 筛选mp4文件
            print(video_name)  # 输出所有的mp4文件
            # break
        	pool.apply(do_capture, args=(videos_src_path, output_root_path, video_name))
    pool.close()
    pool.join()
    print("done!")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/280223
推荐阅读
相关标签
  

闽ICP备14008679号