Permalink
Browse files

initial implementation

  • Loading branch information...
0 parents commit 9ac246fcfacfcc474603364c8ec0dd2ca811619b @xonecas committed Jan 29, 2011
Showing with 184 additions and 0 deletions.
  1. +49 −0 README
  2. +94 −0 ffmpeg-node.js
  3. +41 −0 test.js
@@ -0,0 +1,49 @@
+Node.js module to control ffmpeg
+
+ Documentation and webm convenience method comming asap.
+
+usage:
+
+ var ffmpeg = require('/path/to/ffmpeg-node.js');
+
+ ffmpeg.command(
+ input file, // string representing a path
+ audio options, // json-like object of ffmpeg audio options [strings]
+ video options, // json-like object of ffmpeg video options [strings]
+ video format, // json-like object of width and height values [strings]
+ output file, // string with the target /path/file.ext
+ callback // function to call after video conversion
+ );
+
+convenience methods:
+
+ ffmpeg.toMp4(
+ input file, // string representing a path
+ video format, // json-like object of width and height values [strings]
+ callback // function to call after video conversion
+ );
+
+ ffmpeg.toOgg(
+ input file, // string representing a path
+ video format, // json-like object of width and height values [strings]
+ callback // function to call after video conversion
+ );
+
+
+These are the ffmpeg commands used by the module for the convenience methods.
+(If you know how to improve them for better quality/speed, please let me know).
+
+ mp4:
+
+ ffmpeg -i ./test.3gp -acodec libfaac -ab 128k -ar 41000 -vcodec libx264 -vpre slow -vpre baseline -s 640x360 -r 25 ./test.mp4
+
+
+ ogg:
+
+ ffmpeg -i ./test.3gp -acodec libvorbis -ab 128k -ar 41000 -vcodec libtheora -s 640x360 -r 30 ./test.ogg
+
+ webm:
+
+ ffmpeg -i test.3gp -acodec libvorbis -ab 128k -ar 41000 -vcodec libvpx -s 640x360 -b 614400 -aspect 16:9 video.webm
+
+
@@ -0,0 +1,94 @@
+// doc missing, dog ate it
+
+var path = require('path'),
+ exec = require('child_process').exec;
+
+exports.command = function (input, audio, video, format, output, callback) {
+ path.exists(input, function (exists) {
+ if (exists) {
+ var command = 'ffmpeg -i '+ input +' ';
+
+ if (typeof audio === 'object') {
+ for (flag in audio) {
+ command += '-'+ flag +' '+ audio[flag] +' ';
+ }
+ }
+
+ if (typeof video === 'object') {
+ for (flag in video) {
+ var value = video[flag];
+
+ if (flag === 'vpre' && typeof value === 'object') {
+ value.forEach(function (preset) {
+ command += '-vpre '+ preset +' ';
+ });
+ }
+ else if (typeof value === 'string') {
+ command += '-'+ flag +' '+ value +' ';
+ }
+ else
+ throw new TypeError('Presets must be a string or an array of strings.');
+ }
+ }
+
+ if (typeof format === 'object' &&
+ typeof format.width === 'string' &&
+ typeof format.height === 'string') {
+
+ command += '-s '+ format.width +'x'+ format.height +' ';
+ }
+
+ if (typeof output === 'string') {
+ command += output;
+ }
+ else
+ throw new SyntaxError('Output must be a fileName.containerFormat: '+ output);
+
+ exec(command , function (error, stdout, stderr) {
+ if (error)
+ throw new Error('ffmpeg didn\'t finish successfully: '+ stderr);
+ else
+ callback();
+ });
+ }
+ else
+ throw new ReferenceError('File not found: '+ input);
+ });
+}
+
+exports.toMp4 = function (input, format, callback) {
+
+ var output = path.dirname(input) +'/'+
+ path.basename(input, path.extname(input)) +'.mp4';
+
+ this.command(input, {
+ 'acodec': 'libfaac',
+ 'ab': '128k',
+ 'ar': '41000'
+ }, {
+ 'vcodec': 'libx264',
+ 'vpre': ['slow', 'baseline'],
+ 'r': '25'
+ }, format, output, callback);
+}
+
+exports.toOgg = function(input, format, callback) {
+ var output = path.dirname(input) +'/'+
+ path.basename(input, path.extname(input)) +'.ogg';
+
+ this.command(input, {
+ 'acodec': 'libvorbis',
+ 'ab': '128k',
+ 'ar': '41000'
+ }, {
+ 'vcodec': 'libtheora',
+ 'r': '25'
+ }, format, output, callback);
+}
+
+
+
+
+
+
+
@@ -0,0 +1,41 @@
+var ffmpeg = require('./ffmpeg-node.js');
+
+/*
+ffmpeg.command('./test.3gp', {
+ 'acodec': 'libfaac',
+ 'ab': '128k',
+ 'ar': '41000'
+ }, {
+ 'vcodec': 'libx264',
+ 'vpre': ['slow', 'baseline'],
+ 'r': '25'
+ }, {
+ 'width': '640',
+ 'height': '360'
+ }, './test.mp4', function () {
+
+ console.log('it worked');
+
+ });
+*/
+
+/*
+ffmpeg.toMp4('./test.3gp', {
+ 'width': '640',
+ 'height': '360'
+ }, function () {
+
+ console.log('it worked');
+
+ });
+*/
+
+
+ffmpeg.toOgg('./test.3gp', {
+ 'width': '640',
+ 'height': '360'
+ }, function () {
+
+ console.log('it worked');
+
+ });

0 comments on commit 9ac246f

Please sign in to comment.