Skip to main content

A tiny library for parsing, modifying, and composing SRT files.

Project description

Tests Coverage

srt is a tiny but featureful Python library for parsing, modifying, and composing SRT files. Take a look at the quickstart for a basic overview of the library. Detailed API documentation is also available.

Want to see some examples of its use? Take a look at the tools shipped with the library. This library is also used internally by projects like subsync, NVIDIA RAD-TTS, manim, kinobot, bw_plex, and many more.

Why choose this library?

  • Can parse many broken SRT files which other SRT libraries cannot, and fix them

  • Extremely lightweight, ~200 lines of code excluding docstrings

  • Simple, intuitive API

  • High quality test suite using Hypothesis

  • 100% test coverage (including branches)

  • Well documented API, at both a high and low level

  • ~30% faster than pysrt on typical workloads

  • Full support for PyPy

  • No dependencies outside of the standard library

  • Tolerant of many common errors found in real-world SRT files

  • Support for Asian-style SRT formats (ie. “fullwidth” SRT format)

  • Completely Unicode compliant

  • Released under a highly permissive license (MIT)

  • Real world tested — used in production to process thousands of SRT files every day

  • Portable — runs on Linux, OSX, and Windows

  • Tools included — contains lightweight tools to perform generic tasks with the library

Usage

Tools

There are a number of tools shipped with the library to manipulate, process, and fix SRT files. Here’s an example using hanzidentifier to strip out non-Chinese lines:

$ cat pe.srt
1
00:00:33,843 --> 00:00:38,097
Only 3% of the water on our planet is fresh.
地球上只有3%的水是淡水

2
00:00:40,641 --> 00:00:44,687
Yet, these precious waters are rich with surprise.
可是这些珍贵的淡水中却充满了惊奇

$ srt lines-matching -m hanzidentifier -f hanzidentifier.has_chinese -i pe.srt
1
00:00:33,843 --> 00:00:38,097
地球上只有3%的水是淡水

2
00:00:40,641 --> 00:00:44,687
可是这些珍贵的淡水中却充满了惊奇

These tools are easy to chain together, for example, say you have one subtitle with Chinese and English, and other with French, but you want Chinese and French only. Oh, and the Chinese one is 5 seconds later than it should be. That’s easy enough to sort out:

$ srt lines-matching -m hanzidentifier -f hanzidentifier.has_chinese -i chs+eng.srt |
>     srt fixed-timeshift --seconds -5 |
>     srt mux --input - --input fra.srt

See the srt_tools/ directory for more information.

Library

Detailed API documentation is available, but here are the basics.

Here’s how you convert SRT input to Subtitle objects which you can manipulate:

>>> data = '''\
1
00:00:33,843 --> 00:00:38,097
地球上只有3%的水是淡水

2
00:00:40,641 --> 00:00:44,687
可是这些珍贵的淡水中却充满了惊奇

3
00:00:57,908 --> 00:01:03,414
所有陆地生命归根结底都依赖於淡水

'''
>>> for sub in srt.parse(data):
...     print(sub)
Subtitle(index=1, start=datetime.timedelta(seconds=33, microseconds=843000), end=datetime.timedelta(seconds=38, microseconds=97000), content='地球上只有3%的水是淡水', proprietary='')
Subtitle(index=2, start=datetime.timedelta(seconds=40, microseconds=641000), end=datetime.timedelta(seconds=44, microseconds=687000), content='可是这些珍贵的淡水中却充满了惊奇', proprietary='')
Subtitle(index=3, start=datetime.timedelta(seconds=57, microseconds=908000), end=datetime.timedelta(seconds=63, microseconds=414000), content='所有陆地生命归根结底都依赖於淡水', proprietary='')

And here’s how you go back from Subtitle objects to SRT output:

>>> subs = list(srt.parse(data))
>>> subs[1].content = "Changing subtitle data is easy!"
>>> print(srt.compose(subs))
1
00:00:33,843 --> 00:00:38,097
地球上只有3%的水是淡水

2
00:00:40,641 --> 00:00:44,687
Changing subtitle data is easy!

3
00:00:57,908 --> 00:01:03,414
所有陆地生命归根结底都依赖於淡水

Installation

To install the latest stable version from PyPi:

pip install -U srt2

To install the latest development version directly from GitHub:

pip install -U https://github.com/RaSan147/srt2/archive/refs/heads/develop.zip

Testing

tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

srt2-3.5.3.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

srt2-3.5.3-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file srt2-3.5.3.tar.gz.

File metadata

  • Download URL: srt2-3.5.3.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for srt2-3.5.3.tar.gz
Algorithm Hash digest
SHA256 977892940687e272a738693354edc99d2fb595e83185c72b19d86f8fbf6b1d84
MD5 021661a330e120b4f7d107a42e8a3db2
BLAKE2b-256 d1af35c3aeab3d9da73805324be3483e08f78df32e64d41da85b816cc78073da

See more details on using hashes here.

File details

Details for the file srt2-3.5.3-py3-none-any.whl.

File metadata

  • Download URL: srt2-3.5.3-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for srt2-3.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fefd0768c86c2554467b34fd8badbd08a9355ec606bb285594bac121f1092fa8
MD5 75c68ed12b61502174566b7e39a85e42
BLAKE2b-256 5ccc141b8ff07f2e0d5dcf1865ee4c3e68c13248a3c4ee39f4901503cd65a7e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page