diff --git a/utils/utilities.py b/utils/utilities.py index d8eba27..6ddc602 100644 --- a/utils/utilities.py +++ b/utils/utilities.py @@ -15,6 +15,14 @@ import requests from bs4 import BeautifulSoup from pywidevine.device import Device, DeviceTypes from rich.console import Console +from subby import ( + CommonIssuesFixer, + SMPTEConverter, + WebVTTConverter, + ISMTConverter, + WVTTConverter, + SAMIConverter, +) from unidecode import unidecode console = Console() @@ -283,6 +291,25 @@ def add_subtitles(soup: object, subtitle: str, language: str = None) -> object: return soup +def convert_subtitles(tmp: Path, filename: str, sub_type: str) -> Path: + converters = { + "vtt": WebVTTConverter(), + "ttml": SMPTEConverter(), + "mp4": WVTTConverter(), + "dfxp": ISMTConverter(), + "sami": SAMIConverter(), + } + + converter = converters[sub_type] + fixer = CommonIssuesFixer() + + file = Path(tmp / f"{filename}.{sub_type}") + srt, _ = fixer.from_srt(converter.from_file(file)) + output = Path(tmp / f"{filename}.srt") + srt.save(output) + return output + + def from_mpd(mpd_data: str, url: str = None): root = ET.fromstring(mpd_data) items = []