VideoHelp Forum
+ Reply to Thread
Results 1 to 7 of 7
Thread
  1. Member
    Join Date
    Feb 2008
    Location
    United States
    Search Comp PM
    I used ffmpeg to batch convert some ttml/vtt subtitles to srt. I then noticed that there are random indents at the beginning of some lines in the SRT file.

    For example:
    Code:
    1
    00:00:01,000 --> 00:00:05,000
     one
    two
      three
    
    2
    00:00:06,000 --> 00:00:10,000
    one
      two
     three
    I would like it to be:
    Code:
    1
    00:00:01,000 --> 00:00:05,000
    one
    two
    three
    
    2
    00:00:06,000 --> 00:00:10,000
    one
    two
    three
    I've been using Subtitle Edit 4.0.4, but can't find a setting to remove indentation.
    Is there a way to batch it in Subtitle Edit or another way to batch remove the indentations?
    Quote Quote  
  2. I don't know about a batch convert but I would use MS Word to make changes. SE can do it under Edit > Replace but you might have a hard time removing a single space at the beginning of a line since there are single spaces throughout the SRT. In Word just add a ^p in front of the spaces ("^p ") without the quotes. In SE I don't know how you signify a CR/LF. The ^p would make it a unique search since it follows a line.
    Sorry, I keep editing this reply.
    When you replace just replace with a ^p. So find "^p ", replace "^p".
    Last edited by Moralez; 11th Apr 2024 at 10:46.
    Quote Quote  
  3. Member
    Join Date
    Feb 2008
    Location
    United States
    Search Comp PM
    I found the option in Subtitle Edit. I'll post it here in case anyone else has the same issue.

    Tools --> Fix common errors --> Remove unneeded spaces


    Thanks for the helpful suggestion Moralez. I have too many files to do each one manually so I need to do it batch. But your method would work great for someone who only has to make a few edits. Thanks.
    Quote Quote  
  4. Many advanced text editors have search and replace commands that would allow you to replace a newline followed by a space (or several spaces) with just a newline. Since SRT files are plain text you can use any of those editors.

    Even Notepad can do this:

    Search for:
    Code:
    Shift+RightArrow Space
    replace with:
    Code:
    Shift+RightArrow
    Where Shift+RightArrow means hold down the shift button and press Right Arrow (you won't see anything in the search text). Space means type the space bar.
    Last edited by jagabo; 12th Apr 2024 at 17:14.
    Quote Quote  
  5. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    I use this, it's tiny 263KB. http://findandreplace.io/
    It has a built in GUI, just run it to see.
    It also works in command line mode (--cl)
    This is my batch file. Each of the 3 iterations removes one space from the beginning of a line. If you set it to do 5, then all leading spaces should be removed.
    no_spaces.cmd
    Code:
    @echo off
     for %%a in ("*.txt") do (
        fnr.exe --cl --dir "%CD%" --fileMask "*.srt" --silent --useRegEx --useEscapeChars --find "^ " --replace ""
        fnr.exe --cl --dir "%CD%" --fileMask "*.srt" --silent --useRegEx --useEscapeChars --find "^ " --replace ""
        fnr.exe --cl --dir "%CD%" --fileMask "*.srt" --silent --useRegEx --useEscapeChars --find "^ " --replace ""
    )
     pause
    I also have these lines in my batch file to remove any font colour characteristics that are not the default for a srt. i.e. white.
    Code:
        fnr.exe --cl --dir "%CD%" --fileMask "*.srt" --silent --useRegEx --useEscapeChars --find "\</font\>" --replace ""
        fnr.exe --cl --dir "%CD%" --fileMask "*.srt" --silent --useRegEx --useEscapeChars --find "\<font color.{10,}\>" --replace ""
    Suddenly finding the font changing to yellow or, yetch!, lime green, annoys me.
    Cheers.
    Last edited by pcspeak; 12th Apr 2024 at 23:52.
    Quote Quote  
  6. You may find this useful. Consider this, not as a humbug but, as an opportunity to learn something new.


    Create a loop that will capture all your subtitle files. There are numerous examples of doing this in this very forum.


    For each file found execute


    Code:
    sed -e "s#^ *##"

    this code will find all lines that begin with one or multiple spaces and delete all the spaces.


    I suspect this question was asked and answered multiple times before.






    Although sed is not shipped with windows, it is a free download.


    This should take no more than 5 lines of code and you will be quite happy with your accomplishment.
    Quote Quote  
  7. To widen a collection of solutions, using python to remove leading space characters in srt files in a directory:
    Code:
    from pathlib import Path
    import sys
    
    def main(directory):
        #batch remove leading space characters for a srt files in a directory
        print(f'changed files:')
        for text_file in Path(directory).glob("*.srt"):
            if process_text(text_file):
                print(f'  {text_file}')
            
    def process_text(text_file):
        #remove leading space characters from a text file
        text_file = str(text_file)
        with open(text_file) as f:
            is_change = False
            lines=list(f)
            for i, line in enumerate(lines):
                if line.startswith(" "):
                    is_change = True
                    lines[i]=line.lstrip()
        if is_change:
            with open(text_file,'w') as f:
                f.write(''.join(lines))
        return is_change
    
    if __name__ == '__main__':
        """
        Usage:
        python "delete_leading_spaces.py" "D:/my_directory" 
        """
        try:
            main(sys.argv[1])
        except IndexError:
            raise IndexError('No directory argument passed') from None
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!