Thursday 22 February 2018

FFMPEG commands - Most common commands

FFMPEG commands - Most common commands used

Question: How to combine two mp4 video files?
ffmpeg -i video1.mp4 -i video2.mp4  -filter_complex concat=n=2:v=1:a=1 -f mp4 -y finalvideo.mp4



Question: How to combine three mp4 video files?
ffmpeg -i video1.mp4 -i video2.mp4   -i video3.mp4 -filter_complex concat=n=2:v=1:a=1 -f mp4 -y finalvideo.mp4



Question: How to combine four mp4 video files?
ffmpeg -i video1.mp4 -i video2.mp4   -i video3.mp4 -filter_complex concat=n=2:v=1:a=1 -f mp4 -y finalvideo.mp4



Question: How to combine two video file (and disable video)?
ffmpeg -i video1.mp4 -i video2.mp4  -filter_complex concat=n=2:v=1:a=1 -f mp4 -vn -y finalvideo.mp4



Question: How to combine two video file (and disable audio)?
ffmpeg -i video1.mp4 -i video2.mp4  -filter_complex concat=n=2:v=1:a=1 -f mp4 -an -y finalvideo.mp4



Question: What are the different -filter_complex options used in ffmpeg?
concat means use the media concatenate.
n means total count of input files.
v means has video? use 0 = no video, 1 = contains video.
a means has audio? use 0 = no audio, 1 = contain audio.
-f means force set file format (to see all supported formats, use ffmpeg -formats)
-vn means disable video 
-an means disable audio 
-y means overwrite output files



Question: How to trim first 10 seconds from the audio using ffmpeg?
ffmpeg -i input_aac.mp4 -ss 10  -acodec copy output_aac.mp4



Question: How to capture the audio between 10 to 30 audio (20 seconds audio)?
ffmpeg -ss 10 -t 20 -i file.mp3 -y output.mp3

-ss 10 - Start at 0 seconds
-t 20 - Capture 20 seconds (from 0, so 0:10 - 0:30).
-y : force output file to overwrite.



Question: How to the rescaling the video with specified size 320:240?
ffmpeg -i input.mp4 -vf scale=320:240 output.mp4



Question: How to the rescaling the image with specified size 320:240?
ffmpeg -i input.jpg -vf scale=320:240 output_320x240.jpg



Question: How to the rescaling the image with specified width size 320 with keeping the Aspect Ratio?
ffmpeg -i input.jpg -vf scale=320:-1 output_320xauto.jpg



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');