Tuesday, 20 February 2018

php FFMpeg extract images from videos

php FFMpeg extract images from videos in every 10 or nth seconds

Question: From where download the PHP FFMpeg library?
Install the php FFMpeg with composer.
composer require php-ffmpeg/php-ffmpeg



Question: Check php FFMpeg installed properly or not?
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
print_r($ffmpeg);

If it will print the object means FFMpeg is proper configure.


Question: How to extract image from video?
require_once 'ffmpeglib/vendor/autoload.php'; 
$folderPath='convert/';

#Create the required Object
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($folderPath.'video.mp4');

#extra the image at 10 seconds
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame_10.jpg');


#extra the image at 20 seconds
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(20))
    ->save('frame_20.jpg');


#extra the image at 30 seconds
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(30))
    ->save('frame_30.jpg');



Question: How to extract image in every 10 seconds from video?
    require_once 'ffmpeglib/vendor/autoload.php'; 
    $folderPath='convert/';

    ##Create the required Object
    $ffmpeg = FFMpeg\FFMpeg::create();
    $video = $ffmpeg->open($folderPath.'video.mp4');

    for($i=10; $i<=300; $i+=10){
        $video
            ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($i))
            ->save($folderPath.'frame_'.$i.'.jpg');            

    }



Question: How to set the binary file path in ffpeg?
$ffmpeg = FFMpeg\FFMpeg::create(array(
    'ffmpeg.binaries'  => '/opt/local/ffmpeg/bin/ffmpeg',
    'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
    'timeout'          => 3600, // The timeout for the underlying process
    'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
));



Question: How to get the codec_name information of video?
$ffprobe    = \FFMpeg\FFProbe::create();
$codeAc=$ffprobe->streams($uploadingFileName[0])
        ->videos()
        ->first()
        ->get('codec_name');



Question: How to get the duration of video?
$ffprobe    = \FFMpeg\FFProbe::create();
$codeAc=$ffprobe->streams($uploadingFileName[0])->get('duration');        



Question: How to combine multiple video?
$staticFilePath='/videopath';
$video = $ffmpeg->open( $staticFilePath.'0.mp4');
        $video
        ->concat(array($staticFilePath.'0.mp4',$staticFilePath.'1.mp4',$staticFilePath.'2.mp4',$staticFilePath.'3.mp4'))    
        ->saveFromSameCodecs($staticFilePath.'final.mp4', TRUE);
//final.mp4 will be combined file



Question: How to combine multiple video from h265 to h264?
$staticFilePath='/videopath';
$video = $ffmpeg->open( $staticFilePath.'0.mp4');
$video
        ->concat($array($staticFilePath.'0.mp4',$staticFilePath.'1.mp4',$staticFilePath.'2.mp4',$staticFilePath.'3.mp4'))
        ->saveFromDifferentCodecs(new FFMpeg\Format\Video\X264,$staticFilePath.'final_264.mp4', true);  



Question: How to check file is video file OR Audio file?
$videoDataAvailable=$ffprobe->streams($videoFile)->videos();
if(count($videoDataAvailable)==0){
    echo 'AUDIO FILE';
}else{
    echo 'Video File FILE';
} 




Question: How to convert Mp4 to wav using php-ffmpeg?
 
require_once 'ffmpeglib/vendor/autoload.php'; 
$ffmpeg = FFMpeg\FFMpeg::create();
$format = new FFMpeg\Format\Audio\Wav();        
$videoFolderPath='folder';


$audioObj = $ffmpeg->open($videoFolderPath.'/myfile.mp4');    
$audioObj->save($format, $videoFolderPath.'/myfile.wav'); 





Question: How to convert Mp4 to mp3 using php-ffmpeg?
 
require_once 'ffmpeglib/vendor/autoload.php'; 
$ffmpeg = FFMpeg\FFMpeg::create();
$videoFolderPath='folder';

$mp3Format = new FFMpeg\Format\Audio\Mp3(); 
$audioObj = $ffmpeg->open($videoFolderPath.'/myfile.mp4');    
$audioObj->save($mp3Format, $videoFolderPath.'/myfile.mp3');