forked from rebolsource/r3
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: SubRip Subtitle codec (loading/saving
*.srt
files)
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] |