Jump to content

Sync subtitles: Difference between revisions

julia example
(Added COBOL)
(julia example)
Line 827:
 
</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Dates
function syncsubtitles(intext::AbstractString, sec::Integer, msec::Integer)
outtext, fmt = "", dateformat"HH:MM:SS,sss"
dsec, dmsec = Dates.Second(sec), Dates.Millisecond(msec)
for line in split(intext, r"\r?\n")
if !isempty(line) && length(begin times = split(line, " --> ") end) == 2
start, stop = DateTime.(times, fmt) .+ dsec .+ dmsec
line = join(Dates.format.((start, stop), fmt), " ==> ")
end
outtext *= line * "\n"
end
return outtext
end
 
function syncsubtitles(infile, outfile::AbstractString, sec, msec = 0)
outs = open(outfile, "w")
write(outs, syncsubtitles(read(infile, String), sec, msec))
close(outs)
end
 
println("After fast-forwarding 9 seconds:\n")
syncsubtitles("movie.srt", "movie_corrected.srt", 9)
println(read("movie_corrected.srt", String))
println("After rolling-back 9 seconds:\n")
syncsubtitles("movie.srt", "movie_corrected2.srt", -9)
println(read("movie_corrected2.srt", String))
</syntaxhighlight>{{out}}
<pre>
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.
 
</pre>
 
=={{header|Phix}}==
4,108

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.