From 77976c7e746c5f99522308f55e62df979ad047ec Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Sat, 2 Mar 2024 15:25:30 +0000 Subject: [PATCH] feat(Subtitle): Convert from fTTML->TTML & fVTT->WebVTT post-download --- devine/core/tracks/subtitle.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/devine/core/tracks/subtitle.py b/devine/core/tracks/subtitle.py index 036dc10..7a64e43 100644 --- a/devine/core/tracks/subtitle.py +++ b/devine/core/tracks/subtitle.py @@ -4,11 +4,13 @@ import re import subprocess from collections import defaultdict from enum import Enum +from functools import partial from io import BytesIO from pathlib import Path from typing import Any, Callable, Iterable, Optional import pycaption +import requests from construct import Container from pycaption import Caption, CaptionList, CaptionNode, WebVTTReader from pycaption.geometry import Layout @@ -147,6 +149,21 @@ class Subtitle(Track): track_name += flag return track_name or None + def download( + self, + session: requests.Session, + prepare_drm: partial, + progress: Optional[partial] = None + ): + super().download(session, prepare_drm, progress) + if not self.path: + return + + if self.codec == Subtitle.Codec.fTTML: + self.convert(Subtitle.Codec.TimedTextMarkupLang) + elif self.codec == Subtitle.Codec.fVTT: + self.convert(Subtitle.Codec.WebVTT) + def convert(self, codec: Subtitle.Codec) -> Path: """ Convert this Subtitle to another Format.