Commit dc985e83 authored by Alexander Makarenko's avatar Alexander Makarenko

introduce secondsPerThumbnail and framesPerThumbnail

parent 2ee20c28
...@@ -89,10 +89,18 @@ Create thumbnails (and optionally pack them into spritesheet) and create WebVTT ...@@ -89,10 +89,18 @@ Create thumbnails (and optionally pack them into spritesheet) and create WebVTT
* Array with timemarks in seconds. E.g. ['123.123', '345.345'] * Array with timemarks in seconds. E.g. ['123.123', '345.345']
* **numThumbnails** defaults to `null` * **numThumbnails** defaults to `0`
* Number of thumbnails to generate. Used in opposite to `timemarks`. Each thumbnail moment is calculated as `source_duration * 0.9 / numThumbnails`. * Number of thumbnails to generate. Used in opposite to `timemarks`. Each thumbnail moment is calculated as `source_duration * 0.9 / numThumbnails`.
* **secondsPerThumbnail** defaults to `0`
* If specified thumbnails will be generated each `secondsPerThumbnail` seconds.
* **framesPerThumbnail** defaults to `0`
* If specified thumbnails will be generated each `framesPerThumbnail` frames.
* **spritesheet** defaults to `false` * **spritesheet** defaults to `false`
* Generate spritesheet or not. * Generate spritesheet or not.
......
...@@ -27,8 +27,8 @@ module.exports = function(source, options, callback) { ...@@ -27,8 +27,8 @@ module.exports = function(source, options, callback) {
if (!source) { if (!source) {
return callback(new Error('Source video file is not specified')) return callback(new Error('Source video file is not specified'))
} }
else if (!options.numThumbnails && !options.timemarks) { else if (!options.numThumbnails && !options.secondsPerThumbnail && !options.framesPerThumbnail && !options.timemarks) {
return callback(new Error('You should specify either timemarks or number of thumbnails to generate')) return callback(new Error('You should specify the way timemarks are calculated'))
} }
var sourceExt = path.extname(source) var sourceExt = path.extname(source)
...@@ -58,18 +58,53 @@ module.exports = function(source, options, callback) { ...@@ -58,18 +58,53 @@ module.exports = function(source, options, callback) {
metadata = data metadata = data
if (!options.timemarks) { if (!options.timemarks) {
var diff = metadata.duration * 0.9 / options.numThumbnails
, i = 0
options.timemarks = [] options.timemarks = []
}
options.bounds = [] options.bounds = []
var mark
, i = 0
if (options.timemarks.length) {
i = 0
while (mark = options.timemarks[i++]) {
options.bounds.push(mark)
}
}
else if (options.numThumbnails) {
var diff = metadata.duration * 0.9 / options.numThumbnails
i = 0
while (i < options.numThumbnails) { while (i < options.numThumbnails) {
options.bounds.push(Number(i*diff).toFixed(3)) options.bounds.push(Number(i*diff).toFixed(3))
options.timemarks.push(Number(i * diff + diff / 2).toFixed(3)) options.timemarks.push(Number(i * diff + diff / 2).toFixed(3))
i++ i++
} }
} }
else if (options.secondsPerThumbnail) {
mark = 0
while (mark < metadata.duration) {
options.bounds.push(Number(mark).toFixed(3))
options.timemarks.push(Number(mark).toFixed(3))
mark += options.secondsPerThumbnail
}
}
else if (options.framesPerThumbnail) {
mark = 0
while (mark < metadata.duration) {
options.bounds.push(Number(mark).toFixed(3))
options.timemarks.push(Number(mark).toFixed(3))
if (!metadata.fps) {
return callback(new Error('Can\'t determine video FPS.'))
}
mark += options.framesPerThumbnail / metadata.fps
}
}
if (!options.size) { if (!options.size) {
options.size = { options.size = {
......
...@@ -80,7 +80,8 @@ exports.metadata = function(source, callback) { ...@@ -80,7 +80,8 @@ exports.metadata = function(source, callback) {
return callback(null, { return callback(null, {
duration: parseFloat(metadata.format.duration), duration: parseFloat(metadata.format.duration),
width: parseInt(stream.width, 10), width: parseInt(stream.width, 10),
height: parseInt(stream.height, 10) height: parseInt(stream.height, 10),
fps: parseInt((stream.r_frame_rate || stream.avg_frame_rate).replace(/\/1/, ''), 10)
}) })
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment