Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 09 Dec 2019, 03:20
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Simple script to adjust times for subtitles in SRT format
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Fri 28 Dec 2018, 07:57    Post subject:  Simple script to adjust times for subtitles in SRT format  

I was showing some movies for my family on TV but noticed that subtitles aren't synchronized well with dialogs. So i've made a script:

Code:
#!/bin/sh
# srted.sh by puppy_apprentice
# simple script to adjust times for subtitles in SRT format
# https://matroska.org/technical/specs/subtitles/srt.html
# usage: srted.sh srtfile delta skip

srtfile=$1 # file with subtitles in SRT format
delta=$2   # number of seconds
skip=$3    # start after subtitle number
n=1

# used grep for extra newline for read command
grep "" $srtfile | while read -r line
do
      if [[ $line =~ "-->" ]]
      then
         if [[ $n > $skip ]]
         then
            ft=$(date "+%s" -d "01/01/1970 ${line:0:8}")
            ft=$((ft+delta))
            st=$(date "+%s" -d "01/01/1970 ${line:17:8}")
            st=$((st+delta))
            line=${line/${line:0:8}/$(date +%T -d @$ft)}
            echo "${line/${line:17:8}/$(date +%T -d @$st)}"
         else
            echo "$line"
         fi
         n=$((n+1))
      else
         echo "$line"
      fi
done


Do we have in Puppy Land apps for subtitles editing/converting?
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1942
Location: Japan

PostPosted: Sat 29 Dec 2018, 01:40    Post subject:  

Doesn't seem to work properly with the srt sample of the matroska.org page

Sample srt text:
Code:
1
00:02:17,440 --> 00:02:20,375
Senator, we're making
our final approach into Coruscant.

2
00:02:20,476 --> 00:02:22,501
Very good, Lieutenant.

With delta=2 and skip=0 it gives me this:
Code:
1
00:02:19,440 --> 00:02:22,375
Senator, we're making
our final approach into Coruscant.

2
00:02:24,476 --> 00:02:22,501
Very good, Lieutenant.

Subtitle 1 is OK, subtitle 2 is not.
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Sat 29 Dec 2018, 03:02    Post subject:  

LOL. For 10 movies and subtitles for them worked fine. I had luck Smile

I think that switching two lines is enough:
Code:
line=${line/${line:17:8}/$(date +%T -d @$st)}
echo "${line/${line:0:8}/$(date +%T -d @$ft)}"


Thx for feedback.
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1942
Location: Japan

PostPosted: Sat 29 Dec 2018, 08:47    Post subject:  

I also noticed that delta does not accept split seconds. I can imagine that even a delay of 0.5 sec can be annoying, so I tried a quick & dirty variation that allows deltas up to nanosecond level:
Code:
#!/bin/bash
srtfile=$1 # file with subtitles in SRT format
delta=$2   # number of seconds (min 1, max 9 decimals)
skip=$3    # start after subtitle number

[[ $delta =~ ^[0-9]*$ ]] && delta=${delta}.0   #date command requires delta to be floating-point value
n=1

