mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-27 09:41:20 -08:00
refactor(auto-thumbnail): [PKG-AUTO-THUMB-03] type internal extraction contracts
This commit is contained in:
1 parent
00e81309ca
commit
6c989deccd
20 files changed
+3968
-48
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import type { ExtractionConfig, ThumbnailOptions } from './types'
|
||||
|
||||
export function readOptions(option: ThumbnailOptions, getFallbackUrl: () => string): ExtractionConfig {
|
||||
const config = {
|
||||
url: option.url || getFallbackUrl(),
|
||||
width: option.width || 160,
|
||||
number: option.number || 100,
|
||||
scale: option.scale || 1,
|
||||
}
|
||||
if (!Number.isFinite(Number(config.width)) || config.width <= 0 || !Number.isFinite(Number(config.number)) || config.number <= 0)
|
||||
throw new TypeError('Auto-thumbnail width and number must be finite positive numbers')
|
||||
return config
|
||||
}
|
||||
|
||||
export function sheetSize(config: ExtractionConfig, video: Pick<HTMLVideoElement, 'duration' | 'videoWidth' | 'videoHeight'>) {
|
||||
if (!Number.isFinite(video.duration) || video.duration <= 0 || !Number.isFinite(video.videoWidth) || video.videoWidth <= 0 || !Number.isFinite(video.videoHeight) || video.videoHeight <= 0)
|
||||
throw new TypeError('Auto-thumbnail requires finite media duration and dimensions')
|
||||
const height = Math.floor(config.width * video.videoHeight / video.videoWidth)
|
||||
const canvasWidth = Math.trunc(config.width * 10)
|
||||
const canvasHeight = Math.trunc(height * Math.ceil(config.number / 10))
|
||||
if (![height, canvasWidth, canvasHeight].every(value => Number.isSafeInteger(value) && value > 0 && value <= 0xFFFFFFFF))
|
||||
throw new RangeError('Auto-thumbnail canvas dimensions are invalid')
|
||||
return { height, canvasWidth, canvasHeight }
|
||||
}
|
||||
Reference in new issue
Block a user