Sync subtitles: Difference between revisions
Content added Content deleted
(→{{header|Phix}}: Added Wren) |
(Added FreeBASIC) |
||
Line 158: | Line 158: | ||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="vbnet">Function addSeconds(timeStr As String, secs As Integer) As String |
|||
Dim As Integer hh, mm, ss, ttt |
|||
hh = Val(Mid(timeStr, 1, 2)) |
|||
mm = Val(Mid(timeStr, 4, 2)) |
|||
ss = Val(Mid(timeStr, 7, 2)) + secs |
|||
ttt = Val(Mid(timeStr, 10, 3)) |
|||
While ss < 0 |
|||
ss += 60 |
|||
mm -= 1 |
|||
Wend |
|||
While mm < 0 |
|||
mm += 60 |
|||
hh -= 1 |
|||
Wend |
|||
While hh < 0 |
|||
hh += 24 |
|||
Wend |
|||
mm += ss \ 60 |
|||
hh += mm \ 60 |
|||
ss Mod= 60 |
|||
mm Mod= 60 |
|||
hh Mod= 24 |
|||
Return Right("0" & Str(hh), 2) & ":" & Right("0" & Str(mm), 2) & ":" & _ |
|||
Right("0" & Str(ss), 2) & "," & Right("000" & Str(ttt), 3) |
|||
End Function |
|||
Sub syncSubtitles(fileIn As String, fileOut As String, secs As Integer) |
|||
Dim As String linea, pio, fin, nl, fmt |
|||
Dim As Ubyte f1, f2 |
|||
fmt = "hh:MM:ss,ttt" |
|||
f1 = Freefile |
|||
Open fileOut For Output As #f1 |
|||
f2 = Freefile |
|||
Open fileIn For Input As #f2 |
|||
While Not Eof(f2) |
|||
Line Input #f2, linea |
|||
If Instr(linea, "-->") > 0 Then |
|||
pio = Mid(linea, 1, 12) |
|||
pio = addSeconds(pio, secs) |
|||
fin = Mid(linea, 18, 12) |
|||
fin = addSeconds(fin, secs) |
|||
Print #f1, pio; " --> "; fin |
|||
Else |
|||
Print #f1, linea |
|||
End If |
|||
Wend |
|||
Close #f2, #f1 |
|||
End Sub |
|||
Dim As String linea |
|||
Dim As Ubyte f = Freefile |
|||
Print !"After fast-forwarding 9 seconds:\n" |
|||
syncSubtitles("movie.srt", "movie_corrected.srt", 9) |
|||
Open "movie_corrected.srt" For Input As #f |
|||
While Not Eof(f) |
|||
Line Input #f, linea |
|||
Print linea |
|||
Wend |
|||
Close #f |
|||
Print !"\n\nAfter rolling-back 9 seconds:\n" |
|||
syncSubtitles("movie.srt", "movie_corrected2.srt", -9) |
|||
Open "movie_corrected2.srt" For Input As #f |
|||
While Not Eof(f) |
|||
Line Input #f, linea |
|||
Print linea |
|||
Wend |
|||
Close #f |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as Wren entry.</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
Revision as of 23:06, 15 June 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Sync subtitles
Sync subtitles
Sometimes, when you download a movie, the subtitles are out of sync with the audio in this movie. Video players like VLC solve the problem, but it must be done manually.
Make a program that takes the "movie.srt" file, and synchronizes the subtitles "n" seconds.
- Try to fast-forward the subtitles by 9 seconds.
- Try rolling back the subtitles by 9 seconds.
Take the excerpt from the following subtitles file as an example:
movie.srt
1 00:01:31,550 --> 00:01:36,347 Four billion years ago, the first marine life forms. 2 00:01:36,555 --> 00:01:42,019 First came the fish...then slowly other life forms evolved. 3 00:01:42,144 --> 00:01:43,979 Therefore, our ancestors... 4 00:01:43,979 --> 00:01:45,898 ...came from fish. 5 00:01:46,232 --> 00:01:47,608 Everyone, come and see this. 6 00:01:47,733 --> 00:01:50,361 Cretaceous Tyrannosaurus. 7 00:01:50,736 --> 00:01:52,488 Ferocious! 8 00:01:58,035 --> 00:01:58,869 Red, 9 00:01:59,203 --> 00:02:00,079 Pong! 10 00:02:02,540 --> 00:02:03,999 Isn't this a gecko? 11 00:02:04,416 --> 00:02:07,419 How else can I get a 15 ton T-Rex in here? 12 00:02:07,503 --> 00:02:11,048 With our advanced technology, I shrank it down.
Amazing Hopper
#include <basico.h>
algoritmo
fd=0
abrir para leer ( "movie.srt", fd )
iterar
s="", t1=0, t2=0, s1=0, s2=0
usando '1000', leer línea desde 'fd', mover a 's'
cuando ( "-->", está exactamente en 's' ){
[1:8] obtener de 's', ---copiar en 's1'---
convertir a segundos, sumarle '9', convertir a hora, guardar en 't1'
[18:25] obtener de 's', ---copiar en 's2'---
convertir a segundos, sumarle '9', convertir a hora, guardar en 't2'
transformar(s1,t1,s), guardar en 's'
transformar(s2,t2,s), guardar en 's'
}
imprimir ( s, "\n")
mientras ' no sea fin de archivo (fd ) '
cerrar archivo 'fd'
terminar
- Output:
$ hopper3 basica/modsrt.bas > movie_corrected.srt $ cat movie_corrected.srt 1 00:01:40,550 --> 00:01:45,347 Four billion years ago, the first marine life forms. 2 00:01:45,555 --> 00:01:51,019 First came the fish...then slowly other life forms evolved. 3 00:01:51,144 --> 00:01:52,979 Therefore, our ancestors... 4 00:01:52,979 --> 00:01:54,898 ...came from fish. 5 00:01:55,232 --> 00:01:56,608 Everyone, come and see this. 6 00:01:56,733 --> 00:01:59,361 Cretaceous Tyrannosaurus. 7 00:01:59,736 --> 00:02:01,488 Ferocious! 8 00:02:07,035 --> 00:02:07,869 Red, 9 00:02:08,203 --> 00:02:09,079 Pong! 10 00:02:11,540 --> 00:02:12,999 Isn't this a gecko? 11 00:02:13,416 --> 00:02:16,419 How else can I get a 15 ton T-Rex in here? 12 00:02:16,503 --> 00:02:20,048 With our advanced technology, I shrank it down.
FreeBASIC
Function addSeconds(timeStr As String, secs As Integer) As String
Dim As Integer hh, mm, ss, ttt
hh = Val(Mid(timeStr, 1, 2))
mm = Val(Mid(timeStr, 4, 2))
ss = Val(Mid(timeStr, 7, 2)) + secs
ttt = Val(Mid(timeStr, 10, 3))
While ss < 0
ss += 60
mm -= 1
Wend
While mm < 0
mm += 60
hh -= 1
Wend
While hh < 0
hh += 24
Wend
mm += ss \ 60
hh += mm \ 60
ss Mod= 60
mm Mod= 60
hh Mod= 24
Return Right("0" & Str(hh), 2) & ":" & Right("0" & Str(mm), 2) & ":" & _
Right("0" & Str(ss), 2) & "," & Right("000" & Str(ttt), 3)
End Function
Sub syncSubtitles(fileIn As String, fileOut As String, secs As Integer)
Dim As String linea, pio, fin, nl, fmt
Dim As Ubyte f1, f2
fmt = "hh:MM:ss,ttt"
f1 = Freefile
Open fileOut For Output As #f1
f2 = Freefile
Open fileIn For Input As #f2
While Not Eof(f2)
Line Input #f2, linea
If Instr(linea, "-->") > 0 Then
pio = Mid(linea, 1, 12)
pio = addSeconds(pio, secs)
fin = Mid(linea, 18, 12)
fin = addSeconds(fin, secs)
Print #f1, pio; " --> "; fin
Else
Print #f1, linea
End If
Wend
Close #f2, #f1
End Sub
Dim As String linea
Dim As Ubyte f = Freefile
Print !"After fast-forwarding 9 seconds:\n"
syncSubtitles("movie.srt", "movie_corrected.srt", 9)
Open "movie_corrected.srt" For Input As #f
While Not Eof(f)
Line Input #f, linea
Print linea
Wend
Close #f
Print !"\n\nAfter rolling-back 9 seconds:\n"
syncSubtitles("movie.srt", "movie_corrected2.srt", -9)
Open "movie_corrected2.srt" For Input As #f
While Not Eof(f)
Line Input #f, linea
Print linea
Wend
Close #f
Sleep
- Output:
Same as Wren entry.
Phix
Needed a bugfix to adjust_timedate() for time-only handling, in that a y,m,d of 0,0,0 pre-dates the introduction of the Gregorian calendar. There was also a floor(milliseconds) which should have been/is now round(milliseconds). Fixes can be grabbed from github if needed.
requires("1.0.6") -- (a time-only handling bugfix in adjust_timedate)
constant movie_srt = `
1
00:01:31,550 --> 00:01:36,347
Four billion years ago,
00:01:36,555 --> 00:01:42,019
00:01:42,144 --> 00:01:43,979
00:01:43,979 --> 00:01:45,898
00:01:46,232 --> 00:01:47,608
00:01:47,733 --> 00:01:50,361
00:01:50,736 --> 00:01:52,488
00:01:58,035 --> 00:01:58,869
00:01:59,203 --> 00:02:00,079
00:02:02,540 --> 00:02:03,999
00:02:04,416 --> 00:02:07,419
00:02:07,503 --> 00:02:11,048
With our advanced technology, I shrank it down.
`,
sep = ` --> `,
fmt = `hh:mm:ss,ms`
include timedate.e
for line in split(movie_srt,'\n') do
if match(sep,line) then
line = split(line,sep)
for i,t in line do
timedate td = parse_date_string(t,{fmt})
line[i] = format_timedate(adjust_timedate(td,9),fmt)
end for
line = join(line,sep)
end if
printf(1,"%s\n",line)
end for
- Output:
1 00:01:40,550 --> 00:01:45,347 Four billion years ago, 00:01:45,555 --> 00:01:51,019 00:01:51,144 --> 00:01:52,979 00:01:52,979 --> 00:01:54,898 00:01:55,232 --> 00:01:56,608 00:01:56,733 --> 00:01:59,361 00:01:59,736 --> 00:02:01,488 00:02:07,035 --> 00:02:07,869 00:02:08,203 --> 00:02:09,079 00:02:11,540 --> 00:02:12,999 00:02:13,416 --> 00:02:16,419 00:02:16,503 --> 00:02:20,048 With our advanced technology, I shrank it down.
Wren
import "./date" for Date
import "./ioutil" for File, FileUtil
var syncSubtitles = Fn.new { |fileIn, fileOut, secs|
var nl = FileUtil.lineBreak
var fmt = "hh|:|MM|:|ss|,|ttt"
var f = File.create(fileOut)
for (line in FileUtil.readLines(fileIn)) {
if (line.contains("-->")) {
var start = line[0..11]
var startDate = Date.parse(start, fmt).addSeconds(secs)
start = startDate.format(fmt)
var end = line[17..28]
var endDate = Date.parse(end, fmt).addSeconds(secs)
end = endDate.format(fmt)
f.writeBytes(start + " --> " + end + nl)
} else {
f.writeBytes(line + nl)
}
}
f.close()
}
System.print("After fast-forwarding 9 seconds:\n")
syncSubtitles.call("movie.srt", "movie_corrected.srt", 9)
System.print(File.read("movie_corrected.srt"))
System.print("After rolling-back 9 seconds:\n")
syncSubtitles.call("movie.srt", "movie_corrected2.srt", -9)
System.print(File.read("movie_corrected2.srt"))
- Output:
After fast-forwarding 9 seconds: 1 00:01:40,550 --> 00:01:45,347 Four billion years ago, the first marine life forms. 2 00:01:45,555 --> 00:01:51,019 First came the fish...then slowly other life forms evolved. 3 00:01:51,144 --> 00:01:52,979 Therefore, our ancestors... 4 00:01:52,979 --> 00:01:54,898 ...came from fish. 5 00:01:55,232 --> 00:01:56,608 Everyone, come and see this. 6 00:01:56,733 --> 00:01:59,361 Cretaceous Tyrannosaurus. 7 00:01:59,736 --> 00:02:01,488 Ferocious! 8 00:02:07,035 --> 00:02:07,869 Red, 9 00:02:08,203 --> 00:02:09,079 Pong! 10 00:02:11,540 --> 00:02:12,999 Isn't this a gecko? 11 00:02:13,416 --> 00:02:16,419 How else can I get a 15 ton T-Rex in here? 12 00:02:16,503 --> 00:02:20,048 With our advanced technology, I shrank it down. After rolling-back 9 seconds: 1 00:01:22,550 --> 00:01:27,347 Four billion years ago, the first marine life forms. 2 00:01:27,555 --> 00:01:33,019 First came the fish...then slowly other life forms evolved. 3 00:01:33,144 --> 00:01:34,979 Therefore, our ancestors... 4 00:01:34,979 --> 00:01:36,898 ...came from fish. 5 00:01:37,232 --> 00:01:38,608 Everyone, come and see this. 6 00:01:38,733 --> 00:01:41,361 Cretaceous Tyrannosaurus. 7 00:01:41,736 --> 00:01:43,488 Ferocious! 8 00:01:49,035 --> 00:01:49,869 Red, 9 00:01:50,203 --> 00:01:51,079 Pong! 10 00:01:53,540 --> 00:01:54,999 Isn't this a gecko? 11 00:01:55,416 --> 00:01:58,419 How else can I get a 15 ton T-Rex in here? 12 00:01:58,503 --> 00:02:02,048 With our advanced technology, I shrank it down.