feat: 压缩前后体积对比

顺便也分离文件名冲突处理逻辑
This commit is contained in:
2026-04-20 13:57:00 +08:00
parent ef7965d274
commit b01bc0145b

42
cmps_hevc_crf18.py Executable file → Normal file
View File

@@ -140,6 +140,23 @@ def make_output_path(infile: Path, outdir: Path = None, relroot: str = None):
return outdir / rel_path
def resolve_output(infile: Path, outfile: Path, rm_original: bool = False):
final_out = outfile.with_name(
outfile.name.replace(".hevc.mp4", ".mp4"))
is_path_conflict = final_out.resolve() == infile.resolve()
if is_path_conflict:
logger.warning(
f"Filename conflict!\n i: {infile}\n o: {final_out}")
if not rm_original:
logger.debug("Renaming original to avoid conflict.")
infile.rename(infile.with_stem(infile.stem + '.bak'))
logger.debug(f"Output file: {final_out}")
outfile.replace(final_out)
if rm_original and not is_path_conflict:
logger.debug(f"Remove original: {infile}")
infile.unlink()
def parse_args():
parser = argparse.ArgumentParser(
prog=itsme,
@@ -185,7 +202,8 @@ def main():
errmsg = 'No input files provided'
if not files:
raise SystemExit(errmsg)
files.sort(key=lambda x: x.stat().st_size)
if len(files) > 1:
files.sort(key=lambda x: x.stat().st_size)
for idx, infile in enumerate(files, 1):
logger.info(f"[{idx}/{len(files)}] {infile.name}")
@@ -198,21 +216,13 @@ def main():
except (OSError, PermissionError) as e:
logger.error(f"Job failed: {e}")
raise
final_out = outfile.with_name(
outfile.name.replace(".hevc.mp4", ".mp4"))
if final_out.resolve() == infile.resolve():
logger.warning(
f"Filename conflict!\n i: {infile}\n o: {final_out}")
if not args.remove_original:
logger.debug("renaming original to avoid conflict.")
infile.rename(infile.with_stem(infile.stem + '.bak'))
outfile.replace(final_out)
else:
outfile.replace(final_out)
if args.remove_original:
logger.debug(f"Remove original: {infile}")
infile.unlink()
logger.debug(f"Output file: {final_out}")
isize = infile.stat().st_size / (1024 * 1024)
osize = outfile.stat().st_size / (1024 * 1024)
logger.info(f"Size deductions: {osize - isize:+.2f} MB")
logger.debug(f" i: {isize:.2f} MB -> o: {osize:.2f} MB")
resolve_output(infile, outfile, args.remove_original)
if __name__ == '__main__':