feat: 压缩前后体积对比
顺便也分离文件名冲突处理逻辑
This commit is contained in:
40
cmps_hevc_crf18.py
Executable file → Normal file
40
cmps_hevc_crf18.py
Executable file → Normal file
@@ -140,6 +140,23 @@ def make_output_path(infile: Path, outdir: Path = None, relroot: str = None):
|
|||||||
return outdir / rel_path
|
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():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog=itsme,
|
prog=itsme,
|
||||||
@@ -185,6 +202,7 @@ def main():
|
|||||||
errmsg = 'No input files provided'
|
errmsg = 'No input files provided'
|
||||||
if not files:
|
if not files:
|
||||||
raise SystemExit(errmsg)
|
raise SystemExit(errmsg)
|
||||||
|
if len(files) > 1:
|
||||||
files.sort(key=lambda x: x.stat().st_size)
|
files.sort(key=lambda x: x.stat().st_size)
|
||||||
|
|
||||||
for idx, infile in enumerate(files, 1):
|
for idx, infile in enumerate(files, 1):
|
||||||
@@ -198,21 +216,13 @@ def main():
|
|||||||
except (OSError, PermissionError) as e:
|
except (OSError, PermissionError) as e:
|
||||||
logger.error(f"Job failed: {e}")
|
logger.error(f"Job failed: {e}")
|
||||||
raise
|
raise
|
||||||
final_out = outfile.with_name(
|
|
||||||
outfile.name.replace(".hevc.mp4", ".mp4"))
|
isize = infile.stat().st_size / (1024 * 1024)
|
||||||
if final_out.resolve() == infile.resolve():
|
osize = outfile.stat().st_size / (1024 * 1024)
|
||||||
logger.warning(
|
logger.info(f"Size deductions: {osize - isize:+.2f} MB")
|
||||||
f"Filename conflict!\n i: {infile}\n o: {final_out}")
|
logger.debug(f" i: {isize:.2f} MB -> o: {osize:.2f} MB")
|
||||||
if not args.remove_original:
|
|
||||||
logger.debug("renaming original to avoid conflict.")
|
resolve_output(infile, outfile, args.remove_original)
|
||||||
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}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user