Back PJ Onori’s code

Export frames from video

This was the first script of many I wrote for An over-analytical analysis of style. To be honest, it’s probably not a super useful script for anyone other than myself. Nonetheless, it’s a quick boilerplate for anyone who wants to quickly being exporting frames from a video. This script can take some time to grind through large videos, so just be prepared.

Instructions

  1. Copy the code below into a file on your machine. Let’s name it export-video-frames.js
  2. Install Node.js
  3. Fire up a Terminal window and cd to the directory where you saved the export-video-frames.js file
  4. This script requires node-ffmpeg. To install, cd into your project’s directory and run npm i ffmpeg --save-dev in your terminal. I try to avoid using third-party libraries, but I ended up taking a shortcut for this project.
  5. Edit VIDEO_PATH, EXPORT_PATH, EXPORT_FILENAME, FRAME_RATE, and IMAGE_DIMENSIONS to fit your needs
  6. Run node export-video-frames.js in your terminal
  7. Read out the results returned in the terminal
                
import ffmpeg from 'ffmpeg'

// The location of the video to export frames
const VIDEO_PATH = './video.mp4'
// The destination for all exported frames
const EXPORT_PATH = './video-frames'
// The prefix name of each image file exported
const EXPORT_FILENAME = 'video-frame'
// The number of frames to export each second. Setting the value to 24 would export 24 images for each second of video
const FRAME_RATE = 1
// The width and height of the image being exported
const IMAGE_DIMENSIONS = '960x400'

try {
	var process = new ffmpeg(VIDEO_PATH);
	process.then(function (video) {
		video.fnExtractFrameToJPG(EXPORT_PATH, {
			frame_rate : FRAME_RATE,
			file_name : EXPORT_FILENAME,
			size: IMAGE_DIMENSIONS,
			keep_aspect_ratio: true
		}, function (error, files) {
			if (!error) {
				console.log('Frames: ' + files);
			}	
		});
	}, function (err) {
		console.log('Error: ' + err);
	});
} catch (e) {
	console.log(e.code);
	console.log(e.msg);
}