Wednesday 24 October 2018

How to add formated text on video using php-ffmpeg?

How to add formated text on video using php-ffmpeg?

Question: What is drawtext on video?
Adding the single line text/multiple line text over the video.



Question: Can we change the font-size of text?
Yes, we can change the color, size, font family etc of text in videos.



Question: How to add text on videos?
ffmpeg -i myvideo.mp4 -vf drawtext="fontfile=OpenSans-Bold.ttf: text=this is text on videos: fontcolor=white: fontsize=120: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2" -codec:a copy output_video.mp4

Note: OpenSans-Bold.ttf file must available (with parallel to video i.e myvideo.mp4)



Question: How to multiple line text on videos?
ffmpeg -i myvideo.mp4 -vf "[in]drawtext=fontfile=OpenSans-Bold.ttf: text=Arun kumar: fontcolor=white: fontsize=20: box=1: boxcolor=black@0.5: boxborderw=5: x=100: y=100 drawtext=fontfile=OpenSans-Bold.ttf: text='Kuldeep ': fontcolor=white: fontsize=20: box=1: boxcolor=black@0.5: boxborderw=5: x=100: y=400[output]" -codec:a copy output_video.mp4

Note: OpenSans-Bold.ttf file must available (with parallel to video i.e myvideo.mp4)




Question: How to add text in video with PHP?
  1. You must have installed PHP-FFMPEG
    php composer.phar require php-ffmpeg/php-ffmpeg
    If not, you can check https://www.web-technology-experts-notes.in/2017/10/how-to-install-ffmpeg-in-wamp-server-in-windows7.html
  2. To achieve this, you need to use the ffmpeg filters.
  3. require_once 'ffmpeglib/vendor/autoload.php';
    $ffmpeg = FFMpeg\FFMpeg::create();
    $format = new FFMpeg\Format\Video\X264();
     $format->setAudioCodec("aac");  
    $videoFile='myvideo.mp4';
    $captionStaticFilePath='';//directory path
    $captionStaticFilePath=$_SERVER['DOCUMENT_ROOT'].'/video/';
    
    /////////////// Debug ////////////////////////////
    /*$ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener());
    $ffmpeg->getFFMpegDriver()->on('debug', function ($message) {
        echo $message;
    });*/
    /////////////// Debug ////////////////////////////                
    
    /* Filter the text correctly*/ 
    $captionJSON=json_decode('["text=This is caption text: fontfile=OpenSans-Regular.ttf: x=384: y=10: fontcolor=#ffffff: fontsize=24px","text=This is clip 2 - Samik das: fontfile=OpenSans-Bold.ttf: x=404.08331298828125: y=34.91668701171875: fontcolor=#ffffff: fontsize=24px: fonttype=bold"]');
    $captionArray=array();
    if(!empty($captionJSON) && !empty($captionJSON)){
        foreach($captionJSON as $caption){
            //String to array
            $caption=str_replace(':', '&', $caption);
            parse_str($caption, $output);
            //pr($output);die;
    
            unset($output['fonttype']);
            $output['fontsize']=str_replace('px','',$output['fontsize']);
            $output['fontfile']=$captionStaticFilePath.$output['fontfile'];
    
            $output['x'] = (int)$output['x'];
            $output['y'] = (int)$output['y'];
    
            //Array to string
            $outputStr='';
            $counter=0;
            foreach($output as $i=>$v){
                if($counter){
                    $outputStr.= ': ';
                }
                $outputStr.= $i.'='.$v;
                $counter++;
            }
            //Update to array                            
            $captionArray[]=$outputStr;
        }
    }
    /* Filter the text correctly*/ 
    
    
    try{
        $video = $ffmpeg->open($captionStaticFilePath.$videoFile);
        $command='';
        if(!empty($captionArray)){
            $command='[in]';
            foreach($captionArray as $index=>$captionComand){
                if($index>0){
                    $command= $command.',';
                }
                $command= $command.'drawtext='.$captionComand;
            }
            $command.='[out]';
    
    
        }
    
        //nOW execute the command
        $video->filters()->custom($command);
        $captionFile='caption_'.$videoFile;
        //Save the video with caption
        $video->save($format, $captionStaticFilePath.$captionFile);
        die('done');
    }catch(Exception $e){
        echo $e->getMessage();die;
    }



Question: How to add debugging in php-ffmpeg?
Just add below code after the initialize the ffmpeg object.

$ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener());
$ffmpeg->getFFMpegDriver()->on('debug', function ($message) {
    echo $message;
});



Question: Share Few useful urls for drawtext?
https://www.ffmpeg.org/ffmpeg-all.html#fade
https://ffmpeg.org/ffmpeg-filters.html


Friday 19 October 2018

What is NodeJS API wrapper?

What is NodeJS API wrapper?

Question: What is NodeJS API wrapper?
It is RESTful services which is integrated within NodeJS.
With use the these RESTful services, we can integrate third party modules in our nodeJS application.


Question: How can use NodeJS API wrapper in our application?
Just download the source code using npm command and use it.

For Example
npm install --save google



Question: Give Few Examples of NodeJS API wrapper?
Following are few examples which provide API Wrapper to use them:
  1. Braintree provide API so that we can integrate paypment stuff, In NodeJS application.
    https://www.npmjs.com/package/braintree

    Offical Website: https://developers.braintreepayments.com
    API Wrapper Purpose: Integrate braintree in nodeJS application
  2. Ipinfodb provide API so that we can get client detail using Ip Address, In NodeJS applicaton.
    https://www.npmjs.com/package/node-ipinfodb
    Offical Website: http://www.ipinfodb.com/
    API Wrapper Purpose: Get client details like city, state, address, and zip etc using Ip Address.
  3. Google provide API wrapper to get search results from google.com
    https://www.npmjs.com/package/google
    Offical Website: https://gooogle.com
    API Wrapper Purpose: Get search Results from google.com

Question: From where we can find all NodeJS Wrappers?
https://www.npmjs.com/browse/keyword/wrapper