sed + math, or adding 11 seconds to time in textfile (LOTS of rows!)


Results 1 to 4 of 4

Thread: sed + math, or adding 11 seconds to time in textfile (LOTS of rows!)

  1. #1
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494

    sed + math, or adding 11 seconds to time in textfile (LOTS of rows!)

    The case is adding a subtitle to a movie in the correct place.
    This is done by creating a textfile (extension .srt) and put it in the same directory as the movie. This works fine.
    Problem:
    I have the movie. I have the file subtitle.srt - but the text appears 11 seconds too late!
    So what I need to do is edit the file "subtitle.srt" so that every text line appears 11 seconds earlier.
    "Easy done with sed, I thought"...
    Well, I hit a brick wall - how on earth do I do math with sed???
    Or am I on the wrong path, is there some other way to do it?

    Here is an extract from the file "subtitle.srt", you'll easily see what needs be done:

    Code:
    pingu@edgar:~$ head -20 Documents/The\ Name\ of\ the\ Rose.srt
    0
    00:00:01,000 --> 00:00:04,000
    Downloaded From www.AllSubs.org
    
    1
    00:00:21,720 --> 00:00:25,360
    Having reached the end
    of my poor sinner's life...
    
    2
    00:00:25,560 --> 00:00:27,680
    my hair now white...
    
    3
    00:00:27,800 --> 00:00:30,760
    I prepare to leave
    on this parchment my testimony...
    
    4
    00:00:30,840 --> 00:00:33,360
    So record number 1, first line, shall be changed from
    00:00:21,720 --> 00:00:25,360
    to
    00:00:10,720 --> 00:00:14,360
    ... and the same for each and every line of text, a few thousand records.
    In pingvino veritas!

  2. #2
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,604
    This might be a better fit for PERL. SED is pretty much limited to text manipulation, I suppose you could write a clever regex that would do the math for you, but I think that would be somewhere from fairly complex to extremely complex. You might also be able to find some way to combine it with awk, but I'm not particularly good at awk so I'm not sure.

    In PERL, which has very nice regex support builtin too, you could read the line in, pull out the bits you want to change, subtract 11 seconds (easy to do in PERL), then write out the altered line. I don't know whether you know PERL, but if you have done much programming it isn't hard to pick up.

  3. #3
    Join Date
    Jan 2003
    Location
    Austin, Texas
    Posts
    683
    I am sure there are way better ways to accomplish this, but I just used awk to separate all of the fields. For each of the two numbers that need to be edited, I pull them out separate from everything else, and then you can just use the syntax "let i=i-11" or "let i=i+1" or whatever you want. At the very end of the loop it just echoes out the fully concatenated value.

    Here is the code:

    Code:
    #!/bin/bash
    
    inFile=$1
    arrow=" --> "
    
    # line look like this:
    # 00:00:21,720 --> 00:00:25,360
    # so we need to edit tokens 3 and 5
    while read line
    do
      first=$(echo "$line" | awk -F '[ :,]' '{print $1 ":" $2 ":"}')
      edit=$(echo "$line" | awk -F '[ :,]' '{print $3}')
      let edit=edit-11
      third=$(echo "$line" | awk -F '[ :,]' '{print "," $4}')
      fourth=$(echo "$line" | awk -F '[ :,]' '{print $6 ":" $7 ":"}')
      edit2=$(echo "$line" | awk -F '[ :,]' '{print $8}')
      let edit2=edit2-11
      last=$(echo "$line" | awk -F '[ :,]' '{print "," $9}')
      echo $first$edit$third$arrow$fourth$edit2$last
    done < $inFile
    
    exit 0

    It seems to work:

    Code:
    bdmayes@ubuntubox:~$ cat input.txt 
    00:00:21,720 --> 00:00:25,360
    00:00:22,720 --> 00:00:26,360
    bdmayes@ubuntubox:~$ ./foo.sh input.txt 
    00:00:10,720 --> 00:00:14,360
    00:00:11,720 --> 00:00:15,360
    You can just redirect the output to another file.

    What you'll need to do though is wrap the code inside of the while loop in an if statement. Just use a regular expression to determine whether or not the line should be altered. If so then execute the alterations. Else just echo the line out as is. This is left as an exercise for the reader.
    "The author of that poem is either Homer or, if not Homer, somebody else of the same name."

  4. #4
    Join Date
    Oct 2000
    Location
    Sweden
    Posts
    494
    Thanks all!
    Several good suggestions, problem is now solved.
    Needed to get this thing done pretty quick - and now it's done!
    Thx!
    In pingvino veritas!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •