-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoder.tsx
212 lines (189 loc) · 8.33 KB
/
encoder.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Progress, Spinner } from "@material-tailwind/react"
import PromiseHandler from "~components/PromiseHandler"
import { FFMpegHooks, useFFMpeg, type FFMpegProgress } from "~hooks/ffmpeg"
import injectToaster from "~toaster"
import { useEffect, useState } from "react"
import { toast } from "sonner"
import BJFThemeProvider from "~components/BJFThemeProvider"
import { useAsyncEffect, useTimeoutElement } from "~hooks/life-cycle"
import { useShardReceiver } from "~hooks/stream"
import '~style.css'
import { downloadBlob } from "~utils/file"
import { toTimer } from "~utils/misc"
import { toArrayBuffer } from "~utils/binary"
const urlParams = new URLSearchParams(window.location.search);
const ffmpegID = urlParams.get('id')
injectToaster()
export type ConverterProps = {
ffmpeg: FFMpegHooks
}
function Converter(props: ConverterProps): JSX.Element {
const { ffmpeg } = props
const [progress, setProgress] = useState<FFMpegProgress>(null)
const [result, setResult] = useState<Blob>(null)
const [error, setError] = useState<Error>(null)
const {
info,
progress: infoProgress,
error: infoError,
ready
} = useShardReceiver(ffmpegID, 'pages')
useEffect(() => {
if (result || error) return
const beforeUnload = (e: BeforeUnloadEvent) => {
e.preventDefault()
e.returnValue = ''
}
window.addEventListener('beforeunload', beforeUnload)
return () => window.removeEventListener('beforeunload', beforeUnload)
}, [result, error])
useAsyncEffect(
async () => {
console.info('info is null: ', !info)
console.info('ffmpeg is null: ', !ffmpeg)
if (!ffmpeg) return
if (!info) {
ready('content-script')
console.info('正在等待视频数据...')
return
}
console.info('开始编译视频...')
ffmpeg.onProgress(progress => {
console.info('received progress: ', progress)
setProgress(() => progress)
if (progress.progress > 0 && progress.progress < 1) {
setError(null) // 如果有进度,一律当没有错误
}
})
const original = new Blob(info.content, { type: info.videoInfo.mimeType })
const fixed = await ffmpeg.fixInfoAndCut(original, info.duration, info.videoInfo.extension)
const buffer = toArrayBuffer(fixed)
setResult(() => new Blob([buffer], { type: info.videoInfo.mimeType }))
downloadBlob(new Blob([buffer], { type: info.videoInfo.mimeType }), info.filename)
},
async () => { },
(err) => {
toast.error('编译视频时发生错误')
console.error('unexpected error: ', err)
setError(err)
},
[ffmpeg, info]
)
if (error) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center text-red-500">
<h1>编译视频时发生错误</h1>
<div>{error?.message ?? new String(error)}</div>
</div>
)
} else if (infoError) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center text-red-500">
<h1>获取视频数据时发生错误</h1>
<div>{infoError?.message ?? new String(infoError)}</div>
</div>
)
}
if (!info) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center dark:text-white space-y-2">
<div>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-10 h-10 dark:stroke-white animate-bounce">
<path strokeLinecap="round" strokeLinejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
</div>
<h1>正在等待视频数据...</h1>
<Progress color="blue" value={infoProgress} className="w-1/2" />
</div>
)
}
if (!progress) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center space-y-2 dark:text-white">
<Spinner className="w-10 h-10" />
<div>编译准备中...</div>
</div>
)
}
const action = progress.stage === 'fix' ? '修复' : '编译'
if (progress.progress === 1) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center dark:text-white space-y-2">
<div>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-10 h-10 stroke-green-400">
<path strokeLinecap="round" strokeLinejoin="round" d="m4.5 12.75 6 6 9-13.5" />
</svg>
</div>
<h1>视频已{action}完成。</h1>
<h3>{info.filename}</h3>
</div>
)
}
console.debug('progress: ', progress)
const progressInvalid = progress.progress > 1 || progress.progress < 0
return (
<div className="h-screen w-screen flex justify-center items-center flex-col space-y-2 dark:text-white">
<div className="flex flex-row items-center space-x-2">
<div>
<Spinner className="h-10 w-10" />
</div>
<div>
{`${action}视频中... ${progressInvalid ? '' : `(${Math.round(progress.progress * 10000) / 100}%)`}`}
</div>
<div>
时间段: {toTimer(Math.round(progress.time / 1000000))}
</div>
</div>
{!progressInvalid && <Progress className="w-1/2" color="blue" value={progress.progress * 100} />}
</div>
)
}
function App(): JSX.Element {
const { ffmpeg } = useFFMpeg()
if (!ffmpegID) {
return (
<div className="w-screen h-screen flex flex-col justify-center items-center text-red-500 text-lg">
<h1>无效的请求</h1>
</div>
)
}
return (
<BJFThemeProvider>
<main className="text-lg">
<PromiseHandler promise={ffmpeg}>
<PromiseHandler.Loading>
<div className="w-screen h-screen flex flex-col space-y-2 justify-center items-center">
<Spinner className="h-10 w-10" color="blue" />
<div className="dark:text-white">正在加载 FFMpeg...</div>
</div>
</PromiseHandler.Loading>
<PromiseHandler.Error>
{err => (
<div className="w-screen h-screen flex flex-col justify-center items-center text-red-500">
<h1>FFMpeg 加载失败</h1>
<div>{err?.message ?? err}</div>
</div>
)}
</PromiseHandler.Error>
<PromiseHandler.Response>
{(ff) => useTimeoutElement(
(
<div className="w-screen h-screen flex flex-col justify-center items-center dark:text-white space-y-2">
<div>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-10 h-10 stroke-green-400">
<path strokeLinecap="round" strokeLinejoin="round" d="m4.5 12.75 6 6 9-13.5" />
</svg>
</div>
<h1>FFMpeg 已成功加载。</h1>
</div>
),
<Converter ffmpeg={ff} />,
2000
)}
</PromiseHandler.Response>
</PromiseHandler>
</main>
</BJFThemeProvider>
)
}
export default App