• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

đŸ–ŧ Extract frames from an animated GIF with pure JS

Please note that open-source maintenance is not my main focus at the moment.

I will not be investing significant effort in the very near future to review and address issues on this repository. However I do want my software to be useable!

If you have an issue that must be resolved for your work, please open a pull request to fix it, and send me a direct email to make sure that I see it. I ignore most messages from GitHub these days.

I'm also happy to help out if you have a question about how to use the library.

My email can be found at the top of this commit.

Keep in mind that I have a full-time job and a personal life as well as other hobbies that have taken priority over open source, so I might not respond immediately. But don't hesitate to follow up after a few days if you think I've missed your email.

gif-frames

A pure JavaScript tool for extracting GIF frames and saving to file. Works in Node or the browser. Uses get-pixels and save-pixels under the hood.

NPM

Install

npm install gif-frames

CDN scripts

If you're not using npm, you can include one of these in your HTML file:

<!-- unminified -->
<script src="https://unpkg.com/[email protected]?main=bundled"></script>

<!-- minified -->
<script src="https://unpkg.com/[email protected]?main=bundled-min"></script>

This will expose gifFrames as a global variable.

require('gif-frames')(options[, callback])

var gifFrames = require('gif-frames');
var fs = require('fs');

gifFrames({ url: 'image.gif', frames: 0 }).then(function (frameData) {
  frameData[0].getImage().pipe(fs.createWriteStream('firstframe.jpg'));
});

Options:

  • url (required): The pathname to the file, or an in-memory Buffer
  • frames (required): The set of frames to extract. Can be one of:
  • outputType (optional, default 'jpg'): Type to use for output (see type for save-pixels)
  • quality (optional): Jpeg quality (see quality for save-pixels)
  • cumulative (optional, default false): Many animated GIFs will only contain partial image information in each frame after the first. Specifying cumulative as true will compute each frame by layering it on top of previous frames. Note: the cost of this computation is proportional to the size of the last requested frame index.

The callback accepts the arguments (error, frameData).

Returns:

A Promise resolving to the frameData array (if promises are supported in the running environment)

frameData

An array of objects of the form:

{
  getImage,
  frameIndex,
  frameInfo
}

getImage()

Returns one of:

  • A drawn canvas DOM element, if options.outputType is 'canvas'
  • A data stream which can be piped to file output, otherwise

frameIndex

The index corresponding to the frame's position in the original GIF (not necessarily the same as the frame's position in the result array)

frameInfo

It is an Object with metadata of the frame. Fields:

Name Type Description
x Integer Image Left Position
y Integer Image Top Position
width Integer Image Width
height Integer Image Height
has_local_palette Boolean Image local palette presentation flag
palette_offset Integer Image palette offset
palette_size Integer Image palette size
data_offset Integer Image data offset
data_length Integer Image data length
transparent_index Integer Transparent Color Index
interlaced Boolean Interlace Flag
delay Integer Delay Time (1/100ths of a second)
disposal Integer Disposal method

See GIF spec for details

Examples

Writing selected frames to the file system in Node:

var gifFrames = require('gif-frames');
var fs = require('fs');

gifFrames(
  { url: 'image.gif', frames: '0-2,7', outputType: 'png', cumulative: true },
  function (err, frameData) {
    if (err) {
      throw err;
    }
    frameData.forEach(function (frame) {
      frame.getImage().pipe(fs.createWriteStream(
        'image-' + frame.frameIndex + '.png'
      ));
    });
  }
);

Drawing first frame to canvas in browser (and using a Promise):

var gifFrames = require('gif-frames');

gifFrames({ url: 'image.gif', frames: 0, outputType: 'canvas' })
  .then(function (frameData) {
    document.body.appendChild(frameData[0].getImage());
  }).catch(console.error.bind(console));

More Repositories

1

cassette

đŸ“ŧ A flexible media player component library for React that requires no up-front config
JavaScript
185
star
2

react-gif-player

đŸ“Ŋ A GIF component that moves when YOU want it to!
JavaScript
94
star
3

win95-media-player

đŸ’ŋ Back from 1995, and running on your website
JavaScript
72
star
4

youtube-vtt

â–ļī¸ Extract and save WebVTT closed caption tracks from YouTube videos
JavaScript
48
star
5

pico8-gpio-listener

🎙ī¸đŸ•šī¸ Listen to changes to your PICO-8 game's GPIO pins
JavaScript
42
star
6

react-dot-fragment

đŸĻ„ Use React 16's <React.Fragment> in React 15
JavaScript
21
star
7

hypergit

📡 Decentralizing the pull request
21
star
8

pico8-table-string

Store a nested Lua data table as one giant string
Lua
15
star
9

vtt-translate

🔇 🉑ī¸ Google Translate your .vtt subtitle files into other languages
JavaScript
14
star
10

pico8-messenger

đŸ’Ŧ Helps you help PICO-8 talk to things
JavaScript
14
star
11

pico8-responsive-webplayer-transform

🕹 Responsive post-processing for #pico8 web player
Python
14
star
12

pico8-to-lua

Converts PICO-8 extended Lua syntax to standard Lua syntax
Lua
11
star
13

volca-sampler

Send a new sound to your volca sample!
JavaScript
11
star
14

dat-pkg

Draft specification of a decentralized package registry and manager based on the Dat protocol
9
star
15

tiny-pico8-touch-ui

👾👌 Add touch controls to your PICO-8 game - no sweat!
JavaScript
8
star
16

steam-party-planner

🎮 💃 Compare Steam libraries with your friends
JavaScript
4
star
17

calorie_tracker_backbone

A personal food/calorie tracker using the Nutritionix API, built with Backbone.js
JavaScript
3
star
18

simple-audio-streaming-app

A web app which serves specified mp3 directories from the host machine.
JavaScript
3
star
19

react-extra-prop-types

Extra PropTypes for use with React components
JavaScript
3
star
20

3dbuzz-archiver

JavaScript
2
star
21

github-pull-requests

Fetches recent pull requests initiated by a given GitHub user, with optional filters.
JavaScript
2
star
22

recursive-json-key-transform

Apply a string transformation recursively to all keys in a JSON-compatible object.
JavaScript
2
star
23

bind-cache

➰ Bound method cache, ideal for React
JavaScript
2
star
24

redux-action-transform-middleware

Apply a transformation to a specified target on a Redux action, if that target exists.
JavaScript
2
star
25

cassette-demo-app

For js-montreal/forwardjs ottawa
JavaScript
1
star
26

wildcard-export-override-example

JavaScript
1
star
27

volca-sampler-plugins

Plugins for https://volcasampler.com
JavaScript
1
star
28

jazzerbot

🎷 User-Customized Jazz Improvisation Generation
Java
1
star
29

ant-swarm-net-load-balancing

Using "ants" to solve network traffic problems... the hard way
TeX
1
star
30

benwiley.org

http://benwiley.org
JavaScript
1
star
31

incognitobook

đŸ•ĩī¸â€â™€ī¸ Browser extension to open all external Facebook links in an Incognito window.
JavaScript
1
star
32

recursive-lowercase-json

Small function recursively converts object keys to lowercase within JSON-compatible object.
JavaScript
1
star
33

string-length-the-hard-way

What if you wanted to get the length of a string in Lua.. the hard way?
1
star
34

hello-modules

A simple working usage of static and dynamic ES2015 imports, working in Safari Technology Preview.
JavaScript
1
star
35

why-bind-cache

A presentation on using a cache for bound functions in React
JavaScript
1
star
36

2019.benwiley.org

JavaScript
1
star