[原创作品]XSLT电影字幕转换代码(SRT2SUB) 
2009/9/30 21:23:09
阅读全文(7909) | 回复(5) | 编辑 | 精华
SRT字幕要完好,每条字幕间隔一空行,多不行,本次代码没有进行针对性处理。 支持时间轴校正(提前或延时)和帧速率转换。 准备工作:SRT转换为如下XML格式 <?xml version="1.0" encoding="gb2312"?> <root> SRT 字幕 </root> 转换代码: <?xml version="1.0" encoding="gb2312"?><!--原创:Qr,http://Qr.blogger.org.cn--><!--时间:2009/08/20--> <!--功能:转换SRT字幕为SUB字幕--> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/> <!--sub实际上是文本文件,故取method="text"--> <!--提前(-)或延后(+)时间,不调整帧速率须设tofps=60--><!--ms以毫秒为单位,1s=1000ms--> <xsl:variable name="ms" select="0"/> <!--调整帧速率以达到提前或延迟字幕请修改此值为电影的实际帧速率,默认为60。不调整时间须设ms=0--> <xsl:variable name="tofps" select="60"/> <!--电影的实际帧速率,根据实际设此值--> <xsl:variable name="fps" select="23.976"/> <xsl:variable name="br" select="'
'"/> <xsl:template match="/"> <xsl:call-template name="srtstr"> <xsl:with-param name="srtstr" select="." /> </xsl:call-template> </xsl:template> <xsl:template name="srtstr"> <xsl:param name="srtstr"/> <xsl:variable name="cache" select="substring-before($srtstr,'

')"/> <xsl:if test="string-length($cache) > 1"> <xsl:call-template name="transform"> <xsl:with-param name="transform" select="$cache" /> </xsl:call-template> <xsl:if test="contains($srtstr,'

')"> <xsl:call-template name="srtstr"> <xsl:with-param name="srtstr" select="substring-after($srtstr,'

')" /> </xsl:call-template> </xsl:if> </xsl:if> </xsl:template> <xsl:template name="transform"> <xsl:param name="transform"/> <xsl:variable name="current" select="substring-after($transform,'
')" /> <xsl:variable name="tmp" select="substring-before($current,'
')" /> <xsl:call-template name="timefix"> <xsl:with-param name="time" select="substring-before($tmp,' ')" /> </xsl:call-template> <xsl:call-template name="timefix"> <xsl:with-param name="time" select="substring-after($tmp,'> ')" /> </xsl:call-template> <xsl:call-template name="subfix"> <xsl:with-param name="temp" select="substring-after($current,'
')" /> </xsl:call-template> <xsl:value-of select="$br"/> </xsl:template> <xsl:template name="timefix"> <xsl:param name="time"/> <xsl:variable name="h" select="substring($time,1,2)"/> <xsl:variable name="m" select="substring($time,4,2)"/> <xsl:variable name="s" select="substring($time,7,2)"/> <xsl:variable name="u" select="substring($time,10)"/> <xsl:variable name="msbac" select="(($h * 3600 + $m * 60 + $s) * 1000 + $u + $ms) * ($tofps div 60) div 1000"/> <xsl:text>{</xsl:text> <xsl:value-of select="round($msbac * $fps)"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:template name="subfix"> <xsl:param name="temp"/> <xsl:choose> <xsl:when test="contains($temp,'
')"> <xsl:value-of select="substring-before($temp,'
')"/> <xsl:text>|</xsl:text> <xsl:call-template name="subfix"> <xsl:with-param name="temp" select="substring-after($temp,'
')" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$temp"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Posted by Qr on 2009/9/30 21:23:09
回复:[原创作品]XSLT电影字幕转换代码(SRT2SUB)
2009/10/7 11:56:15
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
支持了
Posted by chenlltop on 2009/10/7 11:56:15
回复:[原创作品]XSLT电影字幕转换代码(SRT2SUB)
2009/10/7 9:39:21
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
以下引用真不准在2009-10-5 0:02:37的评论: 高手啊~~我第一反应是用.NET或者JAVA的解析器做一个。。。。 节日快乐! ---------------------- .NET或者JAVA写的程序有界面当然更方便易用了,XSLT只对熟悉的人适用。用XSL写只是想让对XSL不熟悉的人知道XSL还能做这种事罢了。
Posted by Qr on 2009/10/7 9:39:21
回复:[原创作品]XSLT电影字幕转换代码(SRT2SUB)
2009/10/5 0:02:37
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
高手啊~~我第一反应是用.NET或者JAVA的解析器做一个。。。。 节日快乐!
Posted by 真不准 on 2009/10/5 0:02:37
回复:[原创作品]XSLT电影字幕转换代码(SRT2SUB)
2009/10/1 2:19:00
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
谨祝国庆、中秋快乐!
Posted by hjx_221 on 2009/10/1 2:19:00
回复:[原创作品]XSLT电影字幕转换代码(SRT2SUB)
2009/9/30 21:30:06
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
纯粹XSLT实现,没有太多实用价值,谨此作为国庆献礼,与XML FANS分享。 没有写注释,觉得不好理解,可以参考XPath中的string相关方法函数和XSL命名模板的应用。
Posted by Qr on 2009/9/30 21:30:06
发表评论: |