fix(video): develop

This commit is contained in:
zhangheng
2026-02-12 17:30:29 +08:00
parent 311a92f3fd
commit 6a5dbd776f
25 changed files with 229 additions and 54 deletions

View File

@@ -61,7 +61,7 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
})
const mountDir = "/input_mount"
const inputPath = `${mountDir}/${file.name}`
let inputPath = `${mountDir}/${file.name}`
const mountInput = async () => {
await ffmpeg.createDir(mountDir)
@@ -69,12 +69,64 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
}
const unmountInput = async () => {
await ffmpeg.unmount(mountDir).catch(() => {})
await ffmpeg.deleteDir(mountDir).catch(() => {})
await ffmpeg.unmount(mountDir).catch((err) => {
console.log("unmounterr", err)
})
await ffmpeg.deleteDir(mountDir).catch((err) => {
console.log("deleteDir", err)
})
}
await mountInput()
const isRawH264 =
file.name.toLowerCase().endsWith(".264") ||
file.name.toLowerCase().endsWith(".h264")
if (isRawH264) {
ctx.postMessage({
type: "LOG",
message: "[fix] 检测到 H.264 裸流,正在封装为 MP4 以修复索引...",
})
const remuxedPath = "fixed_container.mp4"
try {
// -f h264: 强制指定输入格式为 h264 裸流
// -i ... : 输入
// -c copy: 直接复制流,不转码(速度极快)
// -f mp4 : 输出为 MP4 容器
const ret = await ffmpeg.exec([
"-f",
"h264",
"-i",
inputPath,
"-c",
"copy",
remuxedPath,
])
if (ret === 0) {
ctx.postMessage({
type: "LOG",
message: "[fix] 封装成功,切换输入源",
})
// 关键:将后续操作的输入路径指向新生成的 MP4
inputPath = remuxedPath
} else {
ctx.postMessage({
type: "LOG",
message: "[fix] 封装失败,尝试继续使用原始文件...",
})
}
} catch (e) {
ctx.postMessage({
type: "LOG",
message: `[fix] 预处理出错: ${String(e)}`,
})
}
}
try {
const normalizeError = (e: unknown) => {
if (typeof e === "string") return e
@@ -145,7 +197,9 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
message: `[probe] ffprobe failed: ${normalizeError(e)}`,
})
} finally {
await ffmpeg.deleteFile(durationProbePath).catch(() => {})
await ffmpeg.deleteFile(durationProbePath).catch((err) => {
console.log("deleteFile error", err)
})
}
if (!durationSec) {
@@ -388,7 +442,9 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
)}`,
})
}
await ffmpeg.deleteFile(outputName).catch(() => {})
await ffmpeg.deleteFile(outputName).catch((err) => {
console.log("deleteFile error", err)
})
index += 1
continue
}
@@ -397,7 +453,9 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
const raw = (await ffmpeg.readFile(outputName)) as Uint8Array
const copy = raw.slice()
sentBytes += copy.byteLength
await ffmpeg.deleteFile(outputName).catch(() => {})
await ffmpeg.deleteFile(outputName).catch((err) => {
console.log("deleteFile error", err)
})
ctx.postMessage(
{
@@ -484,13 +542,10 @@ ctx.onmessage = async (event: MessageEvent<WorkerMessage>) => {
})
}
} catch (e2) {
let flag = typeof e2 === "string"
ctx.postMessage({
type: "LOG",
message: `[fatal] reload failed: ${
typeof e2 === "string"
? e2
: (e2 as any)?.message || String(e2)
}`,
message: `[fatal] reload failed: ${flag ? e2 : (e2 as any)?.message || String(e2)}`,
})
}