Coding help to correct timestamps on caption file.



  • Hi. The automatic caption creation software I’m using overlaps the timestamps on the .sbv file (a basic text file.) I can open the file in Notepad++ and

    I’m looking for some code to copy the second timestamp and then 3 lines down replace the first timestamp in that line with the copied timestamp.

    So for the first line in the sample, the second timestamp would be copied, so the part after the “,” 0:00:04.799

    That copied timestamp would then replace the first timestamp in the next timestamp (3 lines down) replacing the “0:00:02.720”

    The sample file:

    0:00:00.320,0:00:04.799
    hello and welcome back to this

    0:00:02.720,0:00:06.720
    video series

    0:00:04.799,0:00:08.800
    now from the previous videos you

    0:00:06.720,0:00:10.800
    can see that there’s a lot of new things

    So the new output would be

    0:00:00.320,0:00:04.799
    hello and welcome back to this

    0:00:04.799,0:00:06.720
    video series

    0:00:06.720,0:00:08.800
    now from the previous videos you

    0:00:08.800,0:00:10.800
    can see that there’s a lot of new things

    and continue until the end of the file

    Thanks for your help!



  • @MaximillianM said in Coding help to correct timestamps on caption file.:

    I’m looking for some code to copy the second timestamp and then 3 lines down replace the first timestamp in that line with the copied timestamp.

    So long as your data is as well formatted as you state (and show) the following regex (regular expression) should work. I mainly refer to there being the format of (repeated):
    timestamp,timestampEOL
    textEOL
    EOL

    Using the Replace function we have
    Find What:(?-s)(?<=\d,)(\d:.+)(\R.+\R\R)([^,]+)
    Replace With:\1\2\1
    As this is a regex the search mode must be regular expression. You can either use the “Replace” button or do the entire file in one click with the “Replace All” button. You could also just test it out by using the “Find Next” a few times to see that the selection it is working with looks right.

    Terry



  • @MaximillianM said in Coding help to correct timestamps on caption file.:

    The sample file:
    0:00:00.320,0:00:04.799

    One proviso which you should understand. The timestamp I’m selecting will never have more than the “number” 9:59:59.999. So it cannot be 10 hours or longer. I don’t think this will be an issue. If it is then the Find What code would need slightly altering to:
    Find What:(?-s)(?<=\d,)(\d{1,2}:.+)(\R.+\R\R)([^,]+)
    that allows for times up to just shy of 100 hours.

    Terry



  • @Terry-R Thanks so much Terry! That does exactly what I wanted. Though now I see that the timing doesn’t quite line up with the words. I think subtracting 1 or 2 seconds from the pasted value might be the solution. Is there a way to do that? Thanks again :-)



  • @MaximillianM said in Coding help to correct timestamps on caption file.:

    I think subtracting 1 or 2 seconds from the pasted value might be the solution. Is there a way to do that?

    Not possible with a regex as it doesn’t provide any calculation ability. It would need to be done with the help of a programming language. Actually I just thought of another possible way, but it would require some temporary additions to the lines. I refer to an old post in this forum here.

    As it stands that won’t help but it might provide the foundation, of course the file would likely need to resemble XML code before the plugin will work, I think.

    A third option would be to find all the alternative seconds that exist in the file, then provide the new number alongside them (this might be done in some other external app such as Excel). These “pairs” would be listed below all the text. A regex could then identify a particular seconds number, then look forward to the “pairs” table below, find the replacement and replace the number. Lastly the pairs table would be removed.

    As you can see, there’s a bit involved, regardless which method is used. A fourth option would be to import it into Excel (or some other application) and perform the calculations there, but that is outside the realm of this forum. I only mention it as that might be an easier solution for you to engage with.

    Terry



  • @MaximillianM said in Coding help to correct timestamps on caption file.:

    Is there a way to do that?

    Just looked online and found this, an online tool for permanently syncing subtitle files.

    Good luck

    Terry



  • @MaximillianM said in Coding help to correct timestamps on caption file.:

    Though now I see that the timing doesn’t quite line up with the words

    I should add that although the online tool works with SRT files and you have a SBV file, they are very closely formatted. I think it would be relatively easy to reformat your file to a SRT file, use the online tool to align the timestamps, then change it back to a SBV file. But that is another whole conversation. Possibly look online if any other tools exist for your type of file which may perform the same task.

    Terry



  • @MaximillianM

    You have posted on this type of subject before, and I think the end-point of that was that this sort of thing is much more complicated/involved than a text editor’s regular expression replacement functionality can handle.

    IMO you need to go to some sort of dedicated tooling, something like @Terry-R suggested.



  • @Terry-R Thanks for your help Terry!


Log in to reply