Files
video-frames/index.js

122 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-04-29 10:04:41 +05:30
module.exports = async options => {
2022-04-29 11:15:53 +05:30
let index = 0
let error = false
let seekResolve
let extractOffsets = false
const frames = []
const isNumber = n => {
return +n + '' === n + ''
}
const isTimestamp = timestamp => {
return isNumber(timestamp) && +timestamp >= 0 && +timestamp <= video.duration
}
const fallbackToDefault = (property, defaultValue) => {
2022-04-29 16:50:08 +05:30
options[property] = hasOwnProperty.call(options, property) ? options[property] : defaultValue
2022-04-29 11:15:53 +05:30
}
2022-04-29 16:50:08 +05:30
const hasOwnProperty = Object.prototype.hasOwnProperty
2022-04-29 11:15:53 +05:30
// Buffer Video Element
const video = document.createElement('video')
video.src = options.url
video.crossOrigin = 'anonymous'
video.onseeked = async () => {
if (seekResolve) { seekResolve() }
}
video.onerror = () => {
error = true
}
while ((video.duration === Infinity || isNaN(video.duration)) && video.readyState < 2 && !error) {
await new Promise(resolve => setTimeout(resolve, 100))
video.currentTime = 10000000 * Math.random()
error = false
}
// Set options to default values if not set
fallbackToDefault('format', 'image/png')
fallbackToDefault('offsets', [])
fallbackToDefault('startTime', 0)
fallbackToDefault('endTime', video.duration)
fallbackToDefault('count', 1)
// Filter out invalid offsets
if (options.offsets.constructor !== Array) { options.offsets = [] } else {
options.offsets = options.offsets.filter(offset => {
return isTimestamp(offset)
})
}
if (options.offsets.length !== 0) { extractOffsets = true }
// Check if start and end times are valid
if (!isTimestamp(options.startTime)) { options.startTime = 0 }
if (!isTimestamp(options.endTime)) { options.endTime = video.duration }
if (options.startTime >= options.endTime) {
options.startTime = options.endTime
options.count = 1
}
// Convert count value to a positive integer (floor() or 0 if string)
options.count = Math.abs(~~options.count)
if (extractOffsets) { options.count = options.offsets.length }
// Starting at startTime + interval and ending at endTime - interval
const interval = (options.endTime - options.startTime) / (options.count + 1)
// Set Width and Height
2022-04-29 16:50:08 +05:30
let isWidthSet = hasOwnProperty.call(options, 'width')
let isHeightSet = hasOwnProperty.call(options, 'height')
2022-04-29 11:15:53 +05:30
const videoDimensionRatio = video.videoWidth / video.videoHeight
// Reset Width and Height if not valid
if (isWidthSet && !isNumber(options.width)) { isWidthSet = false }
if (isHeightSet && !isNumber(options.height)) { isHeightSet = false }
if (!isWidthSet && !isHeightSet) {
// Both Width and Height not set
options.width = 128
options.height = options.width / videoDimensionRatio
} else if (isWidthSet && !isHeightSet) {
// Width set but Height not set
options.height = options.width / videoDimensionRatio
} else if (!isWidthSet && isHeightSet) {
// Height set but Width not set
options.width = options.height * videoDimensionRatio
}
// Float values
options.width = +options.width
options.height = +options.height
// Buffer Canvas Element
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
canvas.width = options.width
canvas.height = options.height
2022-04-29 11:39:56 +05:30
const extract = async resolve => {
2022-04-29 11:15:53 +05:30
while (index < options.count) {
video.currentTime = extractOffsets ? options.offsets[index] : options.startTime + (index + 1) * interval
await new Promise(resolve => { seekResolve = resolve })
context.clearRect(0, 0, canvas.width, canvas.height)
context.drawImage(video, 0, 0, canvas.width, canvas.height)
2022-04-29 16:50:08 +05:30
frames.push({ offset: video.currentTime, image: canvas.toDataURL(options.format) })
2022-04-29 11:15:53 +05:30
index++
}
resolve(frames)
2022-04-29 11:39:56 +05:30
}
return new Promise(resolve => {
if (error) { resolve([]) }
extract(resolve)
2022-04-29 11:15:53 +05:30
})
}