From dbaf46b0be3549b0324dd804f86a52e4e829e1a8 Mon Sep 17 00:00:00 2001 From: Adam Hunter Date: Fri, 8 Nov 2019 10:43:02 -0500 Subject: [PATCH 1/2] Adds forceSARRatio option to add 'scale=iw*sar:ih' to video-filter list. --- index.js | 24 ++++++++++++++++++++++-- readme.md | 7 +++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 79c3a7e..591c509 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,15 @@ const probe = require('ffmpeg-probe') const noop = () => { } +const applyVFList = (cmd, list) => { + if (!Array.isArray(list) || list.length === 0) { + return + } + cmd.outputOptions([ + '-vf', list.join(',') + ]) +} + module.exports = async (opts) => { const { log = noop, @@ -19,7 +28,8 @@ module.exports = async (opts) => { offsets, fps, numFrames, - ffmpegPath + ffmpegPath, + forceSARRatio } = opts if (!input) throw new Error('missing required input') @@ -34,10 +44,18 @@ module.exports = async (opts) => { const cmd = ffmpeg(input) .on('start', (cmd) => log({ cmd })) + const vfList = [] + + if (Boolean(forceSARRatio)) { + vfList.push('scale=iw*sar:ih') + } + if (timestamps || offsets) { const folder = outputPath.dir const filename = outputPath.base + applyVFList(cmd, vfList) + return new Promise((resolve, reject) => { cmd .on('end', () => resolve(output)) @@ -60,8 +78,8 @@ module.exports = async (opts) => { cmd.outputOptions([ '-vsync', 'vfr', - '-vf', `select=not(mod(n\\,${nthFrame}))` ]) + vfList.push(`select=not(mod(n\\,${nthFrame}))`) } if (outputPath.ext === '.raw') { @@ -70,6 +88,8 @@ module.exports = async (opts) => { ]) } + applyVFList(cmd, vfList) + return new Promise((resolve, reject) => { cmd .on('end', () => resolve(output)) diff --git a/readme.md b/readme.md index ccbe5c3..6648dfa 100644 --- a/readme.md +++ b/readme.md @@ -113,6 +113,13 @@ Type: `String` Specify a path for the ffmpeg binary. +##### forceSARRatio + +Type: `Boolean` +Default: false + +Force ffmpeg to use `scale=iw*sar:ih` video filter to use SampleAspectRatio as aspect ratio for thumbnail images. + ## Related - [ffmpeg-extract-frame](https://github.com/transitive-bullshit/ffmpeg-extract-frame) - Extracts a single frame from a video. From 2c77d24fd63cc53be99ec6293b04472bc8292523 Mon Sep 17 00:00:00 2001 From: Adam Hunter Date: Fri, 24 Apr 2020 09:41:57 -0400 Subject: [PATCH 2/2] Adds startOffset and duration options Updates README with new parameter intructions Fixes bug where probe may return undefined for number of frames Adds me as contributor to package.json --- index.js | 18 ++++++++++++++++-- package.json | 8 +++++++- readme.md | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 591c509..2640dfc 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,9 @@ module.exports = async (opts) => { fps, numFrames, ffmpegPath, - forceSARRatio + forceSARRatio, + startOffset, + duration, } = opts if (!input) throw new Error('missing required input') @@ -49,6 +51,14 @@ module.exports = async (opts) => { if (Boolean(forceSARRatio)) { vfList.push('scale=iw*sar:ih') } + + if (startOffset) { + cmd.addInputOptions(["-ss", parseInt(startOffset, 10)]); + } + + if (duration) { + cmd.addInputOptions(["-t", parseInt(duration, 10)]); + } if (timestamps || offsets) { const folder = outputPath.dir @@ -73,7 +83,10 @@ module.exports = async (opts) => { ]) } else if (numFrames) { const info = await probe(input) - const numFramesTotal = parseInt(info.streams[0].nb_frames) + let numFramesTotal = parseInt(info.streams[0].nb_frames) + if (isNaN(numFramesTotal)) { + numFramesTotal = Math.ceil((info.duration / 1000) * info.fps); + } const nthFrame = (numFramesTotal / numFrames) | 0 cmd.outputOptions([ @@ -88,6 +101,7 @@ module.exports = async (opts) => { ]) } + applyVFList(cmd, vfList) return new Promise((resolve, reject) => { diff --git a/package.json b/package.json index 5fba57c..091b465 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,16 @@ { "name": "ffmpeg-extract-frames", - "version": "2.0.2", + "version": "2.0.3", "description": "Extracts frames from a video.", "main": "index.js", "repository": "transitive-bullshit/ffmpeg-extract-frames", "author": "Travis Fischer ", + "contributors": [ + { + "name" : "Adam Hunter" + , "url" : "https://github.com/adamrhunter" + } + ], "license": "MIT", "reveal": true, "scripts": { diff --git a/readme.md b/readme.md index 6648dfa..b201dd5 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,8 @@ [![NPM](https://img.shields.io/npm/v/ffmpeg-extract-frames.svg)](https://www.npmjs.com/package/ffmpeg-extract-frames) [![Build Status](https://travis-ci.com/transitive-bullshit/ffmpeg-extract-frames.svg?branch=master)](https://travis-ci.com/transitive-bullshit/ffmpeg-extract-frames) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) +> This is a fork from the orginal (which seemed to have stopped accpeting PRs). I've added some additional parameters/options to pass into the process (and fixed some bugs with frame offsets) + ## Install ```bash @@ -120,6 +122,18 @@ Default: false Force ffmpeg to use `scale=iw*sar:ih` video filter to use SampleAspectRatio as aspect ratio for thumbnail images. +##### startOffset + +Type: `Number` + +Start the frame extraction from the given offset (in seconds) + +##### duration + +Type: `Number` + +Run the extraction for only the given duration (in seconds) + ## Related - [ffmpeg-extract-frame](https://github.com/transitive-bullshit/ffmpeg-extract-frame) - Extracts a single frame from a video.