# !/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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156865.html