mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-03 02:20:49 +08:00
Compare commits
1 Commits
codex/swit
...
codex/swit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f35add587 |
@@ -17,3 +17,8 @@ NUXT_PUBLIC_GITHUB_CLIENT_ID=Ov23liVkO1NPAX5JyWxJ
|
|||||||
NUXT_PUBLIC_DISCORD_CLIENT_ID=1394985417044000779
|
NUXT_PUBLIC_DISCORD_CLIENT_ID=1394985417044000779
|
||||||
NUXT_PUBLIC_TWITTER_CLIENT_ID=ZTRTU05KSk9KTTJrTTdrVC1tc1E6MTpjaQ
|
NUXT_PUBLIC_TWITTER_CLIENT_ID=ZTRTU05KSk9KTTJrTTdrVC1tc1E6MTpjaQ
|
||||||
NUXT_PUBLIC_TELEGRAM_BOT_ID=8450237135
|
NUXT_PUBLIC_TELEGRAM_BOT_ID=8450237135
|
||||||
|
|
||||||
|
# 视频压缩配置 - FFmpeg.wasm 专用
|
||||||
|
# 支持 Chrome 60+ 和 Safari 11.1+
|
||||||
|
NUXT_PUBLIC_VIDEO_MAX_SIZE=52428800 # 50MB (字节)
|
||||||
|
NUXT_PUBLIC_VIDEO_TARGET_SIZE=20971520 # 20MB (字节)
|
||||||
@@ -1,17 +1,57 @@
|
|||||||
/**
|
/**
|
||||||
* 文件上传配置 - 简化版
|
* 文件上传配置 - 简化版
|
||||||
* 专注于 WebCodecs + MP4Box.js 视频压缩,支持 Chrome/Safari
|
* 专注于 FFmpeg.wasm 视频压缩,支持 Chrome/Safari
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 声明全局变量以避免 TypeScript 错误
|
// 声明全局变量以避免 TypeScript 错误
|
||||||
/* global useRuntimeConfig */
|
/* global useRuntimeConfig */
|
||||||
|
|
||||||
export const UPLOAD_CONFIG = {
|
// 简化的环境变量读取功能
|
||||||
VIDEO: {
|
function getEnvNumber(key, defaultValue) {
|
||||||
MAX_SIZE: 20 * 1024 * 1024, // 20mb
|
if (typeof window !== 'undefined') {
|
||||||
TARGET_SIZE: 5 * 1024 * 1024, // 5mb
|
// 客户端:尝试从 Nuxt 环境获取
|
||||||
|
try {
|
||||||
|
// 使用 globalThis 避免直接引用未定义的变量
|
||||||
|
const nuxtApp = globalThis.$nuxt || globalThis.nuxtApp
|
||||||
|
if (nuxtApp && nuxtApp.$config) {
|
||||||
|
const value = nuxtApp.$config.public?.[key.replace('NUXT_PUBLIC_', '').toLowerCase()]
|
||||||
|
return value ? Number(value) : defaultValue
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
} catch {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 服务端:从 process.env 获取
|
||||||
|
return process.env[key] ? Number(process.env[key]) : defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
// 支持的输入格式
|
function getEnvBoolean(key, defaultValue) {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
try {
|
||||||
|
// 使用 globalThis 避免直接引用未定义的变量
|
||||||
|
const nuxtApp = globalThis.$nuxt || globalThis.nuxtApp
|
||||||
|
if (nuxtApp && nuxtApp.$config) {
|
||||||
|
const value = nuxtApp.$config.public?.[key.replace('NUXT_PUBLIC_', '').toLowerCase()]
|
||||||
|
return value === 'true' || value === true
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
} catch {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const envValue = process.env[key]
|
||||||
|
return envValue ? envValue === 'true' : defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
export const UPLOAD_CONFIG = {
|
||||||
|
// 视频文件配置 - 专为 FFmpeg.wasm 优化
|
||||||
|
VIDEO: {
|
||||||
|
// 文件大小限制 (字节)
|
||||||
|
MAX_SIZE: getEnvNumber('NUXT_PUBLIC_VIDEO_MAX_SIZE', 20 * 1024 * 1024), // 5MB
|
||||||
|
TARGET_SIZE: getEnvNumber('NUXT_PUBLIC_VIDEO_TARGET_SIZE', 5 * 1024 * 1024), // 5MB
|
||||||
|
|
||||||
|
// 支持的输入格式 (FFmpeg.wasm 支持更多格式)
|
||||||
SUPPORTED_FORMATS: ['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'm4v', 'ogv'],
|
SUPPORTED_FORMATS: ['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'm4v', 'ogv'],
|
||||||
|
|
||||||
// 输出格式 - MP4 (兼容性最好)
|
// 输出格式 - MP4 (兼容性最好)
|
||||||
|
|||||||
@@ -124,11 +124,10 @@ export function createVditor(editorId, options = {}) {
|
|||||||
// 使用 WebCodecs 压缩视频
|
// 使用 WebCodecs 压缩视频
|
||||||
processedFile = await compressVideo(file, (progress) => {
|
processedFile = await compressVideo(file, (progress) => {
|
||||||
const messages = {
|
const messages = {
|
||||||
initializing: '初始化编码器',
|
initializing: '初始化编解码器',
|
||||||
preparing: '准备压缩',
|
preparing: '准备压缩',
|
||||||
analyzing: '分析视频',
|
|
||||||
compressing: '压缩中',
|
compressing: '压缩中',
|
||||||
finalizing: '完成压缩',
|
packaging: '封装中',
|
||||||
completed: '压缩完成',
|
completed: '压缩完成',
|
||||||
}
|
}
|
||||||
const message = messages[progress.stage] || progress.stage
|
const message = messages[progress.stage] || progress.stage
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ export async function compressVideo(file, onProgress = () => {}) {
|
|||||||
|
|
||||||
// 检查 WebCodecs 支持
|
// 检查 WebCodecs 支持
|
||||||
if (!isWebCodecSupported()) {
|
if (!isWebCodecSupported()) {
|
||||||
throw new Error('当前浏览器不支持视频压缩功能,请使用支持 WebCodecs 的浏览器')
|
throw new Error('当前浏览器不支持视频压缩功能,请使用 Chrome 或 Safari 浏览器')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -64,9 +64,9 @@ export async function preloadVideoCompressor() {
|
|||||||
if (!isWebCodecSupported()) {
|
if (!isWebCodecSupported()) {
|
||||||
throw new Error('当前浏览器不支持 WebCodecs')
|
throw new Error('当前浏览器不支持 WebCodecs')
|
||||||
}
|
}
|
||||||
return { success: true, message: 'WebCodecs 已就绪' }
|
return { success: true, message: 'WebCodecs 可用' }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('WebCodecs 检测失败:', error)
|
console.warn('WebCodecs 预加载失败:', error)
|
||||||
return { success: false, error: error.message }
|
return { success: false, error: error.message }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,98 +1,108 @@
|
|||||||
import MP4Box from 'mp4box'
|
/**
|
||||||
|
* WebCodecs + MP4Box.js video compressor
|
||||||
|
* Simplified transcoding using browser WebCodecs API
|
||||||
|
*/
|
||||||
|
import { createFile } from 'mp4box'
|
||||||
|
import { UPLOAD_CONFIG } from '../config/uploadConfig.js'
|
||||||
|
|
||||||
// 检查 WebCodecs 支持
|
|
||||||
export function isWebCodecSupported() {
|
export function isWebCodecSupported() {
|
||||||
return typeof window !== 'undefined' && typeof window.VideoEncoder !== 'undefined'
|
return (
|
||||||
|
typeof window !== 'undefined' &&
|
||||||
|
'VideoEncoder' in window &&
|
||||||
|
'MediaStreamTrackProcessor' in window &&
|
||||||
|
'VideoFrame' in window
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 WebCodecs + MP4Box.js 压缩视频
|
/**
|
||||||
export async function compressVideoWithWebCodecs(file, opts = {}) {
|
* Compress a video File using WebCodecs and MP4Box.js
|
||||||
const { onProgress = () => {}, width = 720, bitrate = 1_000_000 } = opts
|
* @param {File} file original video file
|
||||||
|
* @param {Object} options optional callbacks
|
||||||
|
* @param {Function} options.onProgress progress callback
|
||||||
|
* @returns {Promise<File>} compressed file
|
||||||
|
*/
|
||||||
|
export async function compressVideoWithWebCodecs(file, { onProgress = () => {} } = {}) {
|
||||||
if (!isWebCodecSupported()) {
|
if (!isWebCodecSupported()) {
|
||||||
throw new Error('当前浏览器不支持 WebCodecs')
|
throw new Error('当前浏览器不支持 WebCodecs')
|
||||||
}
|
}
|
||||||
|
|
||||||
onProgress({ stage: 'initializing', progress: 0 })
|
onProgress({ stage: 'initializing', progress: 0 })
|
||||||
|
|
||||||
// 加载原始视频
|
|
||||||
const url = URL.createObjectURL(file)
|
const url = URL.createObjectURL(file)
|
||||||
const video = document.createElement('video')
|
const video = document.createElement('video')
|
||||||
video.src = url
|
video.src = url
|
||||||
video.muted = true
|
await video.play()
|
||||||
await video.play().catch(() => {})
|
|
||||||
video.pause()
|
video.pause()
|
||||||
await new Promise((resolve) => {
|
|
||||||
if (video.readyState >= 2) resolve()
|
|
||||||
else video.onloadedmetadata = () => resolve()
|
|
||||||
})
|
|
||||||
|
|
||||||
const targetWidth = width
|
onProgress({ stage: 'preparing', progress: 10 })
|
||||||
const targetHeight = Math.round((video.videoHeight / video.videoWidth) * width)
|
|
||||||
const canvas = document.createElement('canvas')
|
const stream = video.captureStream()
|
||||||
canvas.width = targetWidth
|
const track = stream.getVideoTracks()[0]
|
||||||
canvas.height = targetHeight
|
const processor = new MediaStreamTrackProcessor({ track })
|
||||||
const ctx = canvas.getContext('2d')
|
const reader = processor.readable.getReader()
|
||||||
|
|
||||||
|
const { width, height, frameRate = 30 } = track.getSettings()
|
||||||
|
const bitrate = UPLOAD_CONFIG.VIDEO.TARGET_BITRATE || 1_000_000
|
||||||
|
|
||||||
const chunks = []
|
const chunks = []
|
||||||
const encoder = new VideoEncoder({
|
const encoder = new VideoEncoder({
|
||||||
output: (chunk) => {
|
output: (chunk) => {
|
||||||
chunks.push(chunk)
|
const copy = new Uint8Array(chunk.byteLength)
|
||||||
|
chunk.copyTo(copy)
|
||||||
|
chunks.push({ type: chunk.type, timestamp: chunk.timestamp, data: copy })
|
||||||
},
|
},
|
||||||
error: (e) => {
|
error: (e) => console.error('编码失败', e),
|
||||||
throw e
|
|
||||||
},
|
|
||||||
})
|
|
||||||
encoder.configure({
|
|
||||||
codec: 'avc1.42001E',
|
|
||||||
width: targetWidth,
|
|
||||||
height: targetHeight,
|
|
||||||
bitrate,
|
|
||||||
framerate: 30,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const duration = video.duration
|
encoder.configure({
|
||||||
const frameCount = Math.floor(duration * 30)
|
codec: 'avc1.42001E',
|
||||||
for (let i = 0; i < frameCount; i++) {
|
width,
|
||||||
video.currentTime = i / 30
|
height,
|
||||||
await new Promise((res) => (video.onseeked = res))
|
bitrate,
|
||||||
ctx.drawImage(video, 0, 0, targetWidth, targetHeight)
|
framerate: frameRate,
|
||||||
const bitmap = await createImageBitmap(canvas)
|
})
|
||||||
const frame = new VideoFrame(bitmap, { timestamp: (i / 30) * 1000000 })
|
|
||||||
encoder.encode(frame)
|
let processed = 0
|
||||||
frame.close()
|
const totalFrames = Math.ceil(video.duration * frameRate)
|
||||||
bitmap.close()
|
|
||||||
onProgress({ stage: 'compressing', progress: Math.round(((i + 1) / frameCount) * 80) })
|
while (true) {
|
||||||
|
const { done, value } = await reader.read()
|
||||||
|
if (done) break
|
||||||
|
encoder.encode(value)
|
||||||
|
value.close()
|
||||||
|
processed++
|
||||||
|
onProgress({ stage: 'compressing', progress: Math.round((processed / totalFrames) * 80) })
|
||||||
}
|
}
|
||||||
|
|
||||||
await encoder.flush()
|
await encoder.flush()
|
||||||
onProgress({ stage: 'finalizing', progress: 90 })
|
|
||||||
|
|
||||||
const mp4box = MP4Box.createFile()
|
onProgress({ stage: 'packaging', progress: 90 })
|
||||||
const track = mp4box.addTrack({
|
|
||||||
timescale: 1000,
|
const mp4 = createFile()
|
||||||
width: targetWidth,
|
const trackId = mp4.addTrack({
|
||||||
height: targetHeight,
|
id: 1,
|
||||||
|
type: 'avc1',
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
timescale: frameRate,
|
||||||
})
|
})
|
||||||
|
|
||||||
let dts = 0
|
|
||||||
chunks.forEach((chunk) => {
|
chunks.forEach((chunk) => {
|
||||||
const data = new Uint8Array(chunk.byteLength)
|
mp4.addSample(trackId, chunk.data.buffer, {
|
||||||
chunk.copyTo(data)
|
duration: 1,
|
||||||
mp4box.addSample(track, data.buffer, {
|
dts: chunk.timestamp,
|
||||||
duration: chunk.duration ? chunk.duration / 1000 : 33,
|
cts: 0,
|
||||||
dts,
|
|
||||||
cts: dts,
|
|
||||||
is_sync: chunk.type === 'key',
|
is_sync: chunk.type === 'key',
|
||||||
})
|
})
|
||||||
dts += chunk.duration ? chunk.duration / 1000 : 33
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const arrayBuffer = mp4box.flush()
|
const streamOut = mp4.getBuffer()
|
||||||
const outputFile = new File([arrayBuffer], file.name.replace(/\.[^.]+$/, '.mp4'), {
|
const outBuffer = streamOut.buffer.slice(0, streamOut.position)
|
||||||
|
const outFile = new File([outBuffer], file.name.replace(/\.[^.]+$/, '.mp4'), {
|
||||||
type: 'video/mp4',
|
type: 'video/mp4',
|
||||||
})
|
})
|
||||||
|
|
||||||
onProgress({ stage: 'completed', progress: 100 })
|
onProgress({ stage: 'completed', progress: 100 })
|
||||||
URL.revokeObjectURL(url)
|
|
||||||
return outputFile
|
return outFile
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user