Sync subtitles: Difference between revisions
Content added Content deleted
(Added C++) |
(Added various BASIC dialects (Gambas y PureBasic)) |
||
Line 373: | Line 373: | ||
{{out}} |
{{out}} |
||
<pre>Same as Wren entry.</pre> |
<pre>Same as Wren entry.</pre> |
||
==={{header|Gambas}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vbnet">Function addSeconds(timeStr As String, secs As Integer) As String |
|||
Dim hh As Integer, mm As Integer, ss As Integer, ttt As Integer |
|||
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 = ss Mod 60 |
|||
mm = mm Mod 60 |
|||
hh = 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 linea As String, pio As String, fin As String, fmt As String |
|||
Dim f1 As File, f2 As File |
|||
fmt = "hh:MM:ss,ttt" |
|||
f1 = Open fileOut For Output |
|||
f2 = Open fileIn For Input |
|||
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 #f1 |
|||
Close #f2 |
|||
End Sub |
|||
Public Sub Main() |
|||
Dim linea As String |
|||
Dim f As File |
|||
Print "After fast-forwarding 9 seconds:\n" |
|||
syncSubtitles("movie.srt", "movie_corrected.srt", 9) |
|||
f = Open "movie_corrected.srt" For Input |
|||
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) |
|||
f = Open "movie_corrected2.srt" For Input |
|||
While Not Eof(f) |
|||
Line Input #f, linea |
|||
Print linea |
|||
Wend |
|||
Close #f |
|||
End </syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|PureBasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="purebasic">Procedure.s addSeconds(timeStr.s, secs) |
|||
Protected hours, minutes, seconds, milliseconds |
|||
Protected.s hh, mm, ss, ttt |
|||
hh = Mid(timeStr, 1, 2) |
|||
hours = Val(hh) |
|||
mm = Mid(timeStr, 4, 2) |
|||
minutes = Val(mm) |
|||
ss = Mid(timeStr, 7, 2) |
|||
seconds = Val(ss) |
|||
seconds + secs |
|||
ttt = Mid(timeStr, 10, 3) |
|||
If seconds < 0 |
|||
seconds + 60 |
|||
minutes - 1 |
|||
EndIf |
|||
If minutes < 0 |
|||
minutes + 60 |
|||
hours - 1 |
|||
EndIf |
|||
If hours < 0 |
|||
hours + 24 |
|||
EndIf |
|||
ProcedureReturn Str(hours) + ":" + Str(minutes) + ":" + Str(seconds) + "," + ttt |
|||
EndProcedure |
|||
Procedure syncSubtitles(fileIn.s, fileOut.s, secs) |
|||
Protected line.s, pio.s, fin.s |
|||
OpenFile(0, fileIn) |
|||
CreateFile(1, fileOut) |
|||
While Not Eof(0) |
|||
line = ReadString(0) |
|||
If FindString(line, "-->", 1) |
|||
pio = Mid(line, 1, 12) |
|||
fin = Mid(line, 18, 12) |
|||
pio = addSeconds(pio, secs) |
|||
fin = addSeconds(fin, secs) |
|||
WriteStringN(1, pio + " --> " + fin) |
|||
Else |
|||
WriteStringN(1, line) |
|||
EndIf |
|||
Wend |
|||
CloseFile(0) |
|||
CloseFile(1) |
|||
EndProcedure |
|||
OpenConsole() |
|||
PrintN("After fast-forwarding 9 seconds:" + #CRLF$) |
|||
syncSubtitles("movie.srt", "movie_corrected.srt", 9) |
|||
OpenFile(0, "movie_corrected.srt") |
|||
While Not Eof(0) |
|||
PrintN(ReadString(0)) |
|||
Wend |
|||
CloseFile(0) |
|||
PrintN(#CRLF$ + #CRLF$ + "After rolling-back 9 seconds:" + #CRLF$) |
|||
syncSubtitles("movie.srt", "movie_corrected2.srt", -9) |
|||
OpenFile(0, "movie_corrected2.srt") |
|||
While Not Eof(0) |
|||
PrintN(ReadString(0)) |
|||
Wend |
|||
CloseFile(0) |
|||
PrintN(#CRLF$ + "Press ENTER to exit"): Input() |
|||
CloseConsole()</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|QB64}}=== |
==={{header|QB64}}=== |