kidapu
10/29/2015 - 6:12 AM

動画からGIFアニ作る用Node.js

動画からGIFアニ作る用Node.js

// fffmpegが必要
// さんこう:http://takuya-1st.hatenablog.jp/entry/2015/07/15/002701

// require
var _exec = require('child_process').execSync;

// static const
var _moviePath = "./src/test.mp4";
var _outPath ="./dest/out.gif"
var _rate = 0.5;
var _fps = 15;


// convert
convert();
function convert()
{
	// width, height
	var width = Math.floor(""+ _exec('ffmpeg -i ' + _moviePath + ' 2>&1 > /dev/null | grep Stream  | grep -o "[1-9][0-9]\\+x[1-9][0-9]\\+" | cut -d x -f1'));
	var height = Math.floor(""+ _exec('ffmpeg -i ' + _moviePath + ' 2>&1 > /dev/null | grep Stream  | grep -o "[1-9][0-9]\\+x[1-9][0-9]\\+" | cut -d x -f2'));

	// calc new width, height
	var newWidth = width * _rate;
	var newHeight = height * _rate;
	console.log( "target w:" newWidth + " / h:" + newHeight);
	
	// exec
	var log = "" + _exec("ffmpeg -y -i " + _moviePath + " -vf scale=" + newWidth + ":" + newHeight + " -an -r " + _fps + "  -pix_fmt rgb24 -f gif " + _outPath );
	console.log( log );
}