muaz-khan/Ffmpeg.js

code query

Opened this issue · 0 comments

this is my handler.js

var fs = require('fs'),
sys = require('sys'),
exec = require('child_process').exec;
//r ejs = require('ejs');
//r mysql = require('mysql');
//r DATABASE = 'vistasoft';

function home(response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(fs.readFileSync('./static/home.html'));
}

/*
var mysql = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'raazkumar',
});*/

function ifWin(response, files) {
// following command tries to merge wav/webm files using ffmpeg
var merger = __dirname + '\merger.bat';
var audioFile = __dirname + '\upload' + files.audio.name;
var videoFile = __dirname + '\upload' + files.video.name;
var mergedFile = __dirname + '\upload' + files.audio.name.split('.')[0] + '-merged.webm';

// if a "directory" has space in its name; below command will fail
// e.g. "c:\\dir name\\uploads" will fail.
// it must be like this: "c:\\dir-name\\uploads"
var command = merger + ', ' + audioFile + " " + videoFile + " " + mergedFile + '';
exec(command, function (error, stdout, stderr) {
    if (error) {
        console.log(error.stack);
        console.log('Error code: ' + error.code);
        console.log('Signal received: ' + error.signal);
    } else {
        response.statusCode = 200;
        response.writeHead(200, {
            'Content-Type': 'application/json'
        });
        response.end(files.audio.name.split('.')[0] + '-merged.webm');

        fs.unlink(audioFile);
        fs.unlink(videoFile);
    }
});

}

function ifMac(response, files) {
// its probably *nix, assume ffmpeg is available
var audioFile = __dirname + '/uploads/' + files.audio.name;
var videoFile = __dirname + '/uploads/' + files.video.name;
var mergedFile = __dirname + '/uploads/' + files.audio.name.split('.')[0] + '-merged.webm';

var util = require('util'),
    exec = require('child_process').exec;

var command = "ffmpeg -i " + audioFile + " -i " + videoFile + " -map 0:0 -map 1:0 " + mergedFile;

console.log("jieeeeee"+command);
exec(command, function (error, stdout, stderr) {
if (stdout)
{
console.log(stdout);
}
if (stderr){
console.log(stderr);
}

    if (error) {
        console.log('exec error: ' + error);
        response.statusCode = 404;
        response.end();

    } else {
        response.statusCode = 200;
        response.writeHead(200, {
            'Content-Type': 'application/json'
        });
        response.end(files.audio.name.split('.')[0] + '-merged.webm');

        // removing audio/video files
        fs.unlink(audioFile);
        fs.unlink(videoFile);
    }

});

}

//this function merges wav/webm files
function merge(response, files) {
// detect the current operating system
var isWin = !!process.platform.match( /^win/ );

if (isWin) {
    ifWin(response, files);
} else {
    ifMac(response, files);
}

}

function _upload(response, file) {
var fileRootName = file.name.split('.').shift(),
fileExtension = file.name.split('.').pop(),
filePathBase = './public/upload/',
fileRootNameWithBase = filePathBase + fileRootName,
filePath = fileRootNameWithBase + '.' + fileExtension,
fileID = 2,
fileBuffer;

while (fs.existsSync(filePath)) {
    filePath = fileRootNameWithBase + '(' + fileID + ').' + fileExtension;
    console.log("myfilepath="+filePath);

    fileID += 1;
}

file.contents = file.contents.split(',').pop();

fileBuffer = new Buffer(file.contents, "base64");

console.log("file path="+filePath);
fs.writeFileSync(filePath, fileBuffer);
}

// this function uploads files
function upload(response, postData) {
var files = JSON.parse(postData);

// writing audio file to disk
_upload(response, files.audio);

if (files.uploadOnlyAudio) {
    response.statusCode = 200;
    response.writeHead(200, { 'Content-Type': 'application/json' });
    console.log("files.audio="+files.audio.name);
    response.end(files.audio.name);
}

if (!files.uploadOnlyAudio) {
    // writing video file to disk
    _upload(response, files.video);

    merge(response, files);
}

}

function hasMediaType(type) {
var isHasMediaType = false;
['audio/wav', 'audio/ogg', 'video/webm', 'video/mp4'].forEach(function(t) {
if(t===type)
{
isHasMediaType = true;
}
});

return isHasMediaType;

}

function serveStatic(response, pathname) {

var extension = pathname.split('.').pop(),
    extensionTypes = {
        'js': 'application/javascript',
        'webm': 'video/webm',
        'mp4': 'video/mp4',
        'wav': 'audio/wav',
        'ogg': 'audio/ogg',
        'gif': 'image/gif'
    };

response.writeHead(200, {
    'Content-Type': extensionTypes[extension]
});
if (hasMediaType(extensionTypes[extension]))
    {
    response.end(fs.readFileSync('.' + pathname));
    }
else
    {
    response.end(fs.readFileSync('./static' + pathname));
    }
    }

exports.home = home;
exports.upload = upload;
exports.serveStatic = serveStatic;

end of handler.js

this is my merger.bat file code
@echo off
"C:\ffmpeg\bin\ffmpeg.exe" -itsoffset -00:00:00 -i %1 -itsoffset -00:00:00 -i %2 %3

for %%a in (".webm") do ffmpeg -i "%%a" -c:v libx264 -preset slow -crf 20 -c:a libvo_aacenc -b:a 128k "C:\Users\Vistasoft\Desktop\KVApp_update\public\upload%%~na.mp4"
pause

end of merger.bat

now i want to get and save mp4 file help us as soon as
email : rahul.vistasoft@gmail.com