Skip to content

Commit

Permalink
add property to get webvtt content without needing a file
Browse files Browse the repository at this point in the history
  • Loading branch information
DawoudSheraz committed Nov 13, 2020
1 parent 3f4a51d commit a0417d8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
15 changes: 15 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------

Expand Down
22 changes: 22 additions & 0 deletions tests/test_webvtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion webvtt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.4.5'
__version__ = '0.4.6'

from .webvtt import *
from .segmenter import *
Expand Down
10 changes: 10 additions & 0 deletions webvtt/webvtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 14 additions & 6 deletions webvtt/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a0417d8

Please sign in to comment.