I have a series of srt files (film subtitles) that some of their time-lines overlap each others on the video! This means that some of time-lines are displayed about 1-2 seconds longer than they are in the film and conflict with the next ones!

for example:

00:00:01.000 --> 00:00:07.000
The following content is provided

00:00:04.000 --> 00:00:10.000
under a Creative Commons license.

'07' overlaps '04'!

I want to overwrite the first part of time-lines on the 2nd part of their previous line. Be care that all time-lines do not interfere with each other. some of them are correct and shorter than their next timeline! The interference is only to some of them.

  • 2
    I don't understand what you mean by "If B Do nothing".
    – geirha
    Aug 15, 2013 at 18:44
  • and that's what the code already does. Could you show an example where it doesn't?
    – geirha
    Aug 15, 2013 at 19:42
  • If you have access to the original video (with subtitles) you can extract clean subtitles (with no overlaps) using ccextractor as ccextractor VIDEO.mp4 -noru -o SUBS.srt
    – Greenish
    Jan 18, 2019 at 19:55

1 Answer 1


I asked this question on #awk IRC an @geirha wrote the below great scripts. The script may be usable for other people. Subtitles overlapping problem is common because of Human error during subtitling a movie!

Suppose your timelines are in the below format:
A --> B
C --> D

Replacing B with C:

gawk '
    RS = "";
    OFS = FS = "\n";
    n = split($0, prev_rec);
    split($2, prev_time, / --> /);
    split($2, a, / --> /);
    if (a[1] < prev_time[2])
      prev_rec[2] = prev_time[1]" --> "a[1];
    for (i=1;i<=n;i++)
      print prev_rec[i];
    n = split($0, prev_rec);
    split($2, prev_time, / --> /)
  END {
  }' SUBTITLE.srt > RESULT.srt

The above code compare B and C in this way:

If B > C => then run that charming command to replace B.
If B < C => Do nothing!

Replacing C with B:

gawk '
    split($2,a,/ --> /);
      (a[1] < prev) $2=prev" --> "a[2];
      print $0"\n"; prev=a[2]
  }' SUBTITLE.srt > RESULT.srt

To use scripts on some amount of srt files in a folder:

for file in *.srt
  do xxx "$file" > "$file.tmp" && mv "$file.tmp" "$file";

Replace xxx with the right script codes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.