Skip to content

Commit

Permalink
adds srt_translation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Jan 16, 2024
1 parent f1cd828 commit eb1e560
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions transcribe_anything/srt_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Utilities for srt translation, including srt wrap.
"""

import subprocess
import warnings
from pathlib import Path
from typing import Any

from isolated_environment import isolated_environment # type: ignore

HERE = Path(__file__).parent
WRAP_SRT_PY = HERE / "srt_wrap.py"


def get_environment() -> dict[str, Any]:
"""Returns the environment."""
venv_dir = HERE / "venv" / "srttranslator"
env = isolated_environment(venv_dir, ["srtranslator==0.2.6"])
return env


def srt_wrap_to_string(srt_file: Path) -> str:
"""Wrap lines in a srt file."""
env = get_environment()
process = subprocess.run(
["python", str(WRAP_SRT_PY), str(srt_file)],
env=env,
capture_output=True,
text=True,
shell=True,
check=True,
)
out = process.stdout
return out


def srt_wrap(srt_file: Path) -> None:
"""Wrap lines in a srt file."""
try:
assert WRAP_SRT_PY.exists()
out = srt_wrap_to_string(srt_file)
srt_file.write_text(out, encoding="utf-8")
except subprocess.CalledProcessError as exc:
warnings.warn(f"Failed to run srt_wrap: {exc}")
return

0 comments on commit eb1e560

Please sign in to comment.