-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Rewrite ttml conversion #2957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Rewrite ttml conversion #2957
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42ec6f0
ttml to srt conversion
kapodamy 845767e
StandardCharsets.UTF_8 instead of Charset.forName("utf-8")
kapodamy ed18466
Merge branch 'dev' into subtitles
kapodamy a2d3e2c
2 typo fixup
kapodamy afc362d
readability changes
kapodamy b8f7261
Merge branch 'dev' into subtitles
kapodamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
95 changes: 95 additions & 0 deletions
95
app/src/main/java/org/schabi/newpipe/streams/SrtFromTtmlWriter.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package org.schabi.newpipe.streams; | ||
|
|
||
| import org.jsoup.Jsoup; | ||
| import org.jsoup.nodes.Document; | ||
| import org.jsoup.nodes.Element; | ||
| import org.jsoup.nodes.Node; | ||
| import org.jsoup.nodes.TextNode; | ||
| import org.jsoup.parser.Parser; | ||
| import org.jsoup.select.Elements; | ||
| import org.schabi.newpipe.streams.io.SharpStream; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * @author kapodamy | ||
| */ | ||
| public class SrtFromTtmlWriter { | ||
| private static final String NEW_LINE = "\r\n"; | ||
|
|
||
| private SharpStream out; | ||
| private boolean ignoreEmptyFrames; | ||
| private final Charset charset = StandardCharsets.UTF_8; | ||
|
|
||
| private int frameIndex = 0; | ||
|
|
||
| public SrtFromTtmlWriter(SharpStream out, boolean ignoreEmptyFrames) { | ||
| this.out = out; | ||
| this.ignoreEmptyFrames = ignoreEmptyFrames; | ||
| } | ||
|
|
||
| private static String getTimestamp(Element frame, String attr) { | ||
| return frame | ||
| .attr(attr) | ||
| .replace('.', ',');// Str uses comma as decimal separator | ||
| } | ||
|
|
||
| private void writeFrame(String begin, String end, StringBuilder text) throws IOException { | ||
| writeString(String.valueOf(frameIndex++)); | ||
| writeString(NEW_LINE); | ||
| writeString(begin); | ||
| writeString(" --> "); | ||
| writeString(end); | ||
| writeString(NEW_LINE); | ||
| writeString(text.toString()); | ||
| writeString(NEW_LINE); | ||
| writeString(NEW_LINE); | ||
| } | ||
|
|
||
| private void writeString(String text) throws IOException { | ||
| out.write(text.getBytes(charset)); | ||
| } | ||
|
|
||
| public void build(SharpStream ttml) throws IOException { | ||
| /* | ||
| * TTML parser with BASIC support | ||
| * multiple CUE is not supported | ||
| * styling is not supported | ||
| * tag timestamps (in auto-generated subtitles) are not supported, maybe in the future | ||
| * also TimestampTagOption enum is not applicable | ||
| * Language parsing is not supported | ||
| */ | ||
|
|
||
| // parse XML | ||
| byte[] buffer = new byte[(int) ttml.available()]; | ||
| ttml.read(buffer); | ||
| Document doc = Jsoup.parse(new ByteArrayInputStream(buffer), "UTF-8", "", Parser.xmlParser()); | ||
|
|
||
| StringBuilder text = new StringBuilder(128); | ||
| Elements paragraph_list = doc.select("body>div>p"); | ||
|
kapodamy marked this conversation as resolved.
Outdated
|
||
|
|
||
| // check if has frames | ||
| if (paragraph_list.size() < 1) return; | ||
|
|
||
| for (Element paragraph : paragraph_list) { | ||
| text.setLength(0); | ||
|
|
||
| for (Node children : paragraph.childNodes()) { | ||
| if (children instanceof TextNode) | ||
| text.append(((TextNode) children).text()); | ||
| else if (children instanceof Element && ((Element) children).tagName().equalsIgnoreCase("br")) | ||
| text.append(NEW_LINE); | ||
| } | ||
|
|
||
| if (ignoreEmptyFrames && text.length() < 1) continue; | ||
|
|
||
| String begin = getTimestamp(paragraph, "begin"); | ||
| String end = getTimestamp(paragraph, "end"); | ||
|
|
||
| writeFrame(begin, end, text); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.