javascriptsrt

How to add new item into srt file


This is start of a srt file:

0  
00:00:07,000 --> 00:01:00,000  
lorem ipsum...  // this line doesn't work  

1  
00:01:02,960 --> 00:01:05,800  
lorem ipsum...  

2  
00:01:05,840 --> 00:01:08,960  
lorem ipsum...  

The first line doesn't work, I suppose because of0 as the ordinal.
I need a way, javascript way if possible, to correctly change all ordinal numbers at once, not one by one (over 1000 lines), starting with 1 and not with 0.

I was searching for various online solutions, without success.


Solution

  • You could create an html file with a textarea, load it in your browser and copy the contents of the .srt file in. Use the following javascript to convert the textarea's text:

    var numberRegex = /^\d\s*$/;
    
    var originalLines = text.split('\n');
    var lines = [];
    for (var index = 0; index != originalLines.length; ++index)
    {
        var orig = originalLines[index];
        var match = numberRegex.exec(orig);
        lines.push(match ? parseInt(orig) + 1 : orig);
    }
    

    Then the converted text you want is provided by:

    lines.join('\n')