xonecas.io
311
Fn

ffmpeg-node / commit 147c753

moved past proof of concept, now using safer spawn method, cleaner file, with descriptive comments.

xonecas2011-02-02 147c7535e0babc26563986e184d1fecd15a8a346 parent d90ea80

3 files changed

README+3−25
@@ -1,32 +1,10 @@
11 Node.js module to control ffmpeg
22
3- Documentation and webm convenience method comming asap.
4-
53 usage:
64
7- var ffmpeg = require('/path/to/ffmpeg-node.js');
8-
9- ffmpeg.command(
10- input file, // string representing a path
11- audio options, // json-like object of ffmpeg audio options [strings]
12- video options, // json-like object of ffmpeg video options [strings]
13- video format, // json-like object of width and height values [strings]
14- output file, // string with the target /path/file.ext
15- callback // function to call after video conversion
16- );
17-
18-convenience methods:
19-
20- ffmpeg.toMp4(
21- input file, // string representing a path
22- video format, // json-like object of width and height values [strings]
23- callback // function to call after video conversion
24- );
25-
26- ffmpeg.toOgg(
27- input file, // string representing a path
28- video format, // json-like object of width and height values [strings]
29- callback // function to call after video conversion
5+ ffmpeg.call(
6+ [ ... ], // array of ffmpeg flags
7+ callback // function to call after ffmpeg is done
308 );
319
3210 Examples:
ffmpeg-node.js+170−80
@@ -1,94 +1,184 @@
1-// doc missing, dog ate it
1+
2+/**
3+ * Module to drive ffmpeg video enconding library with shortcuts
4+ * for web video. Requires a ffmpeg compiled with support for mp4/ogg/webm.
5+ */
26
37 var path = require('path'),
4- exec = require('child_process').exec;
5-
6-exports.command = function (input, audio, video, format, output, callback) {
7- path.exists(input, function (exists) {
8- if (exists) {
9- var command = 'ffmpeg -i '+ input +' ';
10-
11- if (typeof audio === 'object') {
12- for (flag in audio) {
13- command += '-'+ flag +' '+ audio[flag] +' ';
14- }
15- }
16-
17- if (typeof video === 'object') {
18- for (flag in video) {
19- var value = video[flag];
20-
21- if (flag === 'vpre' && typeof value === 'object') {
22- value.forEach(function (preset) {
23- command += '-vpre '+ preset +' ';
24- });
25- }
26- else if (typeof value === 'string') {
27- command += '-'+ flag +' '+ value +' ';
28- }
29- else
30- throw new TypeError('Presets must be a string or an array of strings.');
31- }
32- }
33-
34- if (typeof format === 'object' &&
35- typeof format.width === 'string' &&
36- typeof format.height === 'string') {
37-
38- command += '-s '+ format.width +'x'+ format.height +' ';
39- }
40-
41- if (typeof output === 'string') {
42- command += output;
43- }
44- else
45- throw new SyntaxError('Output must be a fileName.containerFormat: '+ output);
46-
47- exec(command , function (error, stdout, stderr) {
48- if (error)
49- throw new Error('ffmpeg didn\'t finish successfully: '+ stderr);
50- else
51- callback();
52- });
53- }
54- else
55- throw new ReferenceError('File not found: '+ input);
56- });
8+ spawn = require('child_process').spawn;
9+
10+/**
11+ * Description:
12+ * calls ffmpeg with the specified flags and returns the output
13+ * to the callback function.
14+ *
15+ * Parameters:
16+ * params - an array of ffmpeg options, ex: ['-i','./test.3gp']
17+ * callback - a function to call when ffmpeg is done, ex:
18+ * function (stderr, stdout, exitCode) { ... }
19+ */
20+
21+exports.call = function (params, callback) {
22+
23+ if (params instanceof Array && params.length > 2) {
24+
25+ var stderr = '', stdout = '',
26+ ffmpeg = spawn('ffmpeg', params);
27+
28+ ffmpeg.stderr.on('data', function (err) {
29+ stderr += err;
30+ });
31+
32+ ffmpeg.stdout.on('data', function (output) {
33+ stdout += output;
34+ });
35+
36+ ffmpeg.on('exit', function (code) {
37+ callback(stderr, stdout, code);
38+ });
39+
40+ }
41+
5742 }
5843
59-exports.toMp4 = function (input, format, callback) {
60-
61- var output = path.dirname(input) +'/'+
62- path.basename(input, path.extname(input)) +'.mp4';
63-
64- this.command(input, {
65- 'acodec': 'libfaac',
66- 'ab': '128k',
67- 'ar': '41000'
68- }, {
69- 'vcodec': 'libx264',
70- 'vpre': ['slow', 'baseline'],
71- 'r': '25'
72- }, format, output, callback);
44+
45+/**
46+ * Description:
47+ * serves as middle man for convenience method, required to
48+ * avoid code repetition.
49+ *
50+ * Parameters:
51+ * type - one of 'mp4', 'ogg', 'webm' as a string.
52+ * file - path/to/the/inputFile.ext as a string.
53+ * params - an array of ffmpeg options to be added to the predefined ones (optional).
54+ * output - path/to/the/outputFile.ext as a string (optional).
55+ * callback - function to call when ffmpeg is done, ex:
56+ * function (stderr, stdout, exitCode) { ... }
57+ */
58+
59+exports.convert = function (/* overloaded */) {
60+
61+ var type = arguments[0], file = arguments[1],
62+ params = [], output = '', callback = false;
63+
64+ if (arguments.length === 3) {
65+ params = [],
66+ output = path.basename(
67+ file, path.extname(file)) +'.'+ type,
68+ callback = arguments[2];
69+ }
70+ else if (arguments.length > 3) {
71+ var err = false;
72+
73+ if (arguments[2] instanceof Array)
74+ params = arguments[2];
75+ else if (typeof arguments[2] === 'string')
76+ output = arguments[2];
77+ else if (arguments[2] instanceof Function)
78+ callback = arguments[2];
79+ else
80+ err = true;
81+
82+ if (typeof arguments[3] === 'string')
83+ output = arguments[3];
84+ else if (arguments[3] instanceof Function)
85+ callback = arguments[3];
86+ else
87+ err = true;
88+
89+ if (arguments[4] instanceof Function)
90+ var callback = arguments[4];
91+ else
92+ err = true;
93+
94+ if (err)
95+ throw new Error('Could not parse arguments');
96+ }
97+ else
98+ throw new Error('Not enough arguments');
99+
100+ switch(type) {
101+ case 'mp4':
102+ params = [
103+ '-i', file,
104+ '-acodec', 'libfaac',
105+ '-ab', '128k',
106+ '-ar', '41000',
107+ '-vcodec', 'libx264',
108+ '-vpre', 'slow',
109+ '-vpre', 'baseline',
110+ '-r', '25',
111+ '-y', output
112+ ].concat(params);
113+ break;
114+
115+ case 'ogg':
116+ params = [
117+ '-i', file,
118+ '-acodec', 'libvorbis',
119+ '-ab', '128k',
120+ '-ar', '41000',
121+ '-vodec', 'libtheora',
122+ '-r', '25',
123+ '-y', output
124+ ].concat(params);
125+ break;
126+
127+ case 'webm':
128+ params = [
129+ '-i', file,
130+ '-acodec', 'libvorbis',
131+ '-ab', '128k',
132+ '-ar', '41000',
133+ '-vcodec', 'libvpx',
134+ '-b', '614400',
135+ '-aspect', '16:9',
136+ '-y', output
137+ ].concat(params);
138+ break;
139+ }
140+
141+ this.call(params, callback);
142+
73143 }
74144
75-exports.toOgg = function(input, format, callback) {
76- var output = path.dirname(input) +'/'+
77- path.basename(input, path.extname(input)) +'.ogg';
78-
79- this.command(input, {
80- 'acodec': 'libvorbis',
81- 'ab': '128k',
82- 'ar': '41000'
83- }, {
84- 'vcodec': 'libtheora',
85- 'r': '25'
86- }, format, output, callback);
145+/**
146+ * Description:
147+ * Convenience methods to convert to popular web formats (flash/html5)
148+ * If you know how to improve these default options, let me know.
149+ *
150+ * Parameters:
151+ * file - path/to/the/inputFile.ext as a string.
152+ * params - an array of ffmpeg options to be added to the predefined ones (optional).
153+ * output - path/to/the/outputFile.ext as a string (optional).
154+ * callback - function to call when ffmpeg is done, ex:
155+ * function (stderr, stdout, exitCode) { ... }
156+ */
157+
158+exports.mp4 = function (/* overloaded */) {
159+
160+ var unshift = Array.prototype.unshift;
161+ unshift.call(arguments, 'mp4');
162+
163+ this.convert.apply(this, arguments);
164+
87165 }
88166
167+exports.ogg = function (/* overloaded */) {
168+
169+ var unshift = Array.prototype.unshift;
170+ unshift.call(arguments, 'ogg');
89171
172+ this.convert.apply(this, arguments);
90173
174+}
91175
176+exports.webm = function (/* overloaded */) {
92177
178+ var unshift = Array.prototype.unshift;
179+ unshift.call(arguments, 'webm');
93180
181+ this.convert.apply(this, arguments);
182+
183+}
94184
test.js+8−33
@@ -1,36 +1,11 @@
11 var ffmpeg = require('./ffmpeg-node.js');
22
3-ffmpeg.command('./test.3gp', {
4- 'acodec': 'libfaac',
5- 'ab': '128k',
6- 'ar': '41000'
7- }, {
8- 'vcodec': 'libx264',
9- 'vpre': ['slow', 'baseline'],
10- 'r': '25'
11- }, {
12- 'width': '640',
13- 'height': '360'
14- }, './test.mp4', function () {
3+ffmpeg.mp4(
4+ './test.3gp',
5+ ['-s', '720x480'],
6+ './test.mp4',
7+ function (err, out, code) {
8+ console.log(err, out, code);
9+ }
10+);
1511
16- console.log('it worked');
17-
18- });
19-
20-ffmpeg.toMp4('./test.3gp', {
21- 'width': '640',
22- 'height': '360'
23- }, function () {
24-
25- console.log('it worked');
26-
27- });
28-
29-ffmpeg.toOgg('./test.3gp', {
30- 'width': '640',
31- 'height': '360'
32- }, function () {
33-
34- console.log('it worked');
35-
36- });