Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MKV container option #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
.DS_Store
*.egg-info
auto_subtitle/__pycache__
auto_subtitle/__pycache__
build
18 changes: 14 additions & 4 deletions auto_subtitle/cli.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def main():
choices=whisper.available_models(), help="name of the Whisper model to use")
parser.add_argument("--output_dir", "-o", type=str,
default=".", help="directory to save the outputs")
parser.add_argument("--output_mkv", type=str2bool, default=False,
help="whether to output the new subtitled video as an .mkv container (True) or an .mp4 container (False)")
parser.add_argument("--output_mkv", type=str2bool, default=False,
help="whether to output the new subtitled video as an .mkv container (True) or an .mp4 container (False)")
parser.add_argument("--output_srt", type=str2bool, default=False,
help="whether to output the .srt file along with the video files")
parser.add_argument("--srt_only", type=str2bool, default=False,
Expand All @@ -34,6 +38,7 @@ def main():
output_srt: bool = args.pop("output_srt")
srt_only: bool = args.pop("srt_only")
language: str = args.pop("language")
output_mkv: bool = args.pop("output_mkv")

os.makedirs(output_dir, exist_ok=True)

Expand All @@ -54,17 +59,22 @@ def main():
if srt_only:
return

ext = "mkv" if output_mkv else "mp4"
for path, srt_path in subtitles.items():
out_path = os.path.join(output_dir, f"{filename(path)}.mp4")
out_path = os.path.join(output_dir, f"{filename(path)}.{ext}")

print(f"Adding subtitles to {filename(path)}...")

video = ffmpeg.input(path)
audio = video.audio

ffmpeg.concat(
video.filter('subtitles', srt_path, force_style="OutlineColour=&H40000000,BorderStyle=3"), audio, v=1, a=1
).output(out_path).run(quiet=True, overwrite_output=True)
if output_mkv:
srt = ffmpeg.input(srt_path)
ffmpeg.output(srt, video, out_path, vcodec='copy', scodec='copy').run(quiet=False, overwrite_output=True)
else:
ffmpeg.concat(
video.filter('subtitles', srt_path, force_style="OutlineColour=&H40000000,BorderStyle=3"), audio, v=1, a=1
).output(out_path).run(quiet=False, overwrite_output=True)

print(f"Saved subtitled video to {os.path.abspath(out_path)}.")

Expand Down