Author |
Message |
puppy_apprentice

Joined: 07 Feb 2012 Posts: 300
|
Posted: 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
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: 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
|
|
 |
puppy_apprentice

Joined: 07 Feb 2012 Posts: 300
|
Posted: Sat 29 Dec 2018, 03:02 Post subject:
|
|
LOL. For 10 movies and subtitles for them worked fine. I had luck
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
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: 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
|
|
 |
puppy_apprentice

Joined: 07 Feb 2012 Posts: 300
|
Posted: 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
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 13653 Location: Arizona USA
|
Posted: 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
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: 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
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 13653 Location: Arizona USA
|
Posted: Sat 29 Dec 2018, 22:01 Post subject:
|
|
My mistake.
|
Back to top
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: 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
|
|
 |
misko_2083

Joined: 08 Nov 2016 Posts: 114
|
Posted: 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
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 2084 Location: Japan
|
Posted: 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
|
|
 |
puppy_apprentice

Joined: 07 Feb 2012 Posts: 300
|
Posted: 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
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 13653 Location: Arizona USA
|
Posted: 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.
|
Back to top
|
|
 |
|