Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts

Monday 9 September 2019

How Azure functions can convert mp4 to mp3 with node using ffmpeg?


Pre-requisite
  1. You have created a function with with name "convert-audio"
  2. You have downloaded and uploaded ffmpeg files (ffmpeg.exe) in "site -> wwwroot-> bin"
  3. 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"
    }
}

Friday 6 September 2019

Download the media files with node in Azure serverless

Download the media files with node in Azure serverless

Pre-requisite for Node in Azure Serverless 

  1. You have created a function with with name "download-files"
  2. You have downloaded ffmpeg files (ffmpeg.exe) in "site -> wwwroot-> bin"
  3. 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');
const path = require('path');

//Define constants
const VIDEO_PATH = 'wav_to_mp3/';
const ffmpegLib='bin/ffmpeg';

module.exports =  function (context, req) {
    ////////////// params declaration
    let bodyData=req.body;
    let tourStreamName=bodyData.tour_stream_name;
    let videoUrls=bodyData.url;
    
    let filename='';
    let filenameSuccessLog={};
    let completedCounter=0;
    if (tourStreamName && videoUrls.length>0) {
        ////////////////////// ALL VIDEOS DOWNLOAD ////////////////////////////
        //const forLoop = async _ => {
         videoUrls.forEach(async (url) => {
              filename=path.basename(url); 
             // let videoDownload=VIDEO_PATH+filename;
                //full video url, filename, force download
                downloadFileAsync(url,filename,0).then(data=>{  
                   filenameSuccessLog[data.filename]=data.success;
                   completedCounter++;
                   if(videoUrls.length==completedCounter){
                       messageData(200,{success:1,'msg':'',result:filenameSuccessLog});
                   }                    
               }); 
          })
        //}    
         //forLoop();
        ////////////////////// ALL VIDEOS DOWNLOAD ////////////////////////////

    } else {
        messageData(200,{success:0,msg:"Parameter missing (tour_stream_name OR url)",'result':{}});
    }
    
    
    
    //for print the message
    function messageData(statusCode,message){
        context.res = {
            status: statusCode,
            body: message,
            headers: {
                'Content-Type': 'application/json'
            }
        };   
         context.done();
               
    } 
    
};



///////////////////////////// Other Functions ////////////////////////////////////
/**
 * @url: url of video
 * @destination: file with name download in wav_to_mp3
 * @forcedownload: 1 means download again even
 */
async function downloadFileAsync(url, destination,forcedownload) {
    return new Promise(function(resolve, reject) {
        let result={success:0,'msg':'',filename:destination};
        if (fs.existsSync(VIDEO_PATH+destination) && forcedownload==0) {
            result.success=1
            resolve(result);            
        }else{
            const file = fs.createWriteStream(VIDEO_PATH+destination);
            const request = http.get(url, (res) => {
              res.pipe(file);
            });
            request.once('error', (error) => 
              {
                  result.msg=error;
                  reject(result);
              }
            );
            file.once('finish', () => 
              {
                result.success=1
                resolve(result);
              }
            );             
        }
       
        
    })
}

///////////////////////////// Other Functions ////////////////////////////////////

URL: https://function-name.azurewebsites.net/api/download-files
Method: POST
Request:
{"tour_stream_name":"arun","url":["https://example.com/video/0_20190821064650782.mp4","https://example.com/video/tq_33038dev-GM-ma9kW-1106-26079.mp4"]}
Response:
{
    "success": 1,
    "msg": "",
    "result": {
        "tq_33038dev-GM-ma9kW-1106-26079.mp4": 1,
        "0_20190821064650782.mp4": 1
    }
}

Install FFMPEG in Azure Serverless Architecture

Install FFMPEG in Azure Serverless Architecture

Create "Function" in "Function App" in Azure (If you have not created)

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function


Install FFMPEG in Azure Server

  1. Now Go to Function App, and Click on the your "Function App"
  2. Click on "Platform Features" then click on "Advance tools (Kudu)" like blow
  3. In Kudu, select CMD from the Debug console menu.
  4. Now, you can navigate to the folder by click on site -> wwwroot.

  5. Create a folder with bin,here we will put the ffmpeg library.
  6. a. Go to the https://ffmpeg.zeranoe.com/builds/ (for FFMPEG)
    b. Download the build as per Architecture.
    c. Unzip the downloaded content.
    d. Now copy the ffmpeg.exe, ffplay.exe and ffprobe.exe (from bin folder in downloaded).
    e. Copy these three files in site -> wwwroot-> bin folder.
  7. Create a folder with wav_to_mp3,here we will store the media like photo, videos etc.
  8. Here you can run the ffmpeg command such ffmpeg -version



Thursday 12 October 2017

Azure video indexer - Get text from the Video


Question: What is Video Indexer API?
This API is used to Get the Visual Text from the Video. It will give you all the text that appear in the video.



Question: How to Get Text from the video?
  1. Get the API key
  2. Get the VIdeo URL from Azure OR Amazon S3 Or any public video URL.
  3. Pass the Video URL with API key and get the video Id token.
  4. Pass the video Id token with API key and get the all Text



Question: How to Get the API Key?
Follow the below link, you will get the API key which can be used further.
https://docs.microsoft.com/en-us/azure/cognitive-services/video-indexer/video-indexer-use-apis#subscribe-to-the-api


Question: How to Get the videoId token from the video?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?name=new%20video%20test&privacy=Private&videoUrl=".urlencode($videoUrl),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "ocp-apim-subscription-key: {$subScriptionKey}",    
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';
$jsonData = '';        
$config = array(
       'adapter' => 'Zend_Http_Client_Adapter_Curl',
       'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
   );
    $url='https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns';
   $client = new Zend_Http_Client($url, $config);
   //Set the Header value
    $client->setHeaders('Content-Type', "multipart/form-data");
    $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

    $postData = array(
    'name'=>'GV-NNokn-989-5175',
    'privacy'=>'Private',
    'videoUrl'=>$videoUrl
    );
    $client->setParameterGet($postData);

   $response = $client->request('POST'); 
   $jsonData= ($response->getBody());



Output
9a10d9d0002



Question: How to get video text from videoId token?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/VIDEOIDTOKEN",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "ocp-apim-subscription-key: {$subScriptionKey}",
    "postman-token: 43c1c192-1de6-ac3d-42e0-b1ddc7cc0ee0"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
        $language = 'English';
        $body = 'How to you?';
        $breadDownsId='2bc89c5ec1';

        //Main Data
        $client = new Zend_Http_Client('https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/' . $breadDownsId, array(
            'maxredirects' => 0,
            'timeout' => 30
        ));       
        
        $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

        $parameters = array(
            'language' => $language,
        );
        $client->setParameterGet($parameters);

        try {
            $response = $client->request('GET');
            $data = $response->getBody();
        } catch (Exception $ex) {
            $data = $ex;
        }
        echo $response->getBody();die;

Output
{
    "accountId": "2b525a6-3a5408006294",
    "id": "9a10d9d912",
    "partition": null,
    "name": "new video test",
    "description": null,
    "userName": "Ram Kumar",
    "createTime": "2017-10-12T04:41:20.8986398+00:00",
    "organization": "",
    "privacyMode": "Private",
    "state": "Processed",
    "isOwned": true,
    "isEditable": false,
    "isBase": true,
    "durationInSeconds": 1373,
    "summarizedInsights":{/* It will have data*/},
    "breakdowns":{/* It will have data*/},
    "social":{/* It will have data*/}
}




Friday 14 July 2017

Download the Azure SDK for PHP and upload the video in Azure

Download the Azure SDK for PHP and upload the video in Azure

Download the Azure SDK for PHP

  1. Install Git. (If already, move to next)
  2. Create composer.json and add following contents.
    {
             "require": {
               "microsoft/azure-storage": "*"
             }
           }
  3. Download composer.phar in your project root.
  4. Execute following Command.
    php composer.phar install
  5. It will download the Library, which I have tested.



Upload the Media in Azure

  1. Include following files:
    use WindowsAzure\Common\ServicesBuilder;
    use MicrosoftAzure\Storage\Common\ServiceException;
    require_once 'azure/vendor/autoload.php';
  2. Get the Credentials
    $azureUserName='XXXXXXXXXXXX';
    $azureKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    $containter='XXXXXXXXX';
    
  3. Set the Credentias
    $connectionString = "DefaultEndpointsProtocol=http;AccountName={$azureUserName};AccountKey={$azureKey}";
    $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
  4. Upload the File in Azure
    $blobName='textFile.txt';
    $content = fopen('../data/test6.txt', "r");
    try    {
                
                $blobRestProxy->createBlockBlob($containter, $blobName, $content);
                echo 'Uploaded successfully';
            }
            catch(ServiceException $e){
                $code = $e->getCode();
              $e->getMessage();
    
            }