xonecas.io
311
Fn

ffmpeg-node / commit 9ac246f

initial implementation

xonecas2011-01-29 9ac246fcfacfcc474603364c8ec0dd2ca811619b

3 files changed

README+49−0
@@ -0,0 +1,49 @@
1+Node.js module to control ffmpeg
2+
3+ Documentation and webm convenience method comming asap.
4+
5+usage:
6+
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
30+ );
31+
32+
33+These are the ffmpeg commands used by the module for the convenience methods.
34+(If you know how to improve them for better quality/speed, please let me know).
35+
36+ mp4:
37+
38+ ffmpeg -i ./test.3gp -acodec libfaac -ab 128k -ar 41000 -vcodec libx264 -vpre slow -vpre baseline -s 640x360 -r 25 ./test.mp4
39+
40+
41+ ogg:
42+
43+ ffmpeg -i ./test.3gp -acodec libvorbis -ab 128k -ar 41000 -vcodec libtheora -s 640x360 -r 30 ./test.ogg
44+
45+ webm:
46+
47+ ffmpeg -i test.3gp -acodec libvorbis -ab 128k -ar 41000 -vcodec libvpx -s 640x360 -b 614400 -aspect 16:9 video.webm
48+
49+
ffmpeg-node.js+94−0
@@ -0,0 +1,94 @@
1+// doc missing, dog ate it
2+
3+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+ });
57+}
58+
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);
73+}
74+
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);
87+}
88+
89+
90+
91+
92+
93+
94+
test.js+41−0
@@ -0,0 +1,41 @@
1+var ffmpeg = require('./ffmpeg-node.js');
2+
3+/*
4+ffmpeg.command('./test.3gp', {
5+ 'acodec': 'libfaac',
6+ 'ab': '128k',
7+ 'ar': '41000'
8+ }, {
9+ 'vcodec': 'libx264',
10+ 'vpre': ['slow', 'baseline'],
11+ 'r': '25'
12+ }, {
13+ 'width': '640',
14+ 'height': '360'
15+ }, './test.mp4', function () {
16+
17+ console.log('it worked');
18+
19+ });
20+*/
21+
22+/*
23+ffmpeg.toMp4('./test.3gp', {
24+ 'width': '640',
25+ 'height': '360'
26+ }, function () {
27+
28+ console.log('it worked');
29+
30+ });
31+*/
32+
33+
34+ffmpeg.toOgg('./test.3gp', {
35+ 'width': '640',
36+ 'height': '360'
37+ }, function () {
38+
39+ console.log('it worked');
40+
41+ });