Created
July 31, 2021 05:56
-
-
Save SadmanTariq/34209adfeb29325948edc553da7e3196 to your computer and use it in GitHub Desktop.
Burn .ass subs to video for an entire directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Usage: python burnsubs.py <source> <destination> | |
This script extracts ass subs from all mkv videos in a source directory then | |
burns them to the video and outputs it to the destination directory. | |
Ffmpeg is required. There are some unix specific commands in the script but | |
it shouldn't be too difficult to adapt to windows. | |
""" | |
import subprocess | |
from random import randrange | |
from pathlib import Path | |
from sys import argv | |
def main(): | |
if len(argv) != 3: | |
print_help() | |
return | |
source = Path(argv[1]) | |
destination = Path(argv[2]) | |
if not source.is_dir() or (destination.exists() and | |
not destination.is_dir()): | |
print_help() | |
return | |
subs_dir = extract_subs(source) | |
try: | |
burn_subs(source, destination, subs_dir) | |
finally: | |
subprocess.run(['rm', '-r', str(subs_dir)]) # the rm command is unix specific. | |
def print_help(): | |
print("Usage: python burnsubs.py <source_directory> <destination_directory>") # noqa | |
def extract_subs(source: Path): | |
sub_dir = Path(f'/tmp/extracted_ass-{randrange(999)}') | |
while sub_dir.exists(): | |
sub_dir = Path(f'/tmp/extracted_ass-{randrange(999)}') # /tmp is unix specific. Change to C:\Windows\Temp for windows. | |
sub_dir.mkdir() | |
vids = list(source.glob('*.mkv')) | |
print("Extracting ass...") # hehe | |
for i, vid in enumerate(vids): | |
ass_path = sub_dir / (vid.stem + '.ass') | |
subprocess.run(['ffmpeg', '-loglevel', '-8', '-i', | |
str(vid), str(ass_path)]).check_returncode() | |
print(f'{i+1}/{len(list(vids))}') | |
print(f" Finished extracting subs to {sub_dir}.") | |
return sub_dir | |
def burn_subs(source: Path, destination: Path, subs: Path): | |
if not destination.exists(): | |
destination.mkdir() | |
# Here you can specify file type. | |
vids = list(source.glob('*.mkv')) | |
print("\nBurning subs...") | |
for i, vid in enumerate(vids): | |
out = destination/('incomplete' + vid.suffix) | |
# Feel free to play with ffmpeg settings. Change ultrafast to superfast/fast for | |
# better but slower compression. Change tune from animation to anything else | |
# depending on needs. | |
subprocess.run(['ffmpeg', '-i', str(vid), | |
'-vf', f"ass='{subs/(vid.stem+'.ass')}", | |
'-preset', 'ultrafast', '-tune', 'animation', | |
str(out)]).check_returncode() | |
out.rename(out.parent/vid.name) | |
print(f'{i+1}/{len(list(vids))}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment