Skip to content

Instantly share code, notes, and snippets.

@silverbeak
Last active February 1, 2021 09:41
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 silverbeak/419dabf8d062afbe992e5802666f31d4 to your computer and use it in GitHub Desktop.
Save silverbeak/419dabf8d062afbe992e5802666f31d4 to your computer and use it in GitHub Desktop.
An XSLT transform I use for converting itt-formatted subtitles to srt-formatted subtitles
<?xml version="1.0"?>
<!--
This will convert itt-formatted subtitles to srt-formatted subtitles (or captions, if you prefer that)
Use it like so (using xsltproc in this example, feel free to use whatever engine you like):
# xsltproc itt-to-srt.xslt input_in_itt.itt > output_in_srt.srt
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ttp="http://www.w3.org/ns/ttml#parameter"
xmlns:tt_feature="http://www.w3.org/ns/ttml/feature/"
xmlns:tts="http://www.w3.org/ns/ttml#styling"
xmlns:tt_extension="http://www.w3.org/ns/ttml/extension/"
xmlns:tt_profile="http://www.w3.org/ns/ttml/profile/"
xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:ry="http://namespace.itunes.apple.com/itt/ttml-extension#ruby"
xmlns="http://www.w3.org/ns/ttml"
ttp:frameRate="50"
ttp:frameRateMultiplier="1 1"
xml:lang="en" xmlns:tt="http://www.w3.org/ns/ttml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
ttp:dropMode="nonDrop"
ttp:timeBase="smpte"
>
<xsl:output method="text" omit-xml-declaration="yes" indent="no" />
<xsl:strip-space elements="*" />
<xsl:template match="/tt:tt/tt:body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/tt:tt/tt:head">
</xsl:template>
<xsl:template match="tt:div/tt:p/tt:span/tt:br">
<xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="tt:div">
<xsl:for-each select="tt:p">
<xsl:value-of select="position()"/>
<xsl:text>&#xa;</xsl:text>
<xsl:value-of select='substring(@begin, 0, 9)' />,<xsl:value-of select='concat(substring(@begin, 10, 12), 0)' /> --> <xsl:value-of select='substring(@end, 0, 9)' />,<xsl:value-of select='concat(substring(@end, 10, 12), 0)' />
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="tt:span"/>
<xsl:text>&#xa;</xsl:text>
<xsl:text>&#xa;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
@BilalAlGhazi
Copy link

Great xsl, does the work perfectly except for one small caveat, if you have 2 lined with the same timecode, the output will be 2 entries in the SRT with the same timecode, while it is supposed to merge the 2 lines under the same timecode, as some players will do weird things when the SRT file has 2 entries with the same timecode.
I am trying to modify the code to cater for this.

@silverbeak
Copy link
Author

silverbeak commented Jan 31, 2021

I am trying to modify the code to cater for this.

Hi Bilal. That sounds awesome. I'm by no means an expert in any of this. I just hacked something together that worked for me at the time. I even forgot that I had this Gist. :)
Please feel free to improve on it. I'll be happy to update the Gist if you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment