Skip to content

Instantly share code, notes, and snippets.

@zulfajuniadi
Last active October 27, 2016 13:54
Show Gist options
  • Save zulfajuniadi/bf1122a03435d5d269c8 to your computer and use it in GitHub Desktop.
Save zulfajuniadi/bf1122a03435d5d269c8 to your computer and use it in GitHub Desktop.
var amqpConnectionOptions = {
host: process.env['RABBITMQ_PORT_5672_TCP_ADDR'],
port: 5672,
login: 'guest',
password: 'guest'
};
var amqpQueueOptions = {
durable: true,
autoDelete: false
};
var amqpExchangeName = 'video';
var amqpQueueName = 'video-resample';
var serviceName = 'Video Resample';
var async = require('async');
var amqp = require('amqp');
var fs = require('fs');
var os = require('os');
var exec = require('child_process').exec;
var scales = [240];
async.waterfall([
/**
* Creates AMQP Connection
*/
function(callback) {
var connection = amqp.createConnection(amqpConnectionOptions, {
reconnect: true
});
connection.on('ready', function(){
callback(null, {
connection: connection
});
});
},
/**
* Creates Exchanges
*/
function(context, callback) {
context.connection.exchange(amqpExchangeName, {
durable: true,
type: 'fanout',
autoDelete: false
}, function (queue) {
callback(null, context);
});
},
/**
* Creates Queues
*/
function(context, callback) {
context.connection.queue(amqpQueueName, amqpQueueOptions, function (queue) {
queue.bind(amqpExchangeName, 'all', function(){
context.queue = queue;
callback(null, context);
});
});
}
], function(err, context){
console.info(serviceName + ' Ready');
var queue = context.queue;
queue.subscribe(function(payload){
var path = JSON.parse((new Buffer(payload.data)).toString());
console.info(path + ' queued');
async.eachSeries(scales, function(scale, cb) {
var outfile = path + '.' + scale + '.mp4';
var command = 'avconv -i ' + path + ' -strict experimental -acodec aac -b:a 128k -vcodec h264 -flags +aic+mv4 -y -filter:v scale=-1:' + scale + ' ' + outfile;
console.log(command);
var child = exec(command, [], {
env: process.env,
silent: false
});
child.on('error', function(){
cb(arguments);
});
child.stdout.on('data', function(data) {
console.log(data.trim());
});
child.on('exit', function(){
console.log('video transcode ' + scale + 'p done');
cb(null, null);
});
}, function(){
console.info(path + ' processed');
});
});
process.on('exit', function() {
context.connection.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment