Recursive AI

Shift SRT subtitle timestamps in C#

OneMedia is a free, open-source media library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use SubtitleAlignment.Shift to move cues earlier or later. captions.srt is in the zip.

Terminal
dotnet add package RecursiveAI.OneMedia

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMedia;
using RecursiveAI.OneMedia.Captions;
 
var input = Subtitles.Read("captions.srt");
// -1.5 s: 1000 ticks per second.
var offset = new MediaTime(-1500, 1000);
var shifted = SubtitleAlignment
    .Shift(input, offset);
Subtitles.Write(
    shifted.Document, "shifted.srt");
Console.WriteLine("Wrote shifted.srt");
foreach (var loss in shifted.Report.Losses)
    Console.WriteLine(
        $"Cue {loss.CueIndex + 1}: {loss.Kind}");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Writes a separate SRT and prints:

Text
Wrote shifted.srt
Cue 1: Dropped
Cue 2: Approximated

The example input is:

Text
1
00:00:00,250 --> 00:00:01,250
Welcome.
 
2
00:00:01,000 --> 00:00:02,500
Lights up.
 
3
00:00:03,500 --> 00:00:04,500
Music starts.

Cue numbers in the log are positions in the input, starting at 1. Dropped means the shifted cue ends at or before zero; Approximated means its start was moved up to zero. The resulting file is:

Text
1
00:00:00,000 --> 00:00:01,000
Lights up.
 
2
00:00:02,000 --> 00:00:03,000
Music starts.

If shifted.srt already exists, Subtitles.Write throws IOException instead of overwriting it. Choose a new output name to rerun the example.

Variations

Delay the captions instead. Replace the offset line to move both timestamps of each cue 1.5 seconds later:

C#
var offset = new MediaTime(1500, 1000);

The short answer

Read the file with Subtitles.Read, pass the document and a signed MediaTime to SubtitleAlignment.Shift, then write shifted.Document to a new .srt path. The extension keeps the output in SubRip format. The program lists the affected input cues from shifted.Report.Losses.

How do I adjust the timing of SRT subtitles?

Compare one caption’s start time with where it should appear. Subtract the current time from the desired time: if a caption appears at 3.5 seconds but the sound starts at 2 seconds, the offset is −1.5 seconds. With MediaTime, a timescale of 1000 means the first argument is in milliseconds, so pass -1500. If the captions appear too early, use a positive offset. The video file doesn’t have to be open for this operation.

The same amount is added to each cue’s start and end. That holds the cue duration steady except at the beginning, where the shift may cut off part of a cue or remove it entirely. Check the log if the first caption goes missing. In this sample, the first cue would end a quarter of a second before zero and is dropped, while the second would start half a second before zero and is shortened to its remaining second.

Can a fixed SRT offset fix subtitles that drift out of sync?

Check one caption near the beginning and another near the end. If they’re both off by the same amount and in the same direction, shifting the SRT fits that problem. If the error grows, a constant offset can align one part but leaves a different error elsewhere: it doesn’t stretch or compress the timeline. If the offset changes abruptly at a cut, align the captions on each side of the cut separately rather than shifting the whole file by one amount.

All onemedia recipes