Coder Social home page Coder Social logo

ffmpeg-generate-video-preview's Introduction

ffmpeg-generate-video-preview

Generates an attractive image strip or GIF preview from a video.

NPM Build Status JavaScript Style Guide


Example 6x5 image strip preview (video credit Big Buck Bunny).

Image Strip Example

generate-video-preview big-buck-bunny.avi output.jpg --width 160 --rows 5 --cols 6

Example 6x5 image strip preview with black padding and margin.

Image Strip Example with Padding

generate-video-preview big-buck-bunny.avi output.jpg --width 160 --rows 5 --cols 6 --padding 4 --margin 4

Example gif preview with 30 frames at 4 fps.

GIF Example

generate-video-preview big-buck-bunny.avi output.gif --width 320 --num-frames 30 --gif-fps 4

Install

npm install --save ffmpeg-generate-video-preview

# or if you want to use the CLI
npm install -g ffmpeg-generate-video-preview

This module requires ffmpeg to be installed.

If you want to generate GIFs, you must also install gifski. On Mac OS, you can run

brew install gifski

CLI

  Usage: generate-video-preview [options] <input> <output>

  Options:

    -V, --version                 output the version number
    -w, --width <width>           frame width
    -h, --height <height>         frame height
    -q, --quality <n>             frame image quality
    -n, --num-frames <n>          number of frames to capture
    -p, --num-frames-percent <n>  number of frames to capture as % of overall frames
    -P, --padding <n>             image strip tile padding (default: 0)
    -M, --margin <n>              image strip border margin (default: 0)
    -r, --rows <n>                image strip number of rows
    -c, --cols <n>                image strip number of cols
    -C, --color <color>           image strip background color (default: Black)
    -f, --gif-fps <n>             gifski fps (default: 10)
    -Q, --gif-quality <n>         gifski quality (default: 80)
    -F, --gif-fast                enable gifski fast mode
    -h, --help                    output usage information

Usage

const generatePreview = require('ffmpeg-generate-video-preview')

const metadata = await generatePreview({
  input: 'media/1.mp4',
  output: 'preview.gif',
  width: 128
})

console.log(metadata)
// => {
//   output: 'preview.gif',
//   numFrames: 5,
//   width: 128,
//   height: 72
// }

API

generatePreview(options)

Generates a preview image strip or GIF from a video file. Returns a Promise for when the output file(s) have been written.

Note that some options only target image strips, and some only target GIFs.

options

input

Type: String

Path or URL to a video file.

output

Type: String

Path to a jpg, png, or gif file to output. If output is a gif, then a GIF will be created via gifski; otherwise, an image strip will be generated according to rows and cols.

width

Type: Number

Scales the input to the specified width. If height isn't specified, the aspect ratio of the input will be preserved.

height

Type: Number

Scales the input to the specified height. If width isn't specified, the aspect ratio of the input will be preserved.

quality

Type: Number Default: 2

If exporting a jpeg image, the quality from 1-31 with 31 being the worst quality (source).

If exporting a GIF, this will dictate the image quality of the intermediate frames, not the final output gif quality. Also see gifski.quality.

numFrames

Type: Number

Specify an exact number of frames to capture. Note that if you specify rows and cols for image strips, this value will be overridden.

numFramesPercent

Type: Number Default: 0.05

Specify a percentage of frames to capture as a floating point number between 0 and 1. Defaults to 5%. Note that if you specify numFrames or rows and cols for image stripsgifs, this value will be overridden.

rows

Type: Number Default: 1 (Image Strips Only)

Number of rows to generate in an image strip. See the ffmpeg tile filter for details.

Defaults to one long horizontal image strip with one row.

cols

Type: Number (Image Strips Only)

Number of columns to generate in an image strip. See the ffmpeg tile filter for details.

Note that if cols isn't specified, it will be inferred. Also note that specifying both rows and cols is the easiest way to specify the number of frames to generate in an image strip.

padding

Type: Number Default: 0 (Image Strips Only)

Adds a padding between each image in an image strip. See the ffmpeg tile filter for details.

margin

Type: Number Default: 0 (Image Strips Only)

Adds an outer margin to an image strip. See the ffmpeg tile filter for details.

color

Type: String Default: Black (Image Strips Only)

Background color for unused areas of the image strip. May be any valid ffmpeg color syntax string.

gifski

Type: Object Default: { fps: 10, quality: 80, fast: false } (GIFs Only)

Gifski customization options. Run gifski -h for details.

log

Type: Function Default: noop

Optional function to log the underlying ffmpeg and gifski command(s). You may, for example, use console.log

Related

License

MIT © Travis Fischer

ffmpeg-generate-video-preview's People

Contributors

adu avatar transitive-bullshit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ffmpeg-generate-video-preview's Issues

fps parameter is static

$ generate-video-preview bbb_sunflower_1080p_30fps_normal.mp4 output.gif --width 320 --num-frames 60 --fps 30
{ cmd:
   'ffmpeg -i bbb_sunflower_1080p_30fps_normal.mp4 -y -vsync vfr -q:v 2 -vf select=not(mod(n\\,634)),scale=320:-1 /tmp/5f868a01dc1e7dd62f5e66902812d43e/frame-%d.png' }
gifski -o output.gif --fps 10 --quality 80 --quiet /tmp/5f868a01dc1e7dd62f5e66902812d43e/frame-*.png
{
  "output": "output.gif",
  "numFrames": 30,
  "width": 320,
  "height": 180
}

Whatever I set as fps param, it still use 10 as value. 60 frames as 30 fps should give me a gif of 2 sec right ?

EDIT: ok just figured out the ffmpeg command does not take 30 side by side frames, the gif is 2sec long but with random frames.

EDIT2: working with -f

[Feature] webp video preview

It would be awesome if it was possible to generate a webp video preview of the video, like this

A small extract of x seconds (set by a param) from the middle or the begining of the video.

Arbitrary Code Injection Vulnerability

Description

Improper sanitation of the filename may result in a malformed output file name, which could be used to execute arbitrary code. All versions of this package are vulnerable.

Steps to reproduce

Run this script in Node.js environment and you will be able to see that it copies /etc/passwd file to your pwd.

const generatePreview = require('ffmpeg-generate-video-preview')


generatePreview({
  input: 'sample.avi',
  output: '$(cp${IFS}/etc/passwd${IFS}.)preview.gif',
  width: 128,
})

https://www.youtube.com/watch?v=C6mNDnQVfkg

Recommendation

We need to properly sanitize the output file name/directory (other user inputs too) prior to executing it on the shell.
https://github.com/transitive-bullshit/ffmpeg-generate-video-preview/blob/master/lib/index.js#L126

Unable to Specify Output Path in CLI

It throws a Make error when given an ouput path or file name with blank spaces
generate-video-preview "Input Video.mp4" "Output File.gif" --width 320 --num-frames 15 --gif-fps 5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.