From b01bc0145b4349aa5bbce2b00505ef567a4ce30e Mon Sep 17 00:00:00 2001 From: "SilverAg.L" Date: Mon, 20 Apr 2026 13:57:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8E=8B=E7=BC=A9=E5=89=8D=E5=90=8E?= =?UTF-8?q?=E4=BD=93=E7=A7=AF=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 顺便也分离文件名冲突处理逻辑 --- cmps_hevc_crf18.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) mode change 100755 => 100644 cmps_hevc_crf18.py diff --git a/cmps_hevc_crf18.py b/cmps_hevc_crf18.py old mode 100755 new mode 100644 index 205372f..ce39900 --- a/cmps_hevc_crf18.py +++ b/cmps_hevc_crf18.py @@ -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__':