用于 SRT 字幕的 Java API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5062914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java API for SRT subtitles
提问by FearUs
Is there any Java API for SRT subtitles ?
是否有用于 SRT 字幕的 Java API?
回答by Panayotis
The actual SRT parsing is performed through regular expressions, which Java is able to manipulate.
实际的 SRT 解析是通过 Java 能够操作的正则表达式执行的。
The actual regexp is:
实际的正则表达式是:
protected static final String nl = "\\n";
protected static final String sp = "[ \t]*";
Pattern.compile("(?s)(\d+)" + sp + nl + "(\d{1,2}):(\d\d):(\d\d),(\d\d\d)" + sp + "-->"+ sp + "(\d\d):(\d\d):(\d\d),(\d\d\d)" + sp + "(X1:\d.*?)??" + nl + "(.*?)" + nl + nl);
group 2, 3, 4, and 5 is start time group 6, 7, 8, and 9 is finish time group 11 is subtitle text
第 2、3、4、5 组为开始时间 第 6、7、8、9 组为结束时间 第 11 组为字幕文本
回答by Daren
I have produced a java logic with which to parse and read different subtitle formats, among them is the popular srt: you can find the code licensed under MIT open source license (free to use for whatever) in my GiT repository:
我已经生成了一个 java 逻辑来解析和读取不同的字幕格式,其中包括流行的 srt:您可以在我的 GiT 存储库中找到在 MIT 开源许可下许可的代码(免费用于任何目的):
https://github.com/JDaren/subtitleConverter
https://github.com/JDaren/subtitleConverter
You probably just need the basic classes and the SRTFormat class, and with that you can read srt files from an InputStream or get full String[] files once you've finished editing them.
您可能只需要基本类和 SRTFormat 类,这样您就可以从 InputStream 读取 srt 文件或在完成编辑后获取完整的 String[] 文件。
If you do find this useful or I can help you with anything please contact me.
如果您觉得这很有用,或者我可以帮助您做任何事情,请与我联系。
PS: (other supported formats, either partially or fully are .ASS .SSA .STL .SCC and .XML (from W3C's TTAF-DFXP also known as TTML 1.0)
PS:(其他支持的格式,部分或全部是 .ASS .SSA .STL .SCC 和 .XML(来自 W3C 的 TTAF-DFXP,也称为 TTML 1.0)
EDIT:
编辑:
you can find the logic at work in www.subtitleconverter.net
您可以在www.subtitleconverter.net 中找到工作中的逻辑
回答by privatejava
Actually the modified regex from @Panayotis
that supports multi-line subtitle text is like this:
实际上@Panayotis
,支持多行字幕文本的修改后的正则表达式是这样的:
protected static final String nl = "\n";
protected static final String sp = "[ \t]*";
Pattern.compile(
"(\d+)" + sp + nl
+ "(\d{1,2}):(\d\d):(\d\d),(\d\d\d)" + sp
+ "-->" + sp + "(\d\d):(\d\d):(\d\d),(\d\d\d)" + sp
+ "(X1:\d.*?)??" + nl + "([^\|]*?)" + nl + nl);
Replace ([^\\|]*?)
with any character which have less probability to come as subtitle text. I have currently used "|" character negation rule.
替换([^\\|]*?)
为任何不太可能作为字幕文本出现的字符。我目前使用了“|” 字符否定规则。