Skip to content

Commit

Permalink
FEAT: SubRip Subtitle codec (loading/saving *.srt files)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Jan 23, 2025
1 parent 0b7c672 commit ab7ba22
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/boot/sysobj.reb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ modules: object [
csv: https://src.rebol.tech/mezz/codec-csv.reb
ico: https://src.rebol.tech/mezz/codec-ico.reb
pdf: https://src.rebol.tech/mezz/codec-pdf.reb
srt: https://src.rebol.tech/mezz/codec-srt.reb
swf: https://src.rebol.tech/mezz/codec-swf.reb
xml: https://src.rebol.tech/mezz/codec-xml.reb
json: https://src.rebol.tech/mezz/codec-json.reb
Expand Down
82 changes: 82 additions & 0 deletions src/mezz/codec-srt.reb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
REBOL [
Name: srt
Type: module
Options: [delay]
Version: 0.0.1
Title: "Codec: SRT"
Author: Rights: "Oldes"
History: [23-Jan-2025 "Oldes" {Initial version}]
Usage: [
;- Example how to modify subtitle timestamps
srt: load %example.srt
forall srt [
srt: change srt srt/1 - 0:0:16.4
srt: change srt srt/1 - 0:0:16.4
]
save %example-fixed.srt srt
]
]

invalid-data: "Data not in a correct format!"

register-codec [
name: 'srt
type: 'application
title: "SubRip Subtitle"
suffixes: [%.srt]

decode: function [
"Converts SRT file data to Rebol block: [time1 time2 subtitle ...]"
srt [file! url! binary! string!]
][
case [
any [file? srt url? srt] [ srt: read/string srt ]
binary? srt [ srt: deline to string! srt ]
]
ch_digits: system/catalog/bitsets/numeric
result: make block! 1000
unless attempt [
parse srt [
any [SP | LF]
any [
some ch_digits LF
copy time1: to SP " --> " copy time2: to LF skip
copy text: to "^/^/" 2 skip
(repend result [transcode/one time1 transcode/one time2 text])
]
]
][
sys/log/error 'SRT invalid-data
return none
]
new-line/skip result true 3
result
]

encode: function [
"Converts block of subtitles to a SRT output"
data [block!]
][
result: make string! 50000
count: 0
unless parse data [
any [
set time1: time!
set time2: time!
set text: string!
(
++ count
append result ajoin [
count LF
time1 " --> " time2 LF
text LF LF
]
)
]
][
sys/log/error 'SRT invalid-data
return none
]
result
]
]

0 comments on commit ab7ba22

Please sign in to comment.