Skip to content

Commit 74709a2

Browse files
committed
fix: embedded subtitles were loaded multiple times
1 parent b04e88f commit 74709a2

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

electron-builder.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ releaseInfo:
7070
releaseNotes: |
7171
修复视频导入失败的问题
7272
初次访问 web 版本提示优化
73+
修复内嵌字幕多次加载的问题
74+

src/main/tipc/player.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,19 @@ export const playerRoute = {
148148
getSubtitlesBody: t.procedure
149149
.input<{ path: string; index: number }>()
150150
.action(async ({ input }) => {
151-
const ffmpeg = new FFmpeg(getFilePathFromProtocolURL(input.path))
152-
return ffmpeg.extractSubtitles(input.index)
151+
try {
152+
const ffmpeg = new FFmpeg(getFilePathFromProtocolURL(input.path))
153+
const data = await ffmpeg.extractSubtitles(input.index)
154+
return {
155+
ok: 1,
156+
data,
157+
}
158+
} catch (error: any) {
159+
return {
160+
ok: 0,
161+
message: error?.message || '',
162+
}
163+
}
153164
}),
154165
matchSubtitleFile: t.procedure.input<{ path: string }>().action(async ({ input }) => {
155166
const filePath = getFilePathFromProtocolURL(input.path)
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { isWeb } from '@renderer/lib/utils'
2-
import { useEffect } from 'react'
2+
import { useEffect, useRef } from 'react'
33

4-
import { usePlayerInstance, useSubtitleInstance } from '../Context'
4+
import { usePlayerInstance } from '../Context'
55
import { useSubtitle } from '../setting/items/subtitle/hooks'
66

77
export const InitializeSubtitle = () => {
8-
const { initializeSubtitle, subtitlesInstance, isFetching } = useSubtitle()
8+
const { initializeSubtitle, isFetching } = useSubtitle()
99
const player = usePlayerInstance()
10-
const [subtitle] = useSubtitleInstance()
11-
10+
const onceRef = useRef(false)
1211
useEffect(() => {
13-
if (!player || isFetching || isWeb || subtitle) {
12+
if (isWeb) {
13+
return
14+
}
15+
if (!player || isFetching || onceRef.current) {
1416
return
1517
}
1618

19+
onceRef.current = true
1720
initializeSubtitle()
18-
return () => {
19-
subtitlesInstance?.freeTrack()
20-
}
2121
}, [player, isFetching])
2222
return null
2323
}

src/renderer/src/components/modules/player/setting/items/subtitle/hooks.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MARCHEN_PROTOCOL_PREFIX } from '@main/constants/protocol'
22
import { videoAtom } from '@renderer/atoms/player'
3+
import { useToast } from '@renderer/components/ui/toast'
34
import { db } from '@renderer/database/db'
45
import { tipcClient } from '@renderer/lib/client'
56
import { isWeb } from '@renderer/lib/utils'
@@ -21,6 +22,7 @@ export const useSubtitle = () => {
2122
const player = usePlayerInstance()
2223
const [subtitlesInstance, setSubtitlesInstance] = useSubtitleInstance()
2324
const setVideo = useSetAtom(videoAtom)
25+
const { toast } = useToast()
2426
const [isLoadingEmbeddedSubtitle, setIsLoadingEmbeddedSubtitle] = useAtom(
2527
setIsLoadingEmbeddedSubtitleAtom,
2628
)
@@ -149,13 +151,16 @@ export const useSubtitle = () => {
149151

150152
setIsLoadingEmbeddedSubtitle(true)
151153
// 通过 ipc 获取被选中的动漫内嵌字幕
152-
const subtitlePath = await tipcClient?.getSubtitlesBody({
154+
const subtitleData = await tipcClient?.getSubtitlesBody({
153155
path: url,
154156
index,
155157
})
156-
158+
const subtitlePath = subtitleData?.data
157159
if (!subtitlePath || !data?.tags?.[index]) {
158-
return
160+
toast({
161+
title: '视频内嵌字幕加载失败',
162+
})
163+
throw new Error(subtitleData?.message ?? '字幕加载失败')
159164
}
160165

161166
const newTags = [
@@ -172,10 +177,8 @@ export const useSubtitle = () => {
172177
tags: newTags,
173178
},
174179
})
175-
176180
await setSubtitlesOctopus(subtitlePath)
177-
setIsLoadingEmbeddedSubtitle(false)
178-
} catch {
181+
} finally {
179182
setIsLoadingEmbeddedSubtitle(false)
180183
}
181184
},

0 commit comments

Comments
 (0)