Skip to content

Commit

Permalink
Update for Windows version release
Browse files Browse the repository at this point in the history
  • Loading branch information
jackychu0830 committed Dec 29, 2023
1 parent 1615106 commit 627b89f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
https://youtu.be/OJCRbiwMmlQ

1. 安裝 Java 11 執行環境 (下載: https://www.microsoft.com/openjdk)
2. 下載剪映字幕工具箱式 [2.3.290 版](https://github.com/jackychu0830/jy-srt-tools/releases/download/2.3.290-Win/jy-srt-tools-2.3.290-Win.jar)
3. 滑鼠雙擊 jy-srt-tools-2.3.290-Win.jar 執行程式
2. 下載剪映字幕工具箱式 [2.4.510](https://github.com/jackychu0830/jy-srt-tools/releases/download/2.3.290-Win/jy-srt-tools-2.4.510-Win.jar)
3. 滑鼠雙擊 jy-srt-tools-2.4.510-Win.jar 執行程式

## 使用說明
https://youtu.be/td1EO3I1jJA
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>app.jackychu</groupId>
<artifactId>jy-srt-tools</artifactId>
<packaging>jar</packaging>
<version>2.3.290</version>
<version>2.4.510</version>

<name>jy-srt-tools</name>
<description>The SRT tools for JianyingPro video editor</description>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/app/jackychu/jysrttools/DraftTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import app.jackychu.jysrttools.ui.ErrorMessagePanel;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -37,7 +38,8 @@ public static String getDefaultSubtitleFormat() {

String format = "${text}";
try {
InputStream in = DraftTemplates.class.getClassLoader().getResourceAsStream("draft_templates/subtitle_format.txt");
InputStream in = DraftTemplates.class.getClassLoader().getResourceAsStream("draft_templates/draft_subtitle_content.json");
// InputStream in = new FileInputStream("/Users/jacky.chu/workspace/jy-srt-tools/src/main/resources/draft_templates/draft_subtitle_content.json");
byte[] data = in.readAllBytes();
format = new String(data);
format = format.replace("${font_path}", fontPath);
Expand Down
83 changes: 15 additions & 68 deletions src/main/java/app/jackychu/jysrttools/JyDraft.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.jackychu.jysrttools;

import app.jackychu.jysrttools.exception.JySrtToolsException;
import com.github.houbb.heaven.util.util.JsonUtil;
import lombok.*;
import org.apache.commons.text.StringSubstitutor;
import org.json.simple.JSONArray;
Expand All @@ -24,71 +25,9 @@ public class JyDraft {
private long lastModifiedTime;
private boolean hidden = false;

// @Setter(AccessLevel.NONE)
// @Getter(AccessLevel.NONE)
// id: text
// private Map<String, String> texts;

// @Setter(AccessLevel.NONE)
// @Getter(AccessLevel.NONE)
// private List<String> textIds;

@Getter(AccessLevel.NONE)
private List<Subtitle> subtitles;

// /**
// * Get all texts in this draft
// *
// * @return Map of texts
// * @throws JySrtToolsException Parse draft info json error
// */
// public Map<String, String> getDraftTexts() throws JySrtToolsException {
// // Force clean exist texts
// this.texts = new HashMap<>();
// this.textIds = new ArrayList<>();
//
// if (this.info == null) {
// loadJyDraftInfo();
// }
//
// JSONArray txts = (JSONArray) ((JSONObject) this.info.get("materials")).get("texts");
// for (Object txt : txts.toArray()) {
// Object txtId = ((JSONObject) txt).get("id");
// Object txtObj = ((JSONObject) txt).get("content");
// Object txtType = ((JSONObject) txt).get("type");
// if (txtType.toString().equals("subtitle")) {
// this.texts.put(txtId.toString(), txtObj.toString());
// this.textIds.add(txtId.toString());
// }
// }
//
// return this.texts;
// }

// /**
// * Update specified draft text
// *
// * @param id text id
// * @param value text
// */
// public void updateDraftText(String id, String value) {
// if (texts.containsKey(id)) {
// this.texts.put(id, value);
// this.updateDraftInfoTexts(this.texts);
// }
// }

// /**
// * Get all text ids inorder
// *
// * @return The list of text ids
// * @throws JySrtToolsException Parse draft info json error
// */
// public List<String> getDraftTextIds() throws JySrtToolsException {
// this.getDraftTexts();
// return this.textIds;
// }

@SneakyThrows
public List<Subtitle> getSubtitles() {
if (this.subtitles == null) {
Expand All @@ -110,14 +49,22 @@ public List<Subtitle> loadDraftSubtitles() throws JySrtToolsException {
}

Map<String, String> texts = new HashMap<>();
Map<String, String> contentFormats = new HashMap<>();
JSONArray txts = (JSONArray) ((JSONObject) this.info.get("materials")).get("texts");
for (Object txt : txts.toArray()) {
Object txtId = ((JSONObject) txt).get("id");
Object txtObj = ((JSONObject) txt).get("content");
Object txtType = ((JSONObject) txt).get("type");
if (txtType.toString().equals("subtitle")) {
texts.put(txtId.toString(), txtObj.toString());
// this.textIds.add(txtId.toString());
try {
String originContent = txtObj.toString();
JSONObject content = (JSONObject) (new JSONParser()).parse(originContent);
texts.put(txtId.toString(), content.get("text").toString());
content.put("text", "${text}");
contentFormats.put(txtId.toString(), content.toString());
} catch (ParseException e) {
texts.put(txtId.toString(), e.toString());
}
}
}

Expand All @@ -133,7 +80,8 @@ public List<Subtitle> loadDraftSubtitles() throws JySrtToolsException {
if (texts.containsKey(materialId)) {
Subtitle sub = new Subtitle();
sub.setId(materialId);
sub.setFormattedText(texts.get(materialId));
sub.setText(texts.get(materialId));
sub.setContentFormat(contentFormats.get(materialId));
JSONObject target = (JSONObject) ((JSONObject) segment).get("target_timerange");
sub.setDuration(Long.parseLong(target.get("duration").toString()));
sub.setStartTime(Long.parseLong(target.get("start").toString()));
Expand Down Expand Up @@ -283,14 +231,13 @@ public void replaceDraftSubtitles(List<Subtitle> newSubtitles) throws JySrtTools

String textTemp = DraftTemplates.getTemplate("draft_text");
values = new HashMap<>();
values.put("content", sub.getFormattedText());
values.put("font_path", DraftTemplates.getDefaultFontPath());
String textId = UUID.randomUUID().toString().toUpperCase();
values.put("id", textId);
String textStr = StringSubstitutor.replace(textTemp, values, "${", "}");
JSONObject textObj;
try {
textObj = (JSONObject) parser.parse(textStr);
JSONObject textObj = (JSONObject) parser.parse(textStr);
textObj.put("content", sub.getFormattedText().replace("${font_path}", DraftTemplates.getDefaultFontPath()));
originTexts.add(textObj);
} catch (ParseException e) {
throw new JySrtToolsException("新增 SRT 字幕失敗 (Text) " + System.lineSeparator() + e.getMessage(), e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/app/jackychu/jysrttools/JyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static List<Subtitle> loadSrtFile(String filename) throws JySrtToolsExcep
int num = StringUtils.isNumeric(line) ? Integer.parseInt(line) : -1;
if (num != -1) { //number
sub = new Subtitle();
sub.setFormat(format);
sub.setContentFormat(format);
subLineCount = 1;
sub.setNum(num);
} else {
Expand Down
41 changes: 22 additions & 19 deletions src/main/java/app/jackychu/jysrttools/Subtitle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.Objects;
import java.util.regex.Matcher;
Expand All @@ -20,7 +24,7 @@ public class Subtitle implements Comparable<Subtitle> {
private String findingText;
private boolean found;
private String text;
private String format;
private String contentFormat;

/**
* Convert time from ms to SRT time string format
Expand Down Expand Up @@ -81,25 +85,24 @@ public int hashCode() {
return Objects.hash(id, num, text, startTime, endTime, duration);
}

public void setFormattedText(String str) {
this.text = str;
this.format = "${text}";
Pattern pattern = Pattern.compile("<font.*>\\[([\\s\\S]*)\\]<\\/font>"); //有做任何格式更動的字幕
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
this.text = matcher.group(1).toString();
this.format = str.replace(this.text, "${text}");
} else {
pattern = Pattern.compile("<size=.*>([\\s\\S]*)<\\/size>"); // 沒做任何格式更動的字幕
matcher = pattern.matcher(str);
if (matcher.find()) {
this.text = matcher.group(1).toString();
this.format = str.replace(this.text, "${text}");
}
public String getFormattedText() {
try {
JSONObject content = (JSONObject) (new JSONParser()).parse(this.contentFormat);
content.put("text", this.text);
JSONArray range = new JSONArray();
range.add(0);
range.add(this.text.length());
JSONArray styles = (JSONArray) content.get("styles");
JSONObject style = (JSONObject) styles.get(0);
style.put("range", range);
styles.set(0, style);
content.put("styles", styles);
return content.toString();
} catch (ParseException e) {
e.printStackTrace();
return this.contentFormat.replace("${text}", this.text);
}
}

public String getFormattedText() {
return this.format.replace("${text}", this.text);

}
}
4 changes: 2 additions & 2 deletions src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=2.3.290
jy_version=2.9.0
version=2.4.510
jy_version=5.1.0
30 changes: 30 additions & 0 deletions src/main/resources/draft_templates/draft_subtitle_content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"styles": [
{
"fill": {
"alpha": 1.0,
"content": {
"render_type": "solid",
"solid": {
"alpha": 1.0,
"color": [
1.0,
1.0,
1.0
]
}
}
},
"font": {
"id": "",
"path": "${font_path}"
},
"range": [
0,
10
],
"size": 5.0
}
],
"text": "${text}"
}
1 change: 0 additions & 1 deletion src/main/resources/draft_templates/subtitle_format.txt

This file was deleted.

0 comments on commit 627b89f

Please sign in to comment.