-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[Bug] Fix missing subtitle text in manually downloaded *.SRT files. (issue #10030) #12575
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
Changes from 2 commits
2c35db7
e1888ed
22ee01b
3516667
71aa6d5
d311fae
300afde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,36 @@ private void writeString(final String text) throws IOException { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.write(text.getBytes(charset)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // CHECKSTYLE:OFF checkstyle:JavadocStyle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // checkstyle does not understand that span tags are inside a code block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>Recursive method to extract text from all nodes.</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This method processes {@link TextNode}s and {@code <br>} tags, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * recursively extracting text from nested tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * (e.g. extracting text from nested {@code <span>} tags). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Newlines are added for {@code <br>} tags. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * </p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param node the current node to process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param text the {@link StringBuilder} to append the extracted text to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void extractText(final Node node, final StringBuilder text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (node instanceof TextNode textNode) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text.append((textNode).text()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (node instanceof Element element) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // <br> is a self-closing HTML tag used to insert a line break. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (element.tagName().equalsIgnoreCase("br")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Add a newline for <br> tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text.append(NEW_LINE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Recursively process child nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final Node child : node.childNodes()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| extractText(child, text); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The build method could be rewritten like this:
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But does it convert
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! I gave it a try locally, but it failed to build (built with
In addition, the recursive For this reason, I’d prefer to keep the current approach
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Android has a built-in Pair type. Also, the current Jsoup version in the project has the method. @TobiGr You're right, but it works fine for the subtitles from the linked videos. Alternatively, the map operation could be changed to use the existing logic.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly don't see a good reason to use streams here. It makes the whole code harder to read and I am not sure if we achieve an increased performance by using a stream here. If stream is significantly faster here, we could make use of it though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I changed the parameter to String as well, forgot to add that. I accidentally edited your reply, sorry about that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching to The Switching to For these reasons, I recommend continuing to use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the paragraph.text() definition from https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/nodes/Element.java#L1552 public String text() {
final StringBuilder accum = StringUtil.borrowBuilder();
new TextAccumulator(accum).traverse(this);
return StringUtil.releaseBuilder(accum).trim();
}And it uses the trim() function. Since it removes whitespace, it is not suitable for subtitles.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the implementation in this PR is already good enough. @Isira-Seneviratne If you do not have any objections, I'd proceed with merging this PR.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I am also in favour of keeping the current code |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // CHECKSTYLE:ON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void build(final SharpStream ttml) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * TTML parser with BASIC support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -74,21 +104,15 @@ public void build(final SharpStream ttml) throws IOException { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Elements paragraphList = doc.select("body > div > p"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // check if has frames | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (paragraphList.size() < 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (paragraphList.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final Element paragraph : paragraphList) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text.setLength(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Recursively extract text from all child nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| extractText(paragraph, text); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ignoreEmptyFrames && text.length() < 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.