50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import {
|
|
pickSupportedVideoMimeType,
|
|
resolveVideoDurationLimitError,
|
|
resolveVideoTargetSize,
|
|
} from '../src/utils/video-compress.ts'
|
|
|
|
test('时长策略:超过上限直接拒绝(不自动裁剪)', () => {
|
|
assert.equal(resolveVideoDurationLimitError(30, 30), '')
|
|
assert.equal(resolveVideoDurationLimitError(29.4, 30), '')
|
|
assert.match(resolveVideoDurationLimitError(31, 30), /不能超过 30 秒/)
|
|
assert.match(resolveVideoDurationLimitError(30.6, 30), /不能超过 30 秒/)
|
|
})
|
|
|
|
test('时长策略:读不到时长也要拒绝,避免上传未知长度视频', () => {
|
|
assert.match(resolveVideoDurationLimitError(0, 30), /无法读取视频时长/)
|
|
assert.match(resolveVideoDurationLimitError(Number.NaN, 30), /无法读取视频时长/)
|
|
})
|
|
|
|
test('压缩尺寸:按目标高度等比缩放且宽高为偶数', () => {
|
|
assert.deepEqual(resolveVideoTargetSize(1920, 1080, 720), { width: 1280, height: 720 })
|
|
assert.deepEqual(resolveVideoTargetSize(1080, 1920, 720), { width: 404, height: 720 })
|
|
// 原视频比目标更小:不放大
|
|
assert.deepEqual(resolveVideoTargetSize(640, 360, 720), { width: 640, height: 360 })
|
|
const odd = resolveVideoTargetSize(1081, 721, 720)
|
|
assert.equal(odd.width % 2, 0)
|
|
assert.equal(odd.height % 2, 0)
|
|
})
|
|
|
|
test('容器选择:优先 WebM(VP8),其次 VP9/WebM,最后 MP4', () => {
|
|
assert.equal(
|
|
pickSupportedVideoMimeType((mime) => true),
|
|
'video/webm;codecs=vp8,opus',
|
|
)
|
|
assert.equal(
|
|
pickSupportedVideoMimeType((mime) => mime === 'video/webm' || mime.startsWith('video/mp4')),
|
|
'video/webm',
|
|
)
|
|
assert.equal(
|
|
pickSupportedVideoMimeType((mime) => mime.startsWith('video/mp4')),
|
|
'video/mp4;codecs=avc1.42E01E,mp4a.40.2',
|
|
)
|
|
assert.equal(
|
|
pickSupportedVideoMimeType(() => false),
|
|
'',
|
|
)
|
|
})
|