Sync subtitles: Difference between revisions

Content added Content deleted
(Added various BASIC dialects (BASIC256, Chipmunk Basic and QB64) Moved FreeBASIC to section BASIC)
Line 159: Line 159:
</pre>
</pre>


=={{header|FreeBASIC}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">f = freefile
print "After fast-forwarding 9 seconds:" + chr(10)
call syncSubtitles("movie.srt", "movie_corrected.srt", 9)
open f, "movie_corrected.srt"
while not eof(f)
linea = readline (f)
print linea;
end while
close

print chr(10) + chr(10)
print "After rolling-back 9 seconds:" + chr(10)
call syncSubtitles("movie.srt", "movie_corrected2.srt", -9)
open f, "movie_corrected2.srt"
while not eof(f)
linea = readline (f)
print linea;
end while
close
end

function addSeconds (timestring, secs)
hh = int(mid(timestring, 1, 2))
mm = int(mid(timestring, 4, 2))
ss = int(mid(timestring, 7, 2)) + secs
ttt = int(mid(timestring, 10, 3))

while ss < 0
ss = ss + 60
mm = mm - 1
end while
while mm < 0
mm = mm + 60
hh = hh - 1
end while
while hh < 0
hh = hh + 24
end while

mm = mm + ss \ 60
hh = hh + mm \ 60
ss = ss mod 60
mm = mm mod 60
hh = hh mod 24
return right("0" + string(hh), 2) + ":" + right("0" + string(mm), 2) + ":" + right("0" + string(ss), 2) + "," + right("000" + string(ttt), 3)
end function

subroutine syncSubtitles (fileIn, fileOut, secs)
fmt = "hh:MM:ss,ttt"
f1 = freefile
open f1, fileOut
f2 = freefile
open f2, fileIn
while not eof(f2)
linea = readline (f2)
if instr(linea, "-->") > 0 then
pio = mid(linea, 1, 12)
pio = addSeconds(pio, secs)
fin = mid(linea, 18, 12)
fin = addSeconds(fin, secs)
write f1, pio; " --> "; fin + chr(10)
else
write f1, linea
end if
end while
close
end subroutine</syntaxhighlight>

==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 print "After fast-forwarding 9 seconds:";chr$(10)
120 syncsubtitles("movie.srt","movie_corrected.srt",9)
130 open "movie_corrected.srt" for input as #1
140 while not eof(1)
150 line input #1,linea$
160 print linea$
170 wend
180 close #1
190 print
200 print "After rolling-back 9 seconds:";chr$(10)
210 syncsubtitles("movie.srt","movie_corrected2.srt",-9)
220 open "movie_corrected2.srt" for input as #1
230 while not eof(1)
240 line input #1,linea$
250 print linea$
260 wend
270 close #1
280 end
290 sub addseconds$(timestr$,secs)
300 hh = val(mid$(timestr$,1,2))
310 mm = val(mid$(timestr$,4,2))
320 ss = val(mid$(timestr$,7,2))+secs
330 ttt = val(mid$(timestr$,10,3))
340 while ss < 0
350 ss = ss+60
360 mm = mm-1
370 wend
380 while mm < 0
390 mm = mm+60
400 hh = hh-1
410 wend
420 while hh < 0
430 hh = hh+24
440 wend
450 mm = mm+int(ss/60)
460 hh = hh+int(mm/60)
470 ss = ss mod 60
480 mm = mm mod 60
490 hh = hh mod 24
500 addseconds$ = right$("0"+str$(hh),2)+":"+right$("0"+str$(mm),2)+":"+right$("0"+str$(ss),2)+","+right$("000"+str$(ttt),3)
510 end sub
520 sub syncsubtitles(filein$,fileout$,secs)
530 fmt$ = "hh:MM:ss,ttt"
540 open fileout$ for output as #1
550 open filein$ for input as #2
560 while not eof(2)
570 line input #2,linea$
580 if instr(linea$,"-->") > 0 then
590 pio$ = mid$(linea$,1,12)
600 pio$ = addseconds$(pio$,secs)
610 fin$ = mid$(linea$,18,12)
620 fin$ = addseconds$(fin$,secs)
630 print #1,pio$;" --> ";fin$
640 else
650 print #1,linea$
660 endif
670 wend
680 close #2
690 close #1
700 end sub</syntaxhighlight>

==={{header|FreeBASIC}}===
{{trans|Wren}}
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Function addSeconds(timeStr As String, secs As Integer) As String
<syntaxhighlight lang="vbnet">Function addSeconds(timeStr As String, secs As Integer) As String
Line 237: Line 373:
{{out}}
{{out}}
<pre>Same as Wren entry.</pre>
<pre>Same as Wren entry.</pre>

==={{header|QB64}}===
<syntaxhighlight lang="vbnet">Dim As String linea
f = FreeFile

Print "After fast-forwarding 9 seconds:"; Chr$(10)
Call 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 Chr$(10); Chr$(10); "After rolling-back 9 seconds:"; Chr$(10)
Call 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

Function addSeconds$ (timeStr As String, secs As Integer)
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 = ss + 60
mm = mm - 1
Wend
While mm < 0
mm = mm + 60
hh = hh - 1
Wend
While hh < 0
hh = hh + 24
Wend

mm = mm + ss \ 60
hh = hh + mm \ 60
ss = ss Mod 60
mm = mm Mod 60
hh = hh Mod 24
addSeconds$ = 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, fmt
Dim As Integer 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</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==