while read -r line || [ -n "$line" ] # reads last line even if it does not end with newline
do
   if [[ $line =~ "-->" ]]
   then
     if [[ $n > $skip ]]
     then
       ft=${line%% *}
       st=${line##* }
       echo "$(date -d"$ft +$delta sec" +"%T,%N") --> $(date -d"$st +$delta sec" +"%T,%N")"
      else
       echo "$line"
     fi
     n=$((n+1))
   else
     echo "$line"
   fi
done < "$srtfile"

Previous example with delta=2.2 and skip=0 results in
Code:
1
00:02:19,640000000 --> 00:02:22,575000000
Senator, we're making
our final approach into Coruscant.

2
00:02:22,676000000 --> 00:02:24,701000000
Very good, Lieutenant.

I'm not sure if the trailing zeros created by %N cause problems. If so we may trim them.
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Sat 29 Dec 2018, 16:19    Post subject:  

I didn't test it on my TV or any Linux or Windows player, but i think that milli/nano seconds should be rounded to three places after the comma to be consistent with specification.
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 13389
Location: Arizona USA

PostPosted: Sat 29 Dec 2018, 16:24    Post subject:  

A nanosecond seems like overkill. Synchronized to within a millisecond will be sufficient for human ears.
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1942
Location: Japan

PostPosted: Sat 29 Dec 2018, 21:46    Post subject:  

Flash wrote:
A nanosecond seems like overkill. Synchronized to within a millisecond will be sufficient for human ears.
Human ears are not involved when it comes to subtitles. IMO even milliseconds are overkill and one digit should be sufficient. The date command displays fractions of a second always with 9 digits and there is no straightforward way to limit this number. Question is if the program processing the time value simply ignores excess zeros.
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 13389
Location: Arizona USA

PostPosted: Sat 29 Dec 2018, 22:01    Post subject:  

My mistake. Smile
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1942
Location: Japan

PostPosted: Sat 29 Dec 2018, 23:04    Post subject:  

puppy_apprentice wrote:
I didn't test it on my TV or any Linux or Windows player, but i think that milli/nano seconds should be rounded to three places after the comma to be consistent with specification.
Yes, rereading the spec it appears that exactly 3 digits are required.

Cutting instead of rounding should do:
Code:
       ft=${line%% *}
       ft_new=$(date -d"$ft +$delta sec" +"%T,%N")
       ft_new=${ft_new::12}   #cut to milliseconds
       st=${line##* }
       st_new=$(date -d"$st +$delta sec" +"%T,%N")
       st_new=${st_new::12}   #cut to milliseconds
       echo "$ft_new --> $st_new"
Back to top
View user's profile Send private message 
misko_2083


Joined: 08 Nov 2016
Posts: 100

PostPosted: Sun 30 Dec 2018, 08:37    Post subject:  

MochiMoppel wrote:
puppy_apprentice wrote:
I didn't test it on my TV or any Linux or Windows player, but i think that milli/nano seconds should be rounded to three places after the comma to be consistent with specification.
Yes, rereading the spec it appears that exactly 3 digits are required.

Cutting instead of rounding should do:
Code:
       ft=${line%% *}
       ft_new=$(date -d"$ft +$delta sec" +"%T,%N")
       ft_new=${ft_new::12}   #cut to milliseconds
       st=${line##* }
       st_new=$(date -d"$st +$delta sec" +"%T,%N")
       st_new=${st_new::12}   #cut to milliseconds
       echo "$ft_new --> $st_new"

Or next
Code:
date -d"$ft +$delta sec" +"%T,%3N"

date +"%T.%3N" --> returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1942
Location: Japan

PostPosted: Sun 30 Dec 2018, 09:12    Post subject:  

misko_2083 wrote:
date +"%T.%3N" --> returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
Great, thanks!
I've never seen this documented.
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 02 Jan 2019, 15:35    Post subject:  

So the final version is:

Code:
#!/bin/sh
# srted.sh by puppy_apprentice, MochiMoppel, Flash and misko_2083
# simple script to adjust times for subtitles in SRT format
# https://matroska.org/technical/specs/subtitles/srt.html
# usage: srted.sh srtfile delta skip

srtfile=$1 # file with subtitles in SRT format
delta=$2   # number of seconds and/or miliseconds, format: S, 0.M, S.M
skip=$3    # start processing after subtitle number

[[ $delta =~ ^[0-9]*$ ]] && delta=${delta}.0   #date command requires delta to be floating-point value
n=1

while read -r line || [ -n "$line" ] # reads last line even if it does not end with newline
do
   if [[ $line =~ "-->" ]]
   then
     if [[ $n > $skip ]]
     then
       ft=${line%% *}
       st=${line##* }
       echo "$(date -d"$ft +$delta sec" +"%T,%3N") --> $(date -d"$st +$delta sec" +"%T,%3N")"
      else
       echo "$line"
     fi
     n=$((n+1))
   else
     echo "$line"
   fi
done < "$srtfile"


BTW Is it is possible to attach to GtkDialog window a video preview widget to make a visual subtitles maker?
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 13389
Location: Arizona USA

PostPosted: Wed 02 Jan 2019, 22:02    Post subject:  

puppy_apprentice wrote:
Is it is possible to attach to GtkDialog window a video preview widget to make a visual subtitles maker?

Please start a new thread for this question. Otherwise, no one will ever see it. Thanks. Smile
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0508s ][ Queries: 11 (0.0046s) ][ GZIP on ]