Python library for easier management and processing of subtitle files.
Project description
Sublib
Python library for easier management and processing of subtitle files.
Installation
Currently, Sublib supports releases of Python 3.6 onwards. To install the current release:
pip install sublib
..or you can just download package files via GitHub.
Testing
Sublib uses unittest tests. Tests can be run with:
python -m unittest discover -s tests
Getting Started
To use the module you need to import it first:
import sublib
Detection of the subtitle format:
# If the format is unknown it will return 'undefined'
sub_format = sublib.detect("subtitle.srt", "utf-8")
Creation of the subtitle object:
# You can choose from: MPlayer2, SubRip, MicroDVD, TMPlayer
subtitle = sublib.SubRip("subtitle.srt", "utf-8")
Each subtitle object has two methods:
# Returns a list of lines in a universal format
# [[datetime.timedelta(...), datetime.timedelta(...), 'Line 01|Line 02], ...]
general = subtitle.get_general_format()
# Formats lines and adds them to an existing object
empty_subtitle = sublib.MPlayer2()
empty_subtitle.set_from_general_format(general)
..and several attributes:
subtitle.path # The file path you used to create the object
subtitle.encoding # The encoding you used to create the object
subtitle.content # The contents of the file as a list of lines
subtitle.format # The regex format of a specific type of subtitle
Boolean conversion:
# Empty object will return False
print(bool(subtitle))
# Object with content will return True
subtitle.set_from_general_format(general)
if subtitle:
print(subtitle.content)
Object content comparison:
# The contents of the 'content' attributes of each object are compared
if subtitle_1 != subtitle_2:
subtitle_2.content = subtitle_1.content
Return the number of lines in the file:
# In all formats except SubRip, this is the number of lines that will be displayed
print(len(subtitle))
Presence of a string in the subtitles may be check with 'in' statement:
# The individual lines are searched sequentially
if "some text" in subtitle:
return "Yes"
Iterating over the subtitle lines:
# The individual lines are searched sequentially
for line in subtitle:
print(line)
Supported formats
- as mpl - MPlayer2 (.txt)
- as srt - SubRip (.srt)
- as sub - MicroDVD (.sub)
- as tmp - TMPlayer (.txt)
Contributing
Pull requests are welcome.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.