Enhance progress when length is not avai
This commit is contained in:
@ -220,11 +220,11 @@ def process_video(video_path: Path, compress_dir:Optional[Path]=None ,update_fun
|
|||||||
global esti_data
|
global esti_data
|
||||||
use=None
|
use=None
|
||||||
sz=video_path.stat().st_size//(1024*1024)
|
sz=video_path.stat().st_size//(1024*1024)
|
||||||
if esti is not None or TRAIN:
|
# if esti is not None or TRAIN:
|
||||||
use = func(sz,True)
|
# use = func(sz,True)
|
||||||
logging.info(f"开始处理文件: {video_path.relative_to(root)},大小{sz}M,预计{fmt_time(use)}")
|
# logging.info(f"开始处理文件: {video_path.relative_to(root)},大小{sz}M,预计{fmt_time(use)}")
|
||||||
else:
|
# else:
|
||||||
logging.info(f"开始处理文件: {video_path.relative_to(root)},大小{sz}M")
|
# logging.info(f"开始处理文件: {video_path.relative_to(root)},大小{sz}M")
|
||||||
|
|
||||||
|
|
||||||
bgn=time()
|
bgn=time()
|
||||||
@ -285,13 +285,14 @@ def process_video(video_path: Path, compress_dir:Optional[Path]=None ,update_fun
|
|||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"执行 ffmpeg 命令时发生异常, 文件:{str(video_path_str)},cmd={' '.join(command)}",exc_info=e)
|
logging.error(f"执行 ffmpeg 命令时发生异常, 文件:{str(video_path_str)},cmd={' '.join(map(str,command))}",exc_info=e)
|
||||||
return use
|
return use
|
||||||
|
|
||||||
def traverse_directory(root_dir: Path):
|
def traverse_directory(root_dir: Path):
|
||||||
video_extensions = set(CFG["video_ext"])
|
video_extensions = set(CFG["video_ext"])
|
||||||
sm=None
|
sm=None
|
||||||
if esti is not None:
|
if esti is not None:
|
||||||
|
raise DeprecationWarning("不再支持训练模式")
|
||||||
logging.info(f"正在估算时间(当存在大量小文件时,估算值将会很离谱)")
|
logging.info(f"正在估算时间(当存在大量小文件时,估算值将会很离谱)")
|
||||||
sm = 0
|
sm = 0
|
||||||
for file in root_dir.rglob("*"):
|
for file in root_dir.rglob("*"):
|
||||||
@ -309,24 +310,27 @@ def traverse_directory(root_dir: Path):
|
|||||||
# logging.info("正在估算视频帧数,用于显示进度。")
|
# logging.info("正在估算视频帧数,用于显示进度。")
|
||||||
with Progress() as prog:
|
with Progress() as prog:
|
||||||
task = prog.add_task("正在获取视频信息",total=len(list(root_dir.rglob("*"))))
|
task = prog.add_task("正在获取视频信息",total=len(list(root_dir.rglob("*"))))
|
||||||
frames = {}
|
frames:dict[Path,float] = {}
|
||||||
for file in root_dir.rglob("*"):
|
for file in root_dir.rglob("*"):
|
||||||
prog.advance(task)
|
prog.advance(task)
|
||||||
if file.parent.name == "compress":continue
|
if file.parent.name == "Compress":continue
|
||||||
if file.is_file() and file.suffix.lower() in video_extensions:
|
if file.is_file() and file.suffix.lower() in video_extensions:
|
||||||
cmd = f'ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate,duration -of default=nokey=1:noprint_wrappers=1 "{str(file)}'
|
cmd = f'ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate,duration -of default=nokey=1:noprint_wrappers=1'.split()
|
||||||
|
cmd.append(str(file))
|
||||||
proc = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
proc = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
logging.error(f"无法获取视频信息: {file}, 返回码: {proc.returncode}")
|
logging.debug(f"无法获取视频信息: {file}, 返回码: {proc.returncode}")
|
||||||
frames[file] = 60
|
logging.debug(proc.stdout)
|
||||||
|
logging.debug(proc.stderr)
|
||||||
|
frames[file] = 0
|
||||||
continue
|
continue
|
||||||
if proc.stdout.strip():
|
if proc.stdout.strip():
|
||||||
avg_frame_rate, duration = proc.stdout.strip().split('\n')
|
avg_frame_rate, duration = proc.stdout.strip().split('\n')
|
||||||
tmp = avg_frame_rate.split('/')
|
tmp = avg_frame_rate.split('/')
|
||||||
avg_frame_rate = float(tmp[0]) / float(tmp[1])
|
avg_frame_rate = float(tmp[0]) / float(tmp[1])
|
||||||
if duration == "N/A":
|
if duration == "N/A":
|
||||||
duration = 1000
|
duration = 0
|
||||||
logging.error(f"无法获取视频信息: {file}, 时长为N/A,默认使用1000s。运行时进度条将出现异常。")
|
logging.debug(f"无法获取视频信息: {file}, 时长为N/A,默认使用0s。运行时进度条将出现异常。")
|
||||||
duration = float(duration)
|
duration = float(duration)
|
||||||
frames[file] = duration * avg_frame_rate
|
frames[file] = duration * avg_frame_rate
|
||||||
|
|
||||||
@ -336,15 +340,23 @@ def traverse_directory(root_dir: Path):
|
|||||||
|
|
||||||
with Progress() as prog:
|
with Progress() as prog:
|
||||||
task = prog.add_task("总进度",total=sm if sm is not None else sum(frames.values()))
|
task = prog.add_task("总进度",total=sm if sm is not None else sum(frames.values()))
|
||||||
for file in root_dir.rglob("*"):
|
for file in frames.keys():
|
||||||
if file.parent.name == "compress":continue
|
# if "Compress" in file.relative_to(root_dir).parents:continue
|
||||||
if file.is_file() and file.suffix.lower() in video_extensions:
|
if file.is_file() and file.suffix.lower() in video_extensions:
|
||||||
cur = prog.add_task(f"{file.relative_to(root_dir)}",total=frames[file])
|
filename = file.relative_to(root_dir)
|
||||||
|
if frames[file] == 0:
|
||||||
|
logging.warning("当前文件时长获取失败,进度条持续为0为正常现象。")
|
||||||
|
cur = prog.add_task(f"{filename}")
|
||||||
|
else:
|
||||||
|
cur = prog.add_task(f"{filename}",total=frames[file])
|
||||||
with prog._lock:
|
with prog._lock:
|
||||||
tmp = prog._tasks[task]
|
tmp = prog._tasks[task]
|
||||||
completed_start = tmp.completed
|
completed_start = tmp.completed
|
||||||
|
|
||||||
def update_progress(x):
|
def update_progress(x):
|
||||||
|
if frames[file] == 0:
|
||||||
|
prog.update(cur,description=f"{filename} 已处理{x}帧")
|
||||||
|
else:
|
||||||
prog.update(cur,completed=x)
|
prog.update(cur,completed=x)
|
||||||
prog.update(task, completed=completed_start+x)
|
prog.update(task, completed=completed_start+x)
|
||||||
|
|
||||||
@ -372,7 +384,7 @@ def test():
|
|||||||
exit(-1)
|
exit(-1)
|
||||||
try:
|
try:
|
||||||
ret = subprocess.run(
|
ret = subprocess.run(
|
||||||
"ffmpeg -hide_banner -f lavfi -i testsrc=duration=1:size=1920x1080:rate=30 -c:v libx264 -y -pix_fmt yuv420p compress_video_test.mp4",
|
"ffmpeg -hide_banner -f lavfi -i testsrc=duration=1:size=1920x1080:rate=30 -c:v libx264 -y -pix_fmt yuv420p compress_video_test.mp4".split(),
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
text=True
|
text=True
|
||||||
|
Reference in New Issue
Block a user