Skip to content

Commit

Permalink
Add Subtitle.language_type enum and add Embedded subtitle class (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze authored Jul 9, 2024
1 parent 1e9177e commit 96c0165
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/1148.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add language_type attribute to Subtitle
86 changes: 83 additions & 3 deletions subliminal/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
from codecs import BOM_UTF8, BOM_UTF16_BE, BOM_UTF16_LE, BOM_UTF32_BE, BOM_UTF32_LE
from enum import Enum
from typing import TYPE_CHECKING, ClassVar

import chardet
Expand Down Expand Up @@ -43,6 +44,16 @@
)


#: Subtitle language types
class LanguageType(Enum):
"""Subtitle language types."""

UNKNOWN = 'unknown'
FORCED = 'forced'
NORMAL = 'normal'
HEARING_IMPAIRED = 'hearing_impaired'


class Subtitle:
"""Base class for subtitle.
Expand Down Expand Up @@ -81,7 +92,7 @@ class Subtitle:
subtitle_id: str

#: Whether or not the subtitle is hearing impaired (None if unknown)
hearing_impaired: bool | None
language_type: LanguageType

#: URL of the web page from which the subtitle can be downloaded
page_link: str | None
Expand All @@ -95,16 +106,21 @@ class Subtitle:
#: Framerate for frame-based formats (MicroDVD)
fps: float | None

#: Whether the subtitle is embedded in the video or an external file
embedded: bool

def __init__(
self,
language: Language,
subtitle_id: str = '',
*,
hearing_impaired: bool | None = None,
forced: bool | None = None,
page_link: str | None = None,
encoding: str | None = None,
subtitle_format: str | None = None,
fps: float | None = None,
embedded: bool = False,
guess_encoding: bool = True,
) -> None:
self._content = None
Expand All @@ -115,10 +131,20 @@ def __init__(

self.language = language
self.subtitle_id = subtitle_id
self.hearing_impaired = hearing_impaired
self.page_link = page_link
self.subtitle_format = subtitle_format
self.fps = fps
self.embedded = embedded

self.language_type = LanguageType.UNKNOWN
if hearing_impaired:
self.language_type = LanguageType.HEARING_IMPAIRED
elif forced:
self.language_type = LanguageType.FORCED
# if hearing_impaired or forced is specified to be False
# then for sure the subtitle is normal.
elif hearing_impaired is False or forced is False:
self.language_type = LanguageType.NORMAL

self.encoding = None
# validate the encoding
Expand All @@ -136,7 +162,25 @@ def id(self) -> str:
@property
def info(self) -> str:
"""Info of the subtitle, human readable. Usually the subtitle name for GUI rendering."""
return ''
return self.id

@property
def hearing_impaired(self) -> bool | None:
"""Whether the subtitle is for hearing impaired."""
if self.language_type == LanguageType.HEARING_IMPAIRED:
return True
if self.language_type == LanguageType.UNKNOWN:
return None
return False

@property
def forced(self) -> bool | None:
"""Whether the subtitle is a forced subtitle."""
if self.language_type == LanguageType.FORCED:
return True
if self.language_type == LanguageType.UNKNOWN:
return None
return False

@property
def content(self) -> bytes | None:
Expand Down Expand Up @@ -414,6 +458,42 @@ def __repr__(self) -> str:
return f'<{self.__class__.__name__} {self.id!r} [{self.language}]>'


class EmbeddedSubtitle(Subtitle):
"""Embedded subtitle."""

def __init__(
self,
language: Language,
*,
hearing_impaired: bool | None = None,
forced: bool | None = None,
encoding: str | None = None,
subtitle_format: str | None = None,
) -> None:
subtitle_id = f'Embedded {language}'

super().__init__(
language,
subtitle_id,
hearing_impaired=hearing_impaired,
forced=forced,
encoding=encoding,
subtitle_format=subtitle_format,
embedded=True,
)

@property
def info(self) -> str:
"""Info of the subtitle, human readable. Usually the subtitle name for GUI rendering."""
extra = ''
if self.language_type == LanguageType.HEARING_IMPAIRED:
extra = ' [hi]'
elif self.language_type == LanguageType.FORCED:
extra = ' [forced]'

return f'{self.id}{extra}'


def get_subtitle_format(
text: str,
subtitle_format: str | None = None,
Expand Down

0 comments on commit 96c0165

Please sign in to comment.