Skip to content

Instantly share code, notes, and snippets.

@Reino17
Created November 15, 2021 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reino17/79bfde41e9f3dea0e102f816904a8e09 to your computer and use it in GitHub Desktop.
Save Reino17/79bfde41e9f3dea0e102f816904a8e09 to your computer and use it in GitHub Desktop.
Convert TTML examples from https://trac.ffmpeg.org/ticket/4859 to SRT with Xidel (in Bash)
$ xidel -s "https://trac.ffmpeg.org/ticket/4859" -e '//dt/a[@class="trac-rawlink"]/@href'
/raw-attachment/ticket/4859/MDR%20Exakt%2C%20die%20Story%20(20150617)%20-%20Das%20Kita-Versprechen--Anspruch%20und%20Wirklichkeit%20(720p%2C%20MDR).ttml
/raw-attachment/ticket/4859/Vorstadtweiber%20(Folge%2006%2C%20S01E06%2C%209.%20Feb.%202015)%20-%20Schamlos%20(720p%2C%20SRF).ttml
/raw-attachment/ticket/4859/narvestad_pC%25_tur_23_24-muhh59002316aw.ttml
/raw-attachment/ticket/4859/Still%20Open%20All%20Hours.ttml
$ xidel -s "https://trac.ffmpeg.org/ticket/4859" -e '//dt/a[@class="trac-rawlink"]/uri-decode(extract(@href,".+/(.+)",1))'
MDR Exakt, die Story (20150617) - Das Kita-Versprechen--Anspruch und Wirklichkeit (720p, MDR).ttml
Vorstadtweiber (Folge 06, S01E06, 9. Feb. 2015) - Schamlos (720p, SRF).ttml
narvestad_pC%_tur_23_24-muhh59002316aw.ttml
Still Open All Hours.ttml
$ xidel -s "https://trac.ffmpeg.org/ticket/4859" -f '//dt/a[@class="trac-rawlink"]/@href' --download '{uri-decode(extract($url,".+/(.+)",1))}'
================================================================================================
$ file='MDR Exakt, die Story (20150617) - Das Kita-Versprechen--Anspruch und Wirklichkeit (720p, MDR).ttml'
$ xidel -s "$file" -e 'string-to-hexBinary(substring($raw,1,1))'
EFBBBF # "UTF-8 BOM" --> "--input-format=xml" to force Xidel to parse the input as XML.
$ xidel -s --input-format=xml --ignore-namespaces "$file" -e '//p[50]/outer-xml()'
<p begin="00:02:52:14" end="00:02:55:20">
<span style="white">*Ein Turm fällt um.* </span>
<br/>
<span style="white"> </span>
<span style="green">Das kann auch passieren.</span>
</p>
------------------------------------------------------------------------------------------------
$ xidel -s --input-format=xml "$file" -e '//p[50]/(@begin,@end)'
00:02:52:14
00:02:55:20
$ xidel -s --input-format=xml "$file" -e '//p[50]/(@begin,@end) ! replace(.,"(.{8}):(.+)","$1,$20")'
00:02:52,140
00:02:55,200
$ xidel -s --input-format=xml "$file" -e '//p[50]/join((@begin,@end) ! replace(.,"(.{8}):(.+)","$1,$20")," --> ")'
00:02:52,140 --> 00:02:55,200
------------------------------------------------------------------------------------------------
$ xidel -s --input-format=xml "$file" -e '//p[50]/span'
*Ein Turm fällt um.*
Das kann auch passieren.
$ xidel -s --input-format=xml "$file" -e '//p[50]/span[normalize-space()]/normalize-space()'
*Ein Turm fällt um.*
Das kann auch passieren.
$ xidel -s --input-format=xml "$file" -e '//p[50]/span[normalize-space()]/(if (@style="white") then normalize-space() else x"<font color=&quot;{@style}&quot;>{normalize-space()}</font>")'
*Ein Turm fällt um.*
<font color="green">Das kann auch passieren.</font>
------------------------------------------------------------------------------------------------
$ xidel -s --input-format=xml "$file" -e 'for $x at $i in //p return ($i,join(($x/@begin,$x/@end) ! replace(.,"(.{8}):(.+)","$1,$20")," --> "),$x/span[normalize-space()]/(if (@style="white") then normalize-space() else x"<font color=&quot;{@style}&quot;>{normalize-space()}</font>"),"")'
1
00:00:01,090 --> 00:00:04,030
Er ist eine dt. Erfindung
und eine weltweite Erfolgsgeschichte:
2
00:00:04,030 --> 00:00:05,010
*Copyright MDR 2015
UT: C.Plänitz, S.Kups*
3
00:00:05,050 --> 00:00:05,170
der Kindergarten.
[...]
50
00:02:52,140 --> 00:02:55,200
*Ein Turm fällt um.*
<font color="green">Das kann auch passieren.</font>
[...]
484
00:29:15,100 --> 00:29:19,130
<font color="yellow">legte man in Deutschland</font>
<font color="yellow">den Ausdruck zur Seite.</font>
485
00:29:19,200 --> 00:29:22,140
*Copyright MDR 2015 UT: A. Trommer*
------------------------------------------------------------------------------------------------
$ xidel -s --input-format=xml "$file" -e '
for $x at $i in //p return (
$i,
join(($x/@begin,$x/@end) ! replace(.,"(.{8}):(.+)","$1,$20")," --> "),
$x/span[normalize-space()]/(
if (@style="white") then normalize-space()
else x"<font color=&quot;{@style}&quot;>{normalize-space()}</font>"
),
""
)
'
================================================================================================
$ file='Vorstadtweiber (Folge 06, S01E06, 9. Feb. 2015) - Schamlos (720p, SRF).ttml'
$ xidel -s "$file" -e '//p[1]/(@begin,@end)'
00:00:02.24
00:00:03.64
$ xidel -s "$file" -e '//p[1]/(@begin,@end) ! format-time(time(.),"[H01]:[m01]:[s01],[f001]")'
00:00:02,240
00:00:03,640
$ xidel -s "$file" -e '//p[1]/join((@begin,@end) ! format-time(time(.),"[H01]:[m01]:[s01],[f001]")," --> ")'
00:00:02,240 --> 00:00:03,640
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e 'for $x at $i in //p return ($i,join(($x/@begin,$x/@end) ! format-time(time(.),"[H01]:[m01]:[s01],[f001]")," --> "),$x/span/normalize-space(),"")'
1
00:00:02,240 --> 00:00:03,640
Was is los? Machst du Schlussverkauf?
- Nein!
2
00:00:03,830 --> 00:00:06,200
Mein Lieferant
hat mich hängen lassen.
3
00:00:06,270 --> 00:00:08,360
Francesco, mein Shop is leer!
[...]
766
00:47:40,850 --> 00:47:43,660
Was is? Noch nie einen Dildo g'sehn?
767
00:47:45,480 --> 00:47:46,580
Gute Nacht!
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e '
for $x at $i in //p return (
$i,
join(
($x/@begin,$x/@end) ! format-time(
time(.),"[H01]:[m01]:[s01],[f001]"
),
" --> "
),
$x/span/normalize-space(),
""
)
'
================================================================================================
$ file='narvestad_pC%_tur_23_24-muhh59002316aw.ttml'
$ xidel -s --ignore-namespaces "$file" -e '//p[2]/outer-xml()'
<p begin="00:00:04.480" dur="00:00:03.900" style="left">Ruth, du lurer sikkert på<br/>hva jeg skal gi deg ...</p>
$ xidel -s "$file" -e '//p[2]/(@begin,@dur)'
00:00:04.480
00:00:03.900
$ xidel -s "$file" -e '//p[2]/(time(@begin),time(@begin) + (time(@dur) - time("00:00:00")))'
00:00:04.48
00:00:08.38
$ xidel -s "$file" -e '//p[2]/(time(@begin),time(@begin) + (time(@dur) - time("00:00:00"))) ! format-time(.,"[H01]:[m01]:[s01],[f001]")'
00:00:04,480
00:00:08,380
$ xidel -s "$file" -e '//p[2]/join((time(@begin),time(@begin) + (time(@dur) - time("00:00:00"))) ! format-time(.,"[H01]:[m01]:[s01],[f001]")," --> ")'
00:00:04,480 --> 00:00:08,380
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e '//p[2]'
Ruth, du lurer sikkert påhva jeg skal gi deg ...
$ xidel -s "$file" -e '//p[2]/text()'
Ruth, du lurer sikkert på
hva jeg skal gi deg ...
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e 'for $x at $i in //p return ($i,join((time($x/@begin),time($x/@begin) + (time($x/@dur) - time("00:00:00"))) ! format-time(.,"[H01]:[m01]:[s01],[f001]")," --> "),$x/text(),"")'
1
00:00:00,000 --> 00:00:00,000
Copyright (C) NRK
2
00:00:04,480 --> 00:00:08,380
Ruth, du lurer sikkert på
hva jeg skal gi deg ...
3
00:00:08,680 --> 00:00:13,820
Ikke spis pepperkaker hele tida!
Men ikke legg den tilbake!
[...]
24
00:01:50,880 --> 00:01:54,880
Nei? En drill!
25
00:01:56,640 --> 00:01:59,240
God jul da, Ruth.
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e '
for $x at $i in //p return (
$i,
join(
(
time($x/@begin),
time($x/@begin) + (time($x/@dur) - time("00:00:00"))
) ! format-time(.,"[H01]:[m01]:[s01],[f001]"),
" --> "
),
$x/text(),
""
)
'
================================================================================================
$ file='Still Open All Hours.ttml'
$ xidel -s --ignore-namespaces "$file" -e '//p[position() lt 6]/outer-xml()'
<p begin="00:00:31.04" id="p0" end="00:00:33.20">Visit Santa&apos;s grotto!</p>
<p begin="00:00:33.20" id="p1" end="00:00:35.32">SHE SCREAMS</p>
<p begin="00:00:37.20" id="p2" end="00:00:39.24">HE LAUGHS</p>
<p style="s1" begin="00:00:50.40" id="p3" end="00:00:52.52"><span xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" tts:color="white">You look a right muffin, you. </span>Ssh!</p>
<p style="s1" begin="00:00:52.52" id="p4" end="00:00:55.92">Don&apos;t talk to me.<br/>I&apos;m a stranger round here.</p>
$ xidel -s "$file" -e '//p[1]/join((@begin,@end) ! format-time(time(.),"[H01]:[m01]:[s01],[f001]")," --> ")'
00:00:31,040 --> 00:00:33,200
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e 'for $x in //p[position() lt 6] return $x//text() ! normalize-space()'
Visit Santa's grotto!
SHE SCREAMS
HE LAUGHS
You look a right muffin, you.
Ssh!
Don't talk to me.
I'm a stranger round here.
$ xidel -s --ignore-namespaces "$file" -e '//style/outer-xml()'
<style xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" id="s2" style="s0" tts:color="cyan"/>
<style xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" id="s1" style="s0" tts:color="yellow"/>
<style xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" id="s3" style="s0" tts:color="lime"/>
<style xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" id="s0" tts:backgroundColor="black" tts:fontStyle="normal" tts:fontSize="16" tts:fontFamily="sansSerif" tts:color="white"/>
$ xidel -s "$file" -e 'let $color:=//style for $x in //p[position() lt 6] return $x//text() ! (if (not($x/@style) or parent::span/@tts:color="white") then normalize-space() else if (parent::span) then x"<font color=&quot;{parent::span/@tts:color}&quot;>{normalize-space(parent::span)}</font>" else x"<font color=&quot;{$color[@id=$x/@style]/@tts:color}&quot;>{normalize-space()}</font>")'
Visit Santa's grotto!
SHE SCREAMS
HE LAUGHS
You look a right muffin, you.
<font color="yellow">Ssh!</font>
<font color="yellow">Don't talk to me.</font>
<font color="yellow">I'm a stranger round here.</font>
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e 'let $color:=//style for $x at $i in //p return ($i,join(($x/@begin,$x/@end) ! format-time(time(.),"[H01]:[m01]:[s01],[f001]")," --> "),$x//text() ! (if (not($x/@style) or parent::span/@tts:color="white") then normalize-space() else if (parent::span) then x"<font color=&quot;{parent::span/@tts:color}&quot;>{normalize-space(parent::span)}</font>" else x"<font color=&quot;{$color[@id=$x/@style]/@tts:color}&quot;>{normalize-space()}</font>"),"")'
1
00:00:31,040 --> 00:00:33,200
Visit Santa's grotto!
2
00:00:33,200 --> 00:00:35,320
SHE SCREAMS
3
00:00:37,200 --> 00:00:39,240
HE LAUGHS
4
00:00:50,400 --> 00:00:52,520
You look a right muffin, you.
<font color="yellow">Ssh!</font>
5
00:00:52,520 --> 00:00:55,920
<font color="yellow">Don't talk to me.</font>
<font color="yellow">I'm a stranger round here.</font>
[...]
418
00:27:43,120 --> 00:27:49,240
<font color="cyan"># Sleep in heavenly peace</font>
419
00:27:49,240 --> 00:27:54,840
<font color="cyan"># Sleep in heavenly peace. #</font>
------------------------------------------------------------------------------------------------
$ xidel -s "$file" -e '
let $color:=//style
for $x at $i in //p return (
$i,
join(
($x/@begin,$x/@end) ! format-time(
time(.),"[H01]:[m01]:[s01],[f001]"
),
" --> "
),
$x//text() ! (
if (not($x/@style) or parent::span/@tts:color="white") then
normalize-space()
else if (parent::span) then
x"<font color=&quot;{parent::span/@tts:color}&quot;>{normalize-space(parent::span)}</font>"
else
x"<font color=&quot;{$color[@id=$x/@style]/@tts:color}&quot;>{normalize-space()}</font>"
),
""
)
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment