From 2dd084b10501c6b6e0f41d44756f62ef0223d574 Mon Sep 17 00:00:00 2001 From: TalviT Date: Thu, 2 Jun 2022 01:09:17 +0200 Subject: [PATCH] Add style support for write (VTT only) --- webvtt/webvtt.py | 4 ++-- webvtt/writers.py | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/webvtt/webvtt.py b/webvtt/webvtt.py index adec7c9..1e2c2e7 100644 --- a/webvtt/webvtt.py +++ b/webvtt/webvtt.py @@ -25,7 +25,7 @@ class WebVTT(object): def __init__(self, file='', captions=None, styles=None): self.file = file self._captions = captions or [] - self._styles = styles + self._styles = styles or [] def __len__(self): return len(self._captions) @@ -105,7 +105,7 @@ def save_as_srt(self, output=''): def write(self, f, format='vtt'): if format == 'vtt': - WebVTTWriter().write(self._captions, f) + WebVTTWriter().write(self._styles, self._captions, f) elif format == 'srt': SRTWriter().write(self._captions, f) # elif output_format == OutputFormat.SBV: diff --git a/webvtt/writers.py b/webvtt/writers.py index 5ec551b..af3a537 100644 --- a/webvtt/writers.py +++ b/webvtt/writers.py @@ -1,14 +1,18 @@ class WebVTTWriter(object): - def write(self, captions, f): - f.write(self.webvtt_content(captions)) + def write(self, styles, captions, f): + f.write(self.webvtt_content(styles, captions)) - def webvtt_content(self, captions): + def webvtt_content(self, styles, captions): """ Return captions content with webvtt formatting. """ output = ["WEBVTT"] + for style in styles: + output.append("") + output.append("STYLE") + output.append(style.text) for caption in captions: output.append("") if caption.identifier: