Add Avisynth video encoding workflow
This commit is contained in:
+51
-21
@@ -1,36 +1,69 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from shutil import which
|
||||
from typing import Any
|
||||
|
||||
from tools.executables import require_executable
|
||||
|
||||
|
||||
def require_exiftool() -> str:
|
||||
executable = which("exiftool")
|
||||
if executable:
|
||||
return executable
|
||||
raise RuntimeError(
|
||||
"exiftool was not found on PATH. Install ExifTool and make sure the "
|
||||
"'exiftool' command works before running this script."
|
||||
)
|
||||
return require_executable("exiftool")
|
||||
|
||||
|
||||
def run_exiftool_json(executable: str, paths: list[Path]) -> list[dict]:
|
||||
def _use_windows_argument_file() -> bool:
|
||||
return os.name == "nt"
|
||||
|
||||
|
||||
def run_exiftool_command(
|
||||
executable: str,
|
||||
args: list[str],
|
||||
**run_kwargs: Any,
|
||||
) -> subprocess.CompletedProcess:
|
||||
"""Run ExifTool with Unicode-safe filename arguments on Windows."""
|
||||
command = [executable, "-charset", "filename=UTF8", *args]
|
||||
if not _use_windows_argument_file():
|
||||
return subprocess.run(command, **run_kwargs)
|
||||
|
||||
# ExifTool can't reliably receive arbitrary Unicode paths on the Windows
|
||||
# command line. The outer path is ASCII and the UTF-8 argument file holds
|
||||
# the actual paths and options.
|
||||
with tempfile.TemporaryDirectory(prefix="mbt-exiftool-") as temp_dir:
|
||||
argument_file = Path(temp_dir) / "arguments.txt"
|
||||
argument_file.write_text("\n".join(args) + "\n", encoding="utf-8")
|
||||
return subprocess.run(
|
||||
[executable, "-charset", "filename=UTF8", "-@", argument_file.name],
|
||||
cwd=temp_dir,
|
||||
**run_kwargs,
|
||||
)
|
||||
|
||||
|
||||
def run_exiftool_json(
|
||||
executable: str,
|
||||
paths: list[Path],
|
||||
*,
|
||||
quicktime_utc: bool = True,
|
||||
) -> list[dict]:
|
||||
results: list[dict] = []
|
||||
for start in range(0, len(paths), 80):
|
||||
chunk = paths[start : start + 80]
|
||||
command = [
|
||||
executable,
|
||||
args = [
|
||||
"-j",
|
||||
"-G",
|
||||
"-api",
|
||||
"QuickTimeUTC=1",
|
||||
"-charset",
|
||||
"filename=UTF8",
|
||||
*[str(path) for path in chunk],
|
||||
]
|
||||
completed = subprocess.run(command, check=True, capture_output=True, text=True)
|
||||
if quicktime_utc:
|
||||
args.extend(["-api", "QuickTimeUTC=1"])
|
||||
args.extend(str(path) for path in chunk)
|
||||
completed = run_exiftool_command(
|
||||
executable,
|
||||
args,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
results.extend(json.loads(completed.stdout or "[]"))
|
||||
return results
|
||||
|
||||
@@ -39,11 +72,8 @@ def run_exiftool_write(executable: str, path: Path, args: list[str]) -> None:
|
||||
if not args:
|
||||
return
|
||||
command = [
|
||||
executable,
|
||||
"-overwrite_original",
|
||||
"-charset",
|
||||
"filename=UTF8",
|
||||
*args,
|
||||
str(path),
|
||||
]
|
||||
subprocess.run(command, check=True)
|
||||
run_exiftool_command(executable, command, check=True)
|
||||
|
||||
Reference in New Issue
Block a user