Update index.js
This commit is contained in:
120
index.js
120
index.js
@@ -4,6 +4,24 @@ module.exports = async options => {
|
|||||||
let seekResolve
|
let seekResolve
|
||||||
let extractOffsets = false
|
let extractOffsets = false
|
||||||
|
|
||||||
|
// For minification
|
||||||
|
|
||||||
|
// Options
|
||||||
|
const _call = 'call'
|
||||||
|
const _count = 'count'
|
||||||
|
const _width = 'width'
|
||||||
|
const _height = 'height'
|
||||||
|
const _length = 'length'
|
||||||
|
const _format = 'format'
|
||||||
|
const _onLoad = 'onLoad'
|
||||||
|
const _offsets = 'offsets'
|
||||||
|
const _endTime = 'endTime'
|
||||||
|
const _duration = 'duration'
|
||||||
|
const _startTime = 'startTime'
|
||||||
|
const _onProgress = 'onProgress'
|
||||||
|
const _currentTime = 'currentTime'
|
||||||
|
const _createElement = 'createElement'
|
||||||
|
|
||||||
const frames = []
|
const frames = []
|
||||||
|
|
||||||
const isNumber = n => {
|
const isNumber = n => {
|
||||||
@@ -11,7 +29,7 @@ module.exports = async options => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isTimestamp = timestamp => {
|
const isTimestamp = timestamp => {
|
||||||
return isNumber(timestamp) && +timestamp >= 0 && +timestamp <= video.duration
|
return isNumber(timestamp) && +timestamp >= 0 && +timestamp <= video[_duration]
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPrototypeOf = (constructor, value) => {
|
const isPrototypeOf = (constructor, value) => {
|
||||||
@@ -19,13 +37,13 @@ module.exports = async options => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fallbackToDefault = (property, defaultValue) => {
|
const fallbackToDefault = (property, defaultValue) => {
|
||||||
options[property] = hasOwnProperty.call(options, property) ? options[property] : defaultValue
|
options[property] = hasOwnProperty[_call](options, property) ? options[property] : defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasOwnProperty = Object.prototype.hasOwnProperty
|
const hasOwnProperty = {}.hasOwnProperty
|
||||||
|
|
||||||
// Buffer Video Element
|
// Buffer Video Element
|
||||||
const video = document.createElement('video')
|
const video = document[_createElement]('video')
|
||||||
video.src = options.url
|
video.src = options.url
|
||||||
video.crossOrigin = 'anonymous'
|
video.crossOrigin = 'anonymous'
|
||||||
video.onseeked = async () => {
|
video.onseeked = async () => {
|
||||||
@@ -35,108 +53,108 @@ module.exports = async options => {
|
|||||||
error = true
|
error = true
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((video.duration === Infinity || isNaN(video.duration)) && video.readyState < 2) {
|
while ((video[_duration] === Infinity || isNaN(video[_duration])) && video.readyState < 2) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 100))
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
video.currentTime = 10000000 * Math.random()
|
video[_currentTime] = 10000000 * Math.random()
|
||||||
if (error) { break }
|
if (error) { break }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set currentTime to duration / 2
|
// Set currentTime to duration / 2
|
||||||
// (fix for correctly invoking onseeked event for currentTime = 0 if the first frame is at 0 sec.)
|
// (fix for correctly invoking onseeked event for currentTime = 0 if the first frame is at 0 sec.)
|
||||||
video.currentTime = video.duration / 2
|
video[_currentTime] = video[_duration] / 2
|
||||||
await new Promise(resolve => { seekResolve = resolve })
|
await new Promise(resolve => { seekResolve = resolve })
|
||||||
|
|
||||||
// Set options to default values if not set
|
// Set options to default values if not set
|
||||||
fallbackToDefault('format', 'image/png')
|
fallbackToDefault(_format, 'image/png')
|
||||||
fallbackToDefault('offsets', [])
|
fallbackToDefault(_offsets, [])
|
||||||
fallbackToDefault('startTime', 0)
|
fallbackToDefault(_startTime, 0)
|
||||||
fallbackToDefault('endTime', video.duration)
|
fallbackToDefault(_endTime, video[_duration])
|
||||||
fallbackToDefault('count', 1)
|
fallbackToDefault(_count, 1)
|
||||||
fallbackToDefault('onLoad', false)
|
fallbackToDefault(_onLoad, false)
|
||||||
fallbackToDefault('onProgress', false)
|
fallbackToDefault(_onProgress, false)
|
||||||
|
|
||||||
// Filter out invalid offsets
|
// Filter out invalid offsets
|
||||||
if (!isPrototypeOf(Array, options.offsets)) { options.offsets = [] } else {
|
if (!isPrototypeOf(Array, options[_offsets])) { options[_offsets] = [] } else {
|
||||||
options.offsets = options.offsets.filter(offset => {
|
options[_offsets] = options[_offsets].filter(offset => {
|
||||||
return isTimestamp(offset)
|
return isTimestamp(offset)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.offsets.length !== 0) { extractOffsets = true }
|
if (options[_offsets][_length] !== 0) { extractOffsets = true }
|
||||||
|
|
||||||
// Check if start and end times are valid
|
// Check if start and end times are valid
|
||||||
if (!isTimestamp(options.startTime)) { options.startTime = 0 }
|
if (!isTimestamp(options[_startTime])) { options[_startTime] = 0 }
|
||||||
|
|
||||||
if (!isTimestamp(options.endTime)) { options.endTime = video.duration }
|
if (!isTimestamp(options[_endTime])) { options[_endTime] = video[_duration] }
|
||||||
|
|
||||||
// Float values
|
// Float values
|
||||||
options.startTime = +options.startTime
|
options[_startTime] = +options[_startTime]
|
||||||
options.endTime = +options.endTime
|
options[_endTime] = +options[_endTime]
|
||||||
|
|
||||||
if (options.startTime >= options.endTime) {
|
if (options[_startTime] >= options[_endTime]) {
|
||||||
options.startTime = options.endTime
|
options[_startTime] = options[_endTime]
|
||||||
options.count = 1
|
options[_count] = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert count value to a positive integer (floor() or 0 if string)
|
// Convert count value to a positive integer (floor() or 0 if string)
|
||||||
options.count = Math.abs(~~options.count)
|
options[_count] = Math.abs(~~options[_count])
|
||||||
if (options.count === 0) { options.count = 1 }
|
if (options[_count] === 0) { options[_count] = 1 }
|
||||||
if (extractOffsets) { options.count = options.offsets.length }
|
if (extractOffsets) { options[_count] = options[_offsets][_length] }
|
||||||
|
|
||||||
// Starting at startTime and ending at endTime - interval
|
// Starting at startTime and ending at endTime - interval
|
||||||
const interval = (options.endTime - options.startTime) / options.count
|
const interval = (options[_endTime] - options[_startTime]) / options[_count]
|
||||||
|
|
||||||
// Set Width and Height
|
// Set Width and Height
|
||||||
let isWidthSet = hasOwnProperty.call(options, 'width')
|
let isWidthSet = hasOwnProperty[_call](options, 'width')
|
||||||
let isHeightSet = hasOwnProperty.call(options, 'height')
|
let isHeightSet = hasOwnProperty[_call](options, 'height')
|
||||||
const videoDimensionRatio = video.videoWidth / video.videoHeight
|
const videoDimensionRatio = video.videoWidth / video.videoHeight
|
||||||
|
|
||||||
// Reset Width and Height if not valid
|
// Reset Width and Height if not valid
|
||||||
if (isWidthSet && !isNumber(options.width)) { isWidthSet = false }
|
if (isWidthSet && !isNumber(options[_width])) { isWidthSet = false }
|
||||||
|
|
||||||
if (isHeightSet && !isNumber(options.height)) { isHeightSet = false }
|
if (isHeightSet && !isNumber(options[_height])) { isHeightSet = false }
|
||||||
|
|
||||||
if (!isWidthSet && !isHeightSet) {
|
if (!isWidthSet && !isHeightSet) {
|
||||||
// Both Width and Height not set
|
// Both Width and Height not set
|
||||||
options.width = 128 // Default Value (randomly set)
|
options[_width] = 128 // Default Value (randomly set)
|
||||||
options.height = options.width / videoDimensionRatio
|
options[_height] = options[_width] / videoDimensionRatio
|
||||||
} else if (isWidthSet && !isHeightSet) {
|
} else if (isWidthSet && !isHeightSet) {
|
||||||
// Width set but Height not set
|
// Width set but Height not set
|
||||||
options.height = options.width / videoDimensionRatio
|
options[_height] = options[_width] / videoDimensionRatio
|
||||||
} else if (!isWidthSet && isHeightSet) {
|
} else if (!isWidthSet && isHeightSet) {
|
||||||
// Height set but Width not set
|
// Height set but Width not set
|
||||||
options.width = options.height * videoDimensionRatio
|
options[_width] = options[_height] * videoDimensionRatio
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float values
|
// Float values
|
||||||
options.width = +options.width
|
options[_width] = +options[_width]
|
||||||
options.height = +options.height
|
options[_height] = +options[_height]
|
||||||
|
|
||||||
// Reset onLoad and onProgress functions if not valid
|
// Reset onLoad and onProgress functions if not valid
|
||||||
if (!isPrototypeOf(Function, options.onLoad)) { options.onLoad = false }
|
if (!isPrototypeOf(Function, options[_onLoad])) { options[_onLoad] = false }
|
||||||
|
|
||||||
if (!isPrototypeOf(Function, options.onProgress)) { options.onProgress = false }
|
if (!isPrototypeOf(Function, options[_onProgress])) { options[_onProgress] = false }
|
||||||
|
|
||||||
if (options.onLoad) { options.onLoad() }
|
if (options[_onLoad]) { options[_onLoad]() }
|
||||||
|
|
||||||
// Buffer Canvas Element
|
// Buffer Canvas Element
|
||||||
const canvas = document.createElement('canvas')
|
const canvas = document[_createElement]('canvas')
|
||||||
const context = canvas.getContext('2d')
|
const context = canvas.getContext('2d')
|
||||||
canvas.width = options.width
|
canvas[_width] = options[_width]
|
||||||
canvas.height = options.height
|
canvas[_height] = options[_height]
|
||||||
|
|
||||||
const extract = async resolve => {
|
const extract = async resolve => {
|
||||||
while (index < options.count) {
|
while (index < options[_count]) {
|
||||||
video.currentTime = extractOffsets ? options.offsets[index] : options.startTime + index * interval
|
video[_currentTime] = extractOffsets ? options[_offsets][index] : options[_startTime] + index * interval
|
||||||
await new Promise(resolve => { seekResolve = resolve })
|
await new Promise(resolve => { seekResolve = resolve })
|
||||||
context.clearRect(0, 0, canvas.width, canvas.height)
|
context.clearRect(0, 0, canvas[_width], canvas[_height])
|
||||||
context.drawImage(video, 0, 0, canvas.width, canvas.height)
|
context.drawImage(video, 0, 0, canvas[_width], canvas[_height])
|
||||||
frames.push({
|
frames.push({
|
||||||
offset: video.currentTime,
|
offset: video[_currentTime],
|
||||||
image: canvas.toDataURL(options.format)
|
image: canvas.toDataURL(options[_format])
|
||||||
})
|
})
|
||||||
index++
|
index++
|
||||||
if (options.onProgress) { options.onProgress(index, options.count) }
|
if (options[_onProgress]) { options[_onProgress](index, options[_count]) }
|
||||||
}
|
}
|
||||||
resolve(frames)
|
resolve(frames)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user