Skip to main content

A no-frills solution for fixing subtitle synchronization issues in SRT (SubRip Subtitle) files by precisely shifting all subtitle timestamps forward or backward using configurable time delays.

Project description

srt-time-tweaker

srt-time-tweaker is a no-frills solution for fixing subtitle synchronization issues in SRT (SubRip Subtitle) files by precisely shifting all subtitle timestamps forward or backward using configurable time delays.


Features

  • Shift subtitle timestamps by specified hours, minutes, seconds, and milliseconds
  • Accept delay as a single string in HH:MM:SS,ms format
  • Supports adding or subtracting delay to/from timestamps
  • Handles negative timing errors with an option to ignore or raise exceptions
  • Usable both as a command-line tool and as a Python importable function

Requirements

  • Python 3.x

Installation

pip install srt-time-tweaker

Usage

Command Line Interface (CLI)

Run srt-time-tweaker from the terminal to launch the program.

srt-time-tweaker INPUT_FILE [-o OUTPUT_FILE] [-H HOURS] [-M MINUTES] [-S SECONDS] [-ms MILLISECONDS] [-d DURATION] [--subtract] [--ignore-negative]

Arguments:

Argument Description Default
INPUT_FILE Path to input .srt subtitle file (required)
-o, --output Path to output .srt file output.srt
-H, --hours Hours to shift 0
-M, --minutes Minutes to shift 0
-S, --seconds Seconds to shift 0
-ms, --milliseconds Milliseconds to shift 0
-d, --duration Time delay in HH:MM:SS,ms format (overrides hours, minutes, seconds, milliseconds) None
--subtract Subtract the delay from timestamps instead of adding False
--ignore-negative Ignore negative timing errors instead of raising exceptions (while subtract) False

Examples:

Shift subtitles forward by 2 seconds:

srt-time-tweaker example.srt -S 2 -o shifted.srt

Shift subtitles backward by 1 minute 15 seconds:

srt-time-tweaker example.srt -M 1 -S 15 --subtract -o shifted_back.srt

Shift using duration string:

srt-time-tweaker example.srt -d 00:00:05,500 -o shifted_5s_500ms.srt

Python API

Import and use srt_time_tweaker function directly in Python scripts.

from srt_time_tweaker import srt_time_tweaker as srtt

srtt(
    srt_input="example.srt",
    srt_output="output.srt",
    hours=0,
    minutes=1,
    seconds=30,
    milliseconds=250,
    duration=None,                  # Override other time params if set (format: "HH:MM:SS,ms"); Default : None
    subtract_delay=False,           # True to subtract delay; Default : False
    ignore_negative_error=False     # Ignore negative timing errors instead of raising exceptions (while subtract); Default : False
)

Function Parameters

Parameter Type Description Default
srt_input str Path to input .srt subtitle file (required)
srt_output str Path to output .srt file "output.srt"
hours int Hours to shift 0
minutes int Minutes to shift 0
seconds int Seconds to shift 0
milliseconds int Milliseconds to shift 0
duration str Time delay in HH:MM:SS,ms format (overrides hours, minutes, seconds, milliseconds) None
subtract_delay bool Subtract the delay from timestamps instead of adding True
ignore_negative_error bool Ignore negative timing errors instead of raising exceptions (while subtract) False

Independent Utility Functions

In addition to the main API, two utility functions are provided for validating and extracting subtitle timing data. These are useful when working with .srt files at a lower level or integrating custom logic:

from srt_time_tweaker import valid_timings_line, get_timings
Function Description
valid_timings_line(line) Returns True if the given line matches the standard SRT timing format (HH:MM:SS,ms --> HH:MM:SS,ms); otherwise False.
get_timings(line) Extracts and returns a tuple of two lists: start and end time components as ([HH, MM, SS, ms],[HH, MM, SS, ms]); otherwise ([], []) if invalid line

Example Usage:

line = "00:01:15,500 --> 00:01:18,750"

if valid_timings_line(line):
    ss, to = get_timings(line)
    print("Start:", ss)    # start at [0, 1, 15, 500]
    print("End:", to)      # end at [0, 1, 18, 750]

Example SRT

https://gist.github.com/BhagyaJyoti22006/d7b961e7a1c8b2a7708088cefd28749c

Click on the link and download the example.srt file with which the script can be tested however required. Read the example.srt file to get an overall understanding about how they work.


Notes

  • If the delay subtracts more time than the original timestamp causing negative times, by default a ValueError is raised.
  • Use ignore_negative_error=True to silently skip negative timing errors in codebase or pass --ignore-negative in CLI

License

Apache License 2.0

This library is open-source and free to use under the Apache 2.0 License.


Contributing

Contributions, suggestions, and feature requests are welcome! Feel free to submit an issue or PR.


Author

Developed by भाग्य ज्योति (Bhagya Jyoti)


Happy subtitle syncing!

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

srt_time_tweaker-1.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

srt_time_tweaker-1.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: srt_time_tweaker-1.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for srt_time_tweaker-1.1.0.tar.gz
Algorithm Hash digest
SHA256 1f8c92b80b632588967c5395f4276e251d9f58e1dd5be05c4cb64c6fcc082ed7
MD5 12181655dcc2ca7bd1a22b5cedfa5398
BLAKE2b-256 a3bb146f1e1b59fed8be0abc13b32970b2c0c8525fc8879351ff2de69560b7b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for srt_time_tweaker-1.1.0.tar.gz:

Publisher: pypi.yml on BhagyaJyoti22006/srt-time-tweaker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for srt_time_tweaker-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b24c19823ae994359b5a51cdd5357b8fad023bda0041ee3394f5f88c71b2f323
MD5 3ea804e526a2c9687a6cc44f35d80582
BLAKE2b-256 d3c8ece6df74d3a29d462933b24578f824c6b12ff0be0639a29216a95cec62de

See more details on using hashes here.

Provenance

The following attestation bundles were made for srt_time_tweaker-1.1.0-py3-none-any.whl:

Publisher: pypi.yml on BhagyaJyoti22006/srt-time-tweaker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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