Skip to content

Commit 456c496

Browse files
committed
bugfixes
1 parent a2441ac commit 456c496

File tree

6 files changed

+128
-12
lines changed

6 files changed

+128
-12
lines changed

.DS_Store

0 Bytes
Binary file not shown.

components/TitleBar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const TitleBar: React.FunctionComponent = (props) => {
100100

101101
const vtt = async () => {
102102
const files = await ipcRenderer.invoke("multi-open", "subs")
103+
//if (files?.[0]) ipcRenderer.invoke("remove-duplicate-subs", files)
103104
if (files?.[0]) ipcRenderer.invoke("extract-subtitles", files)
104105
}
105106

declarations.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ declare module "image-pixels"
1111
declare module "bing-translate-api"
1212
declare module "mkv-subtitle-extractor"
1313
declare module "srt-to-vtt"
14+
declare module "ass-to-vtt"
15+
declare module "ass-to-srt"
1416

1517
type OpenImageProps = {
1618
label: string

main.ts

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import PDFDocument from "pdfkit"
2323
import child_process from "child_process"
2424
import mkvExtractor from "mkv-subtitle-extractor"
2525
import srt2vtt from "srt-to-vtt"
26+
import ass2srt from "ass-to-srt"
2627

2728
import util from "util"
2829

@@ -46,12 +47,64 @@ const queue: Array<{started: boolean, info: any}> = []
4647
if (!fs.existsSync(path.join(__dirname, "data"))) fs.mkdirSync(path.join(__dirname, "data"))
4748
fs.writeFileSync(path.join(__dirname, "data/Helvetica.afm"), Helvetica)
4849

49-
const srtToVtt = async (subtitles: string[]) => {
50+
ipcMain.handle("remove-duplicate-subs", async (event, files: string[]) => {
51+
for (let i = 0; i < files.length; i++) {
52+
const content = fs.readFileSync(files[i]).toString().split("\n")
53+
let obj = {} as any
54+
55+
for (let j = 0; j < content.length; j++) {
56+
if (!Number.isNaN(Number(content[j][0]))) {
57+
if (obj[content[j]]) continue
58+
obj[content[j]] = content[j+1]
59+
}
60+
}
61+
62+
let newContent = "WEBVTT\n\n"
63+
64+
for (let j = 0; j < Object.keys(obj).length; j++) {
65+
const key = Object.keys(obj)[j]
66+
const value = Object.values(obj)[j]
67+
newContent += `${key}\n${value}\n\n`
68+
}
69+
70+
fs.writeFileSync(files[i], newContent)
71+
}
72+
shell.openPath(path.dirname(files[0]))
73+
})
74+
75+
const ppmToJpeg = async (files: string[]) => {
76+
for (let i = 0; i < files.length; i++) {
77+
await exec(`pnmtojpeg "${files[i]}" > "${path.dirname(files[i])}/${path.basename(files[i], path.extname(files[i]))}.jpg"`)
78+
}
79+
80+
const promiseArray: any[] = []
81+
for (let i = 0; i < files.length; i++) {
82+
promiseArray.push(new Promise<void>((resolve) => {
83+
fs.unlink(files[i], () => resolve())
84+
}))
85+
}
86+
87+
await Promise.all(promiseArray)
88+
}
89+
90+
const subToVtt = async (subtitles: string[]): Promise<any> => {
91+
const srtSubs = [] as string[]
5092
for (let i = 0; i < subtitles.length; i++) {
5193
await new Promise<void>((resolve, reject) => {
52-
const readStream = fs.createReadStream(subtitles[i]).pipe(srt2vtt())
94+
95+
if (path.extname(subtitles[i]) === ".ass") {
96+
const srt = ass2srt(fs.readFileSync(subtitles[i]))
97+
const srtDest = `${path.dirname(subtitles[i])}/${path.basename(subtitles[i], path.extname(subtitles[i]))}.srt`
98+
fs.writeFileSync(srtDest, srt)
99+
srtSubs.push(srtDest)
100+
resolve()
101+
}
102+
103+
const readStream = fs.createReadStream(subtitles[i])
104+
if (path.extname(subtitles[i]) === ".srt") readStream.pipe(srt2vtt())
53105
const writeStream = fs.createWriteStream(`${path.dirname(subtitles[i])}/${path.basename(subtitles[i], path.extname(subtitles[i]))}.vtt`)
54106
readStream.pipe(writeStream)
107+
.on("error", (e) => console.log(e))
55108
.on("end", () => resolve())
56109
.on("finish", () => resolve())
57110
})
@@ -64,6 +117,10 @@ const srtToVtt = async (subtitles: string[]) => {
64117
}))
65118
}
66119
await Promise.all(promiseArray)
120+
121+
if (srtSubs.length) {
122+
return subToVtt(srtSubs)
123+
}
67124
}
68125

69126
const extractSubtitles = async (videos: string[]) => {
@@ -84,23 +141,23 @@ const extractSubtitles = async (videos: string[]) => {
84141
ipcMain.handle("extract-subtitles", async (event, files: string[]) => {
85142
const directories = files.filter((f) => fs.lstatSync(f).isDirectory())
86143
const videos = files.filter((f) => path.extname(f).toLowerCase() === ".mkv")
87-
const subtitles = files.filter((f) => path.extname(f).toLowerCase() === ".srt")
144+
const subtitles = files.filter((f) => path.extname(f).toLowerCase() === ".srt" || path.extname(f).toLowerCase() === ".ass")
88145

89146
let openDir = ""
90147

91148
for (let i = 0; i < directories.length; i++) {
92149
const dir = directories[i]
93150
let files = fs.readdirSync(dir).map((i) => path.join(dir, i))
94151
let videos = files.filter((f) => path.extname(f).toLowerCase() === ".mkv")
95-
let subs = files.filter((f) => path.extname(f).toLowerCase() === ".srt")
152+
let subs = files.filter((f) => path.extname(f).toLowerCase() === ".srt" || path.extname(f).toLowerCase() === ".ass")
96153
if (videos.length) {
97154
await extractSubtitles(videos)
98155
let files = fs.readdirSync(dir).map((i) => path.join(dir, i))
99-
let subs = files.filter((f) => path.extname(f).toLowerCase() === ".srt")
100-
await srtToVtt(subs)
156+
let subs = files.filter((f) => path.extname(f).toLowerCase() === ".srt" || path.extname(f).toLowerCase() === ".ass")
157+
await subToVtt(subs)
101158
}
102159
if (subs.length) {
103-
await srtToVtt(subs)
160+
await subToVtt(subs)
104161
}
105162
try {
106163
fs.rmdirSync(dir)
@@ -113,13 +170,13 @@ ipcMain.handle("extract-subtitles", async (event, files: string[]) => {
113170
if (videos.length) {
114171
await extractSubtitles(videos)
115172
let subs = fs.readdirSync(path.dirname(videos[0])).map((i) => path.join(path.dirname(videos[0]), i))
116-
subs = subs.filter((f) => path.extname(f).toLowerCase() === ".srt")
117-
await srtToVtt(subs)
173+
subs = subs.filter((f) => path.extname(f).toLowerCase() === ".srt" || path.extname(f).toLowerCase() === ".ass")
174+
await subToVtt(subs)
118175
if (!openDir) openDir = videos[0]
119176
}
120177

121178
if (subtitles.length) {
122-
await srtToVtt(subtitles)
179+
await subToVtt(subtitles)
123180
if (!openDir) openDir = subtitles[0]
124181
}
125182
shell.openPath(path.dirname(openDir))
@@ -253,6 +310,12 @@ ipcMain.handle("pdf", async (event, files: string[]) => {
253310
const directories = files.filter((f) => fs.lstatSync(f).isDirectory())
254311
const PDFs = files.filter((f) => path.extname(f) === ".pdf")
255312
const images = files.filter((f) => path.extname(f).toLowerCase() === ".jpg" || path.extname(f).toLowerCase() === ".png" || path.extname(f).toLowerCase() === ".jpeg")
313+
const PPM = files.filter((f) => path.extname(f).toLowerCase() === ".ppm")
314+
315+
if (PPM.length) {
316+
await ppmToJpeg(PPM)
317+
return shell.openPath(path.dirname(PPM[0]))
318+
}
256319

257320
let openDir = ""
258321

package-lock.json

Lines changed: 50 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@
160160
},
161161
"dependencies": {
162162
"@electron/remote": "^2.0.1",
163+
"ass-to-srt": "^1.2.1",
164+
"ass-to-vtt": "^1.2.0",
163165
"bing-translate-api": "^2.2.1",
164166
"bootstrap": "^4.6.0",
165167
"electron-shortcuts": "^0.4.0",

0 commit comments

Comments
 (0)