diff --git a/cmps_hevc_crf18.py b/cmps_hevc_crf18.py index ce39900..ade7d4e 100644 --- a/cmps_hevc_crf18.py +++ b/cmps_hevc_crf18.py @@ -60,12 +60,32 @@ def set_progress(line: str, dur, pbar): return +def shorten_name(filename: str, flag: int = 1): + stem, ext = filename.rsplit('.', 1) if '.' in filename else (filename, '') + stem = stem.replace(' ', '').upper() + ext = ext[:3].upper() + bstem = stem.encode(errors='ignore') + for i in range(8, 0, -1): + try: + cut = bstem[:i].decode() + if cut != stem: + cut += f'~{flag}' + stem = cut + break + except UnicodeDecodeError: + continue + return f'{stem}.{ext}' if ext else stem + + def start_ffmpeg(infile: Path, outfile: Path): cmd = [ - "ffmpeg", "-y", "-i", str(infile), + "ffmpeg", "-y", "-i", shorten_name(infile.name), "-progress", "pipe:1", "-nostats", "-loglevel", "error", - *COMPRESS_OPTIONS, str(outfile) + *COMPRESS_OPTIONS, shorten_name(outfile.name, flag=2) ] + logger.debug(f"Command: {' '.join(cmd)}") + cmd[3] = str(infile) + cmd[-1] = str(outfile) return subprocess.Popen( cmd, stdout=subprocess.PIPE, @@ -89,9 +109,8 @@ def clean_on_failure(proc: subprocess.Popen, outfile: Path): def hevc_encode(infile: Path, outfile: Path, progress: bool = True): dur = get_duration(infile) logger.debug(f"Duration: {dur:.2f} seconds") - proc = start_ffmpeg(infile, outfile) - logger.debug(f"Command: {proc.args}") + proc = start_ffmpeg(infile, outfile) pbar = get_progress_bar( total=dur, unit='s', bar_format='{l_bar}{bar}| {n:.2f}/{total:.2f}({unit})' @@ -109,10 +128,8 @@ def hevc_encode(infile: Path, outfile: Path, progress: bool = True): except ( KeyboardInterrupt, subprocess.CalledProcessError - ) as e: - logger.error( - f"Encoding failed with code {proc.returncode}:" - f" {e.__class__.__name__}") + ): + # Issue: maybe also interrupted by CtrlC clean_on_failure(proc, outfile) raise finally: