Skip to content

Adds forceSARRatio option to add 'scale=iw*sar:ih' to video-filter list. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,7 +28,10 @@ module.exports = async (opts) => {
offsets,
fps,
numFrames,
ffmpegPath
ffmpegPath,
forceSARRatio,
startOffset,
duration,
} = opts

if (!input) throw new Error('missing required input')
Expand All @@ -34,10 +46,26 @@ 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 (startOffset) {
cmd.addInputOptions(["-ss", parseInt(startOffset, 10)]);
}

if (duration) {
cmd.addInputOptions(["-t", parseInt(duration, 10)]);
}

if (timestamps || offsets) {
const folder = outputPath.dir
const filename = outputPath.base

applyVFList(cmd, vfList)

return new Promise((resolve, reject) => {
cmd
.on('end', () => resolve(output))
Expand All @@ -55,13 +83,16 @@ 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([
'-vsync', 'vfr',
'-vf', `select=not(mod(n\\,${nthFrame}))`
])
vfList.push(`select=not(mod(n\\,${nthFrame}))`)
}

if (outputPath.ext === '.raw') {
Expand All @@ -70,6 +101,9 @@ module.exports = async (opts) => {
])
}


applyVFList(cmd, vfList)

return new Promise((resolve, reject) => {
cmd
.on('end', () => resolve(output))
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"contributors": [
{
"name" : "Adam Hunter"
, "url" : "https://github.com/adamrhunter"
}
],
"license": "MIT",
"reveal": true,
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +115,25 @@ 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.

##### 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.
Expand Down