Examples

As will be shown in the following examples, the standard way to use the PySubLib library is pretty straightforward.

Detecting subtitle file format

To detect a subtitle file format, use the detect_format() function as shown below:

>>> import pysublib
>>> format = pysublib.detect_format("/path/to/subtitle/file.srt")
>>> print format
SubRip

Reading subtitle files

To read a subtitle file, use the read() function as shown in the following example:

>>> import pysublib
>>> subs = pysublib.read("/path/to/file.ext")
>>> for sub in subs:
...     print sub

Writing subtitle files

To write a subtitle file, use the SubtitleFile.write() method. In the following example, a subtitle file is parsed and all the subtitles starting from the 10th subtitle are shifted by 2 seconds. After that, it is written back to the subtitle file using the same format.

>>> from datetime import timedelta
>>> import pysublib
>>> doc = pysublib.read("/path/to/subtitle/file.srt")
>>> doc.shift(timedelta(seconds=2), 9)
>>> doc.file.write()

Table Of Contents

Previous topic

pysublib.formats — Objects representing individual subtitle files

This Page