简化日志中给出的命令行
This commit is contained in:
@@ -60,12 +60,32 @@ def set_progress(line: str, dur, pbar):
|
|||||||
return
|
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):
|
def start_ffmpeg(infile: Path, outfile: Path):
|
||||||
cmd = [
|
cmd = [
|
||||||
"ffmpeg", "-y", "-i", str(infile),
|
"ffmpeg", "-y", "-i", shorten_name(infile.name),
|
||||||
"-progress", "pipe:1", "-nostats", "-loglevel", "error",
|
"-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(
|
return subprocess.Popen(
|
||||||
cmd,
|
cmd,
|
||||||
stdout=subprocess.PIPE,
|
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):
|
def hevc_encode(infile: Path, outfile: Path, progress: bool = True):
|
||||||
dur = get_duration(infile)
|
dur = get_duration(infile)
|
||||||
logger.debug(f"Duration: {dur:.2f} seconds")
|
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(
|
pbar = get_progress_bar(
|
||||||
total=dur, unit='s',
|
total=dur, unit='s',
|
||||||
bar_format='{l_bar}{bar}| {n:.2f}/{total:.2f}({unit})'
|
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 (
|
except (
|
||||||
KeyboardInterrupt,
|
KeyboardInterrupt,
|
||||||
subprocess.CalledProcessError
|
subprocess.CalledProcessError
|
||||||
) as e:
|
):
|
||||||
logger.error(
|
# Issue: maybe also interrupted by CtrlC
|
||||||
f"Encoding failed with code {proc.returncode}:"
|
|
||||||
f" {e.__class__.__name__}")
|
|
||||||
clean_on_failure(proc, outfile)
|
clean_on_failure(proc, outfile)
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user