Created
June 19, 2013 14:58
-
-
Save michaelBenin/5814981 to your computer and use it in GitHub Desktop.
Grunt task to convert Video to HTML5 Formats.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
convertVideo_config: { | |
ffmpeg: 'bin/ffmpeg/./ffmpeg', | |
mp4: '-vcodec libx264', | |
m4v: '-vcodec libx264', | |
'3gp': '-acodec libvo_aacenc -vcodec libx264', | |
webm: '-acodec libvorbis -vcodec libvpx', | |
ogv: '-acodec libvorbis' | |
}, | |
*/ | |
module.exports = function (grunt) { | |
"use strict"; | |
grunt.registerMultiTask('convertVideo', 'Video to html5 formats', function () { | |
var ffmpeg = grunt.config('convertVideo_config.ffmpeg') + ' -i ', | |
done = this.async(), | |
c = [], | |
p = [], | |
Op = { | |
mp4: function (i, o) { | |
return ffmpeg + i + ' ' + grunt.config('convertVideo_config.mp4') + ' ' + o; | |
}, | |
m4v: function (i, o) { | |
return ffmpeg + i + ' ' + grunt.config('convertVideo_config.m4v') + ' ' + o; | |
}, | |
'3gp': function (i, o) { | |
return ffmpeg + i + ' ' + grunt.config('convertVideo_config.3gp') + ' ' + o; | |
}, | |
webm: function (i, o) { | |
return ffmpeg + i + ' ' + grunt.config('convertVideo_config.webm') + ' ' + o; | |
}, | |
ogv: function (i, o) { | |
return ffmpeg + i + ' ' + grunt.config('convertVideo_config.ogv') + ' ' + o; | |
}, | |
mpg: function () {}, | |
mov: function () {}, | |
avi: function () {}, | |
flv: function () {}, | |
f4v: function () {} | |
}, | |
allfiles = grunt.file.expandFiles(this.file.src); | |
grunt.file.expandFiles(this.file.src).forEach(function (file) { | |
var r = [ | |
[/\.mp4/, 'mp4'], | |
[/\.m4v/, 'm4v'], | |
[/\.3gp/, '3gp'], | |
[/\.webm/, 'webm'], | |
[/\.ogv/, 'ogv'], | |
[/\.mpg/, 'mpg'], | |
[/\.mov/, 'mov'], | |
[/\.avi/, 'avi'], | |
[/\.flv/, 'flv'], | |
[/\.f4v/, 'f4v'] | |
], | |
t = []; | |
for (var i = 0; i < r.length; i++) { | |
if (r[i][0].test(file)) { | |
t.push(r[i]); | |
r.splice(i, 1); | |
} | |
} | |
for (var k = 0; k < t.length; t++) { | |
for (var j = 0; j < r.length; j++) { | |
var regEx = new RegExp(t[k][0]); | |
if (allfiles.indexOf(file.replace(regEx, '.' + r[j][1])) === -1 && p.indexOf(file.replace(regEx, '.' + r[j][1])) === -1) { | |
p.push(file.replace(regEx, '.' + r[j][1])); | |
if (Op[r[j][1]](file, file.replace(regEx, '.' + r[j][1])) !== undefined) { | |
c.push(Op[r[j][1]](file, file.replace(regEx, '.' + r[j][1]))); | |
} | |
} | |
} | |
} | |
}); | |
require('child_process').exec(c.join(' && ')).on('exit', function () { | |
done(true); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment