Sync subtitles: Difference between revisions

(Added COBOL)
(One intermediate revision by one other user not shown)
Line 827:
 
</syntaxhighlight>
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
The following program has been written for gojq, the Go implementation of jq,
and will almost surely need adapting for use with other versions of jq.
 
'''Usage'''
 
To fast-forward the subtitles by 9 seconds:
<pre>
< movie.srt gojq -nRr --argjson seconds 9 -f sync-subtitles.jq
</pre>
where sync-subtitles.jq is the name of the file containing the jq program.
To roll-back, specify `seconds` as a negative number, e.g.:
<pre>
< movie.srt gojq -nRr --argjson seconds -9 -f sync-subtitles.jq
</pre>
<syntaxhighlight lang="jq">
def syncSubtitles($secs):
def fmt: "%H:%M:%S";
def adjust: strptime(fmt) | .[5] += $seconds | strftime(fmt);
 
if ($secs|type) != "number" then "The number of seconds must be specified as an integer" | error end
| inputs as $line
| ($line
| capture("^(?<start>[^,]*),(?<startms>[0-9]*) *--> *(?<finish>[^,]*),(?<finishms>[0-9]*)")
| "\(.start|adjust),\(.startms) --> \(.finish|adjust),\(.finishms)" )
// $line ;
 
if $seconds > 0 then
"Fast-forwarding \($seconds) seconds" | debug
| syncSubtitles($seconds)
elif $seconds == 0 then
"No resynchronization is needed" | debug
else
"Rolling-back \(-$seconds) seconds" | debug
| syncSubtitles($seconds)
end
</syntaxhighlight>
{{output}}
As shown elsewhere on this page.
 
=={{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}}==
2,502

edits