cdown/srt


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

License: Other

Language: Python

Keywords: public-domain, python, srt, subtitle, subtitles, subtitles-parsing, text-extraction


Tests LGTM Coverage Dependencies

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, bw_plex, and many more.

Why choose this library?

  • Extremely lightweight, ~150 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
  • Native support for Python 2 and 3
  • 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 into the public domain
  • 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:

>>> # list() is needed as srt.parse creates a generator
>>> subs = list(srt.parse('''\
... 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
... 所有陆地生命归根结底都依赖於淡水
...
... '''))
>>> subs
[Subtitle(index=1, start=datetime.timedelta(0, 33, 843000), end=datetime.timedelta(0, 38, 97000), content='地球上只有3%的水是淡水', proprietary=''),
 Subtitle(index=2, start=datetime.timedelta(0, 40, 641000), end=datetime.timedelta(0, 44, 687000), content='可是这些珍贵的淡水中却充满了惊奇', proprietary=''),
 Subtitle(index=3, start=datetime.timedelta(0, 57, 908000), end=datetime.timedelta(0, 63, 414000), content='所有陆地生命归根结底都依赖於淡水', proprietary='')]
>>> print(srt.compose(subs))
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
所有陆地生命归根结底都依赖於淡水

Installation

To install the latest stable version from PyPi:

pip install -U srt

To install the latest development version directly from GitHub:

pip install -U git+https://github.com/cdown/srt.git@develop

Testing

tox

Project Statistics

Sourcerank 7
Repository Size 360 KB
Stars 140
Forks 19
Watchers 12
Open issues 1
Dependencies 4
Contributors 3
Tags 25
Created
Last updated
Last pushed

Top Contributors See all

Chris Down Stephen Macke Thyrst'

Packages Referencing this Repo

srt
A tiny library for parsing, modifying, and composing SRT files.
Latest release 1.11.0 - Updated - 140 stars

Recent Tags See all

3.1.0 December 08, 2019
3.0.0 October 18, 2019
2.1.0 September 09, 2019
2.0.0 July 27, 2019
1.11.0 July 04, 2019
1.10.0 February 18, 2019
1.9.0 January 06, 2019
1.8.0 December 18, 2018
1.7.0 November 28, 2018
1.6.0 August 26, 2017
1.5.0 June 24, 2017
1.4.0 June 03, 2017
1.3.0 January 17, 2017
1.2.0 September 04, 2016
1.1.0 February 15, 2016

Something wrong with this page? Make a suggestion

Last synced: 2020-01-03 11:44:43 UTC

Login to resync this repository