From a0417d87dbc4c01573a0d999117fe22ab9d82868 Mon Sep 17 00:00:00 2001 From: DawoudSheraz Date: Tue, 22 Sep 2020 21:07:03 +0500 Subject: [PATCH] add property to get webvtt content without needing a file --- docs/history.rst | 5 +++++ docs/usage.rst | 15 +++++++++++++++ tests/test_webvtt.py | 22 ++++++++++++++++++++++ webvtt/__init__.py | 2 +- webvtt/webvtt.py | 10 ++++++++++ webvtt/writers.py | 20 ++++++++++++++------ 6 files changed, 67 insertions(+), 7 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index e5bad12..46ce1bc 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -1,6 +1,11 @@ History ======= +0.4.6 +------------------ + +* Add capability to get WebVTT formatted content without an output file + 0.4.5 (09-04-2020) ------------------ diff --git a/docs/usage.rst b/docs/usage.rst index e85ae16..491bab9 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -119,6 +119,21 @@ Saving captions vtt.write(fd) +Fetching WebVTT formatted Captions +------------------------------------ + +WebVTT formatted captions content can be obtained without having to create an output file. + +.. code-block:: python + + import webvtt + + vtt = webvtt.read('captions.vtt') + + # Print the captions formatted in webvtt + print(vtt.content) + + Converting captions ------------------- diff --git a/tests/test_webvtt.py b/tests/test_webvtt.py index 2c633b0..9da3238 100644 --- a/tests/test_webvtt.py +++ b/tests/test_webvtt.py @@ -393,3 +393,25 @@ def test_save_updated_identifiers(self): ] self.assertListEqual(lines, expected_lines) + + def test_content_formatting(self): + """ + Verify that content property returns the correctly formatted webvtt. + """ + captions = [ + Caption('00:00:00.500', '00:00:07.000', ['Caption test line 1', 'Caption test line 2']), + Caption('00:00:08.000', '00:00:15.000', ['Caption test line 3', 'Caption test line 4']), + ] + expected_content = textwrap.dedent("""\ + WEBVTT + + 00:00:00.500 --> 00:00:07.000 + Caption test line 1 + Caption test line 2 + + 00:00:08.000 --> 00:00:15.000 + Caption test line 3 + Caption test line 4 + """).strip() + vtt = webvtt.WebVTT(captions=captions) + self.assertEqual(expected_content, vtt.content) diff --git a/webvtt/__init__.py b/webvtt/__init__.py index e0a17a9..b6239f4 100644 --- a/webvtt/__init__.py +++ b/webvtt/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.4.5' +__version__ = '0.4.6' from .webvtt import * from .segmenter import * diff --git a/webvtt/webvtt.py b/webvtt/webvtt.py index 139181d..adec7c9 100644 --- a/webvtt/webvtt.py +++ b/webvtt/webvtt.py @@ -131,3 +131,13 @@ def total_length(self): @property def styles(self): return self._styles + + @property + def content(self): + """ + Return webvtt content with webvtt formatting. + + This property is useful in cases where the webvtt content is needed + but no file saving on the system is required. + """ + return WebVTTWriter().webvtt_content(self._captions) diff --git a/webvtt/writers.py b/webvtt/writers.py index f4ae812..5ec551b 100644 --- a/webvtt/writers.py +++ b/webvtt/writers.py @@ -2,12 +2,20 @@ class WebVTTWriter(object): def write(self, captions, f): - f.write('WEBVTT\n') - for c in captions: - if c.identifier: - f.write('\n' + c.identifier) - f.write('\n{} --> {}\n'.format(c.start, c.end)) - f.writelines(['{}\n'.format(l) for l in c.lines]) + f.write(self.webvtt_content(captions)) + + def webvtt_content(self, captions): + """ + Return captions content with webvtt formatting. + """ + output = ["WEBVTT"] + for caption in captions: + output.append("") + if caption.identifier: + output.append(caption.identifier) + output.append('{} --> {}'.format(caption.start, caption.end)) + output.extend(caption.lines) + return '\n'.join(output) class SRTWriter(object):