Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:

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

2
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.

share|improve this question
2  
I don't understand what you mean by "If B Do nothing". –  geirha Aug 15 '13 at 18:44
    
and that's what the code already does. Could you show an example where it doesn't? –  geirha Aug 15 '13 at 19:42

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 '
  BEGIN {
    RS = "";
    OFS = FS = "\n";
    getline;
    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];
    printf("\n");
    n = split($0, prev_rec);
    split($2, prev_time, / --> /)
  }
  END {
    print
  }' 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 '
  BEGIN {
    RS="";
    OFS=FS="\n";
    prev="00:00:00"
  }
  {
    split($2,a,/ --> /);
    if
      (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";
done

Replace xxx with the right script codes!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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