Skip to main content

SRT subtitle scene splitter using Otsu method with OpenAI embeddings

Project description

scene-otsu

A Python library for scene splitting SRT subtitle files using the Otsu method. Uses OpenAI's embedding models to find semantically appropriate scene boundaries.

Features

  • SRT Subtitle Parsing: Parse SRT format subtitle files
  • Otsu Method Scene Splitting: Recursively apply multi-dimensional Otsu method to detect semantically appropriate scene boundaries
  • OpenAI Embeddings: Use OpenAI's embedding models to calculate semantic similarity of text
  • Token Limit Support: Split scenes based on specified maximum token count

Installation

pip install scene-otsu

Usage

Basic Example

from scene_otsu import SceneSplitter

# Set OpenAI API key
api_key = "your-openai-api-key"

# Initialize SceneSplitter
splitter = SceneSplitter(api_key=api_key)

# Process SRT string
srt_content = """
1
00:00:00,000 --> 00:00:05,000
Hello, welcome to this video.

2
00:00:05,000 --> 00:00:10,000
Today we will discuss machine learning.

3
00:00:10,000 --> 00:00:15,000
Let's start with the basics.
"""

# Execute scene splitting (max 200 tokens)
result = splitter.process(srt_content, max_tokens=200)
print(result)

Using SubtitleParser

from scene_otsu import SubtitleParser

# Parse SRT string
srt_content = """
1
00:00:00,000 --> 00:00:05,000
First subtitle

2
00:00:05,000 --> 00:00:10,000
Second subtitle
"""

# Basic parsing
subtitles = SubtitleParser.parse_srt_string(srt_content)
# Returns: [("00:00:00,000", "00:00:05,000", "First subtitle"), ...]

# Parse as scene information
scenes = SubtitleParser.parse_srt_scenes(srt_content)
# Returns: [{"index": 1, "start_time": "00:00:00,000", "end_time": "00:00:05,000", ...}, ...]

API Reference

SceneSplitter

__init__(api_key: str, model: str = "text-embedding-3-small", batch_size: int = 16)

Initialize SceneSplitter.

Parameters:

  • api_key: OpenAI API key
  • model: OpenAI embedding model to use (default: "text-embedding-3-small")
  • batch_size: Batch size for embedding generation (default: 16)

process(srt_string: str, max_tokens: int = 200) -> str

Process SRT string and return scene-split SRT string.

Parameters:

  • srt_string: Input SRT string
  • max_tokens: Maximum tokens per chunk (default: 200)

Returns:

  • Scene-split SRT string

SubtitleParser

parse_srt_string(srt_string: str) -> List[Tuple[str, str, str]]

Parse SRT string.

Returns:

  • List in the format [(start_timestamp, end_timestamp, text), ...]

parse_srt_scenes(srt_string: str) -> List[Dict[str, Any]]

Convert SRT to scene-based dictionary.

Returns:

  • List in the format [{index, start_time, end_time, start_sec, end_sec, text}]

parse_timestamp(timestamp: str) -> float

Convert timestamp string to seconds.

Parameters:

  • timestamp: Timestamp string (e.g., "00:00:05,000")

Returns:

  • Seconds (float, including milliseconds)

Requirements

  • Python 3.11 or higher
  • OpenAI API key

Dependencies

  • numpy >= 2.3.5
  • openai >= 2.8.1
  • scikit-learn >= 1.7.2
  • tiktoken >= 0.12.0
  • tqdm >= 4.67.1

License

MIT License

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Author

Yuki Harada

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

scene_otsu-0.1.5.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

scene_otsu-0.1.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file scene_otsu-0.1.5.tar.gz.

File metadata

  • Download URL: scene_otsu-0.1.5.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for scene_otsu-0.1.5.tar.gz
Algorithm Hash digest
SHA256 64421458899e3b4b05c9376004a0d603d7213a1fef1ecc9e42a4388771ad4a96
MD5 ba86a46ee37aad94d67ced11ad1556bf
BLAKE2b-256 7a782396aae7589ba3bebda5a162ba8c5705b58b0707944e8cedc73a6bd3271a

See more details on using hashes here.

File details

Details for the file scene_otsu-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: scene_otsu-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for scene_otsu-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 24bbab8922e0505e85a842dc8001ccdc0eea9c2a97d801c77f14b8db1a2a1ce3
MD5 14866b3d34a242d2c1869b6031536d3e
BLAKE2b-256 d9fcd9d33600816587fb22ef6de26cf40cb3aaa4c378e29fe750b94f3ff1d8d5

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