多线程抽帧软件

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@contact: JHC000abc@gmail.com
@file: ffmpeg_many.py
@time: 2023/2/8 23:42 $
@desc:

"""
import os
import re
from cup.util import ThreadPool


def make_path(path,file,in_path):
    """
    :param path:
    :return:
    """
    _name = os.path.split(file)[-1]
    name = _name.split(".")[0]
    _path = "\\".join(file.replace(in_path,"").replace(_name,"").split(os.path.sep))
    # print("_path",_path)
    if _path == "\\":
        # print("True")
        _new_path = os.path.join(path, name)
    else:
        _new_path = os.path.join(path,re.sub("^\\\\","",_path),name)
    # print("_new_path",_new_path)
    os.makedirs(_new_path, exist_ok=True)
    return _new_path,name

def split_video(dic):
    """
    所有截取
    :param file:
    :param num:
    :param out_name:
    :return:
    """
    # print(dic)
    file, num, out_path,in_path = dic["file"],dic["num"],dic["out_path"],dic["in_path"]
    new_path,name = make_path(out_path,file,in_path)
    # print(out_name)
    # 每秒抽num帧

    cmd = "ffmpeg.exe -y -i {} -r {} {}\{}_%4d.jpg".format(file, num, new_path,name)
    print(cmd)
    return run(cmd)


def run(cmd, retry=3):
    """
    :param cmd:
    :param retry:
    :return:
    """
    flag = False
    while not flag and retry > 0:
        try:
            os.system(cmd)
            flag = True
            return "视频{}抽帧完成".format(re.findall("-i (.*?) -r",cmd)[0])
            # compute_progress_and_send_progress(process)
        except Exception as e:
            print(e, e.__traceback__.tb_lineno)
            retry -= 1
            return "视频{}error".format(re.findall("-i (.*?) -r",cmd)[0])


def get_files_under_path(path):
    """
    获取路径下所有文件的绝对路径
    :param path: 根路径
    :return: 路径下文件绝对路径列表
    """
    try:
        if os.path.exists(path) and os.path.isabs(path):
            files_path_lis = []
            for path, dir_lis, file_lis in os.walk(path):
                if len(file_lis) > 0:
                    for file in file_lis:
                        files_path_lis.append(os.path.join(path, file))
            return files_path_lis
    except Exception as e:
        print(e, e.__traceback__.tb_lineno)



if __name__ == '__main__':
    _path = input("输入待抽帧视频路径:")
    num = input("输入每秒抽取帧数:")
    out_path = input("输入输出路径:")
    together_num = input("输入同时抽帧视频数:(数越大电脑越卡,直接回车默认值为5)")
    if together_num == "":
        together_num = 5
    else:
        if together_num == "1":
            together_num = 2
        else:
            together_num = int(together_num)
    # print(together_num)
    t = ThreadPool(minthreads=1,maxthreads=together_num,daemon_threads=True)
    t.start()
    for file in get_files_under_path(_path):
        if os.path.split(file)[-1].split(".")[-1] == "mp4":
            args = {"file":file,"num":num,"out_path":out_path,"in_path":_path}
            t.add_1job(split_video,
                       args
                       )
    t.stop()





软件下载地址:https://download.csdn.net/download/CXY00000/87462180?spm=1001.2014.3001.5503

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值