Skip to main content

A simple Python subtitle parsing library

Project description

fairy-subtitle

A simple and powerful Python subtitle parsing library that supports parsing and processing of multiple subtitle formats.

LICENSE MIT Python 3.9+ Version 1.1.0

简体中文 | English

Features

  • Supports multiple common subtitle formats: SRT, VTT, ASS, SUB, SBV
  • Simple and easy-to-use API interface
  • Flexible subtitle item operations (merge, split, insert, delete, etc.)
  • Complete exception handling mechanism
  • Support for format conversion and saving
  • Pure Python implementation with no third-party dependencies

Installation

pip install fairy-subtitle

Quick Start

Parse Subtitle Files

from fairy_subtitle import SubtitleLoader

# Parse SRT file
subtitle = SubtitleLoader.load("example.srt")

# Parse VTT file
subtitle = SubtitleLoader.load("example.vtt")

# Parse ASS file
subtitle = SubtitleLoader.load("example.ass")

# Parse with specified format
subtitle = SubtitleLoader.load("example.txt", format="srt")

Subtitle Operations

# Get number of subtitles
print(f"Number of subtitles: {len(subtitle)}")

# Get subtitle duration
print(f"Subtitle duration: {subtitle.get_duration():.2f} seconds")

# Access subtitles
print(subtitle[0])  # First subtitle
print(subtitle[-1])  # Last subtitle

# Merge subtitles
subtitle.merge(0, 2)  # Merge subtitles from index 0 to 2

# Split subtitle
subtitle.split(0, 10.5)  # Split subtitle at index 0 at 10.5 seconds

# Insert subtitle
from fairy_subtitle.models import Cue
subtitle.insert(0, Cue(start=0.0, end=5.0, text="New subtitle"))

# Delete subtitle
subtitle.remove(0)  # Delete subtitle at index 0

Search and Filter

# Search for subtitles containing specific text
results = subtitle.find("keyword")

# Filter subtitles within specific time range
filtered = subtitle.filter_by_time(0, 100)

Format Conversion and Saving

# Convert to SRT format string
srt_content = subtitle.to_srt()

# Convert to VTT format string
vtt_content = subtitle.to_vtt()

# Convert to ASS format string
ass_content = subtitle.to_ass()

# Save as specified format
subtitle.save("output.vtt")  # Save in original format
subtitle.save("output.srt", save_format="srt")  # Save as SRT format
subtitle.save("output.ass", save_format="ass")  # Save as ASS format

API Reference

SubtitleLoader

load(file_path: str, format: str = None) -> Subtitle

Parse subtitle file and return Subtitle object.

  • file_path: Subtitle file path
  • format: Subtitle format (optional, auto-detected)

Subtitle

Basic Properties

  • cues: List of subtitles
  • info: Subtitle information

Basic Methods

  • __len__(): Return number of subtitles
  • __getitem__(index): Get subtitle at specified index
  • get_duration(): Return total subtitle duration
  • merge(index1: int, index2: int): Merge subtitles
  • split(index: int, time: float): Split subtitle
  • insert(index: int, cue: Cue): Insert subtitle
  • remove(index: int): Delete subtitle
  • find(text: str) -> list[Cue]: Search subtitles
  • filter_by_time(start: float, end: float) -> list[Cue]: Filter subtitles
  • to_dict(): Convert to dictionary
  • to_srt(): Convert to SRT format
  • to_vtt(): Convert to VTT format
  • to_ass(): Convert to ASS format
  • save(file_path: str, save_format: str = None): Save subtitle

Cue

Basic Properties

  • start: Start time (seconds)
  • end: End time (seconds)
  • text: Subtitle text
  • index: Subtitle index

Basic Methods

  • to_dict(): Convert to dictionary

License

This project is licensed under the MIT License - see the LICENSE file for details

Contributing

Welcome to submit Issues and Pull Requests!

Feedback

If you encounter any issues during use, please submit an Issue on GitHub:

[https://github.com/baby2016/fairy-subtitle/issues]

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

fairy_subtitle-1.1.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

fairy_subtitle-1.1.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file fairy_subtitle-1.1.0.tar.gz.

File metadata

  • Download URL: fairy_subtitle-1.1.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for fairy_subtitle-1.1.0.tar.gz
Algorithm Hash digest
SHA256 baa93f28985f39c61c08eae452ecb66e0099c396342ad5963cbee9a56335c555
MD5 f23f73bbf09c333fff279a500a44409d
BLAKE2b-256 b97c3c30fb7aa05e010642969610ff8618473daef2c2bbbfed371276758231ff

See more details on using hashes here.

File details

Details for the file fairy_subtitle-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: fairy_subtitle-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for fairy_subtitle-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5962b414a5132587c5613720a2c5fbc69e5e715103e2d75211783fb179dd3b8
MD5 e3be81888f08a55fd418ddcd56f21208
BLAKE2b-256 1dfef9f49240ed56e35ec5aeaa33bf52bf9e889a2120c6d157f35811b3825708

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