Last active
November 18, 2019 17:00
-
-
Save apb2006/736c661e18adeeeba9629aef4b2f4155 to your computer and use it in GitHub Desktop.
subtitle manipulation
This file contains hidden or 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
(:~ | |
:process srt subtitles | |
: @see https://en.wikipedia.org/wiki/SubRip | |
:) | |
module namespace s = 'quodatum.text.format.subrip'; | |
declare function s:with-numbers($lines as xs:string*) | |
as xs:string* | |
{ | |
for-each-pair($lines,1 to count($lines),function($line,$no){ ``[`{$no}`:`{ $line }`]``}) | |
}; | |
(:~ apply function $fun to id in each group :) | |
declare function s:update-id($groups as element(group)*,$fun as function(*)) | |
{ | |
$groups!( . transform with { replace value of node line[1] with $fun(line[1],position())} ) | |
}; | |
(:~ apply shift to time "00:02:46,019 --> 00:02:48,058" :) | |
declare function s:update-time($groups as element(group)*,$shift as xs:dayTimeDuration) | |
{ | |
$groups!( . transform with { | |
let $d:= tokenize(line[2]," --> ")!function($t){s:parse-time($t)+$shift}(.) | |
return replace value of node line[2] with string-join($d," --> ")} ) | |
}; | |
declare function s:group($lines as xs:string*) | |
as element(group)* | |
{ | |
for tumbling window $w in $lines | |
start $id when fn:true() | |
end $spacer when $spacer="" | |
return <group>{ $w!<line>{.}</line>}</group> | |
}; | |
declare function s:text($groups as element(group)*) | |
as xs:string*{ | |
$groups!string-join(line/string(),out:nl()) | |
}; | |
declare function s:load($src as xs:string) | |
as element(group)* | |
{ | |
unparsed-text-lines($src) => s:group() | |
}; | |
(:~ parse a time eg "00:02:46,019" :) | |
declare function s:parse-time($time as xs:string) | |
as xs:time | |
{ | |
xs:time(replace($time,",",".")) | |
}; | |
(:~ append part :) | |
declare function s:append($group1 as element(group)*,$group2 as element(group)*,$offset as xs:duration) | |
as element(group)* | |
{ | |
let $g2:=s:update-time($group2,$offset) | |
=>s:update-id(function($t,$pos){ $t+count($group1) }) | |
return ($group1, | |
$g2) | |
}; |
This file contains hidden or 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
(:~ | |
:process srt subtitles | |
: @see https://en.wikipedia.org/wiki/SubRip | |
:) | |
import module namespace s = 'quodatum.text.format.subrip' at "subrip.xqm"; | |
declare variable $src:=( | |
"La Belle Noiseuse 1 (1991).eng.srt", | |
"La Belle Noiseuse 2 (1991).eng.srt", | |
"La Belle Noiseuse 3 (1991).eng.srt" | |
); | |
declare variable $length:=("PT1H10M54.5S","PT1H25M21.5S","PT1H22M18S")!xs:dayTimeDuration(.); | |
let $s1:=s:load($src[1]) | |
let $s2:=s:load($src[2]) | |
let $s3:=s:load($src[3]) | |
let $s12:= s:append($s1,$s2,$length[1]) | |
let $s123:= s:append($s12,$s3,$length[1]+$length[2]) | |
return file:write-text(resolve-uri("AA.srt"),string-join(s:text($s123),out:nl())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment