Pre-requisite
- You have created a function with with name "convert-audio"
- You have downloaded and uploaded ffmpeg files (ffmpeg.exe) in "site -> wwwroot-> bin"
- You have created a folder with name wav_to_mp3 "site -> wwwroot-> wav_to_mp3"
Source Code
//Download the Required module const http = require('https'); const fs = require('fs'); const childProcess = require('child_process'); //Define constants const VIDEO_PATH = 'wav_to_mp3/'; const ffmpegLib='bin/ffmpeg'; module.exports = async function (context, req) { ////////////// params declaration let bodyData=req.body; let audioFile=bodyData.audio_file; let videoFile=bodyData.video_file; let extension=bodyData.extension; if (audioFile && videoFile && extension) { if(fs.existsSync(VIDEO_PATH+videoFile)){ await ffmpegConvertVideoToAudio(VIDEO_PATH+videoFile,VIDEO_PATH+audioFile,extension).then(data=>{ messageData(200,{success:1,'msg':'',result:{audio_file:data.filename}}); }); }else{ messageData(200,{success:0,msg:"video_file does not exist",'result':{}}); } } else { messageData(200,{success:0,msg:"Parameter missing (audio_file OR video_file OR extension)",'result':{}}); } //for print the message function messageData(statusCode,message){ context.res = { status: statusCode, body: message, headers: { 'Content-Type': 'application/json' } }; context.done(); } }; ///////////////////////////// Other Functions //////////////////////////////////// function ffmpegConvertVideoToAudio(sourcefile, destinationFile, extension) { return new Promise(function(resolve, reject) { let result={success:0,'msg':'',filename:destinationFile}; console.log('ffmpegConvertVideoToAudio called '+destinationFile); var args = [ '-i', sourcefile, '-f', extension, destinationFile ]; var proc = childProcess.spawnSync(ffmpegLib, args); result.success=1 resolve(result); }); } ///////////////////////////// Other Functions ////////////////////////////////////
URL: https://function-name.azurewebsites.net/api/convert-audio
Method: POST
Request: {"video_file":"0_20190821064650782.mp4","audio_file":"arun.mp3","extension":"mp3"} Response:
{ "success": 1, "msg": "", "result": { "audio_file": "wav_to_mp3/arun.mp3" } }
URL: https://function-name.azurewebsites.net/api/convert-audio
Method: POST
Request: {"video_file":"0_20190821064650782.mp4","audio_file":"arun.flac","extension":"flac"} Response:
{ "success": 1, "msg": "", "result": { "audio_file": "wav_to_mp3/arun.flac" } }