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.
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 pathformat: Subtitle format (optional, auto-detected)
Subtitle
Basic Properties
cues: List of subtitlesinfo: Subtitle information
Basic Methods
__len__(): Return number of subtitles__getitem__(index): Get subtitle at specified indexget_duration(): Return total subtitle durationmerge(index1: int, index2: int): Merge subtitlessplit(index: int, time: float): Split subtitleinsert(index: int, cue: Cue): Insert subtitleremove(index: int): Delete subtitlefind(text: str) -> list[Cue]: Search subtitlesfilter_by_time(start: float, end: float) -> list[Cue]: Filter subtitlesto_dict(): Convert to dictionaryto_srt(): Convert to SRT formatto_vtt(): Convert to VTT formatto_ass(): Convert to ASS formatsave(file_path: str, save_format: str = None): Save subtitle
Cue
Basic Properties
start: Start time (seconds)end: End time (seconds)text: Subtitle textindex: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
657f8142df693ae9a5adf03e5a557753ae2f780a0c8feb516a85b4fa34243ddf
|
|
| MD5 |
74061952deef87daa3857d7e7d1c68b4
|
|
| BLAKE2b-256 |
c0edf026d254e2edc0d6533b0cda11e56ebd89eebfc8ecbe4df0b6b1a4e31f8e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84cc0c6d887b83a5c308af6ac0bbb2562c6b9132782b8d2c4d2ea9ee6e9eacf5
|
|
| MD5 |
6ddc895d5169ae2a86c0faf95cb84f8a
|
|
| BLAKE2b-256 |
cdca5f69e80a51771f04208eb81b4a8b5061b5be319146986418e6256234e709
|