Last active
September 7, 2022 02:12
-
-
Save hldr4/ffa6b7c5ada356de1d09bea8b97e5d5f to your computer and use it in GitHub Desktop.
Small script to extract subtitles from an mkv/s container
This file contains hidden or 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
""" | |
Usage: mkse.py [path_to_container] | |
Requirements: pymediainfo module, mkvextract in system path | |
""" | |
import subprocess | |
import sys | |
import re | |
from pathlib import Path | |
from pymediainfo import MediaInfo | |
file = Path(sys.argv[1]) | |
tracks = MediaInfo.parse(file).text_tracks | |
codec_map = {'UTF8': 'srt', 'PGS': 'sup'} | |
filter_ = r'[\/\\*?|":><]' | |
print('\nExtracting...') | |
for i, track in enumerate(tracks): | |
ext = track.codec_id.split('/')[1] | |
subprocess.run(['mkvextract', file, 'tracks', | |
f'''{i}:{track.language}{f"_{re.sub(filter_, '_', track.title)}" | |
if track.title else ""}.{codec_map.get(ext, ext.lower())}'''], | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
print('\nDone') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment