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 0.1.0

简体中文 | English

Features

  • Supports multiple common subtitle formats: SRT, VTT, ASS
  • 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.0.0.tar.gz (16.4 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.0.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fairy_subtitle-1.0.0.tar.gz
  • Upload date:
  • Size: 16.4 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.0.0.tar.gz
Algorithm Hash digest
SHA256 657f8142df693ae9a5adf03e5a557753ae2f780a0c8feb516a85b4fa34243ddf
MD5 74061952deef87daa3857d7e7d1c68b4
BLAKE2b-256 c0edf026d254e2edc0d6533b0cda11e56ebd89eebfc8ecbe4df0b6b1a4e31f8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fairy_subtitle-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84cc0c6d887b83a5c308af6ac0bbb2562c6b9132782b8d2c4d2ea9ee6e9eacf5
MD5 6ddc895d5169ae2a86c0faf95cb84f8a
BLAKE2b-256 cdca5f69e80a51771f04208eb81b4a8b5061b5be319146986418e6256234e709

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