FFMpeg - from installation to use

1. Write in front

Recording the use of FFMpeg  PHP, which can process audio and video (extract images, transcode, add watermarks, rotate, etc.), is a powerful wheel for us.

2. Installation steps

1. Add package dependencies, provided that composer has been loaded.

composer require php-ffmpeg/php-ffmpeg

requires

  • php: ^5.3.9 || ^7.0

If it goes well, composer.json already has:

  "php-ffmpeg/php-ffmpeg": "^0.14",

2. At this time, if you use the official example to test, you may report an error: Unable to load FFProbe. The discussion on this issue can be viewed here. We need to understand the server environment of php-ffmpeg: here is a list of different systems to install FFmpeg The method (install FFmpeg), we take mac as an example, use brew to install:

Press Command+Space and type Terminal and press enter/return key.

Run in Terminal app:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

and press enter/return key.

If the screen prompts you to enter a password, please enter your Mac's user password to continue. When you type the password, it won't be displayed on screen, but the system would accept it. So just type your password and press ENTER/RETURN key. Then wait for the command to finish.

Run:

brew install ffmpeg

Done! You can now use  ffmpeg. ( For more information, refer to here )

3. Packaging method

function ffmpeg()
{
    $configuration = array(
        'ffmpeg.binaries' => '/usr/local/bin/ffmpeg',
        'ffprobe.binaries' => '/usr/local/bin/ffprobe', //这里是电脑或服务器上所安装的位置
//        'timeout'          => 3600, // The timeout for the underlying process
//        'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
    );
    $ffmpeg = FFMpeg\FFMpeg::create($configuration);
 
//这里根据需要选择视频(本服务器的视频或者其他网站的视频地址)
//    $video = $ffmpeg->open(ROOT_PATH.'public/upload/test/test.mpg');
//    $video = $ffmpeg->open(ROOT_PATH.'public/upload/test/test1.mp4');
    $video = $ffmpeg->open('https://qiniu-xpc4.xpccdn.com/5cde40867122c.mp4');
//    $audio = $ffmpeg->open(ROOT_PATH.'public/upload/test/audio.mp3');
 
 
//1.1提取单张图片-第n秒的视频帧
    $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
        ->save(ROOT_PATH.'public/upload/test/frame_10.jpg');
 
    $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(25))
       ->save(ROOT_PATH.'public/upload/test/frame_25.jpg');
//1.2提取多张图片--失败
    $video->filters()->extractMultipleFrames(FFMpeg\Filters\Video\ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, ROOT_PATH.'public/upload/test/')
       ->synchronize();
   $video->save(new FFMpeg\Format\Video\X264(), ROOT_PATH.'public/upload/test/');
 
//2.1将视频转码
    $format = new FFMpeg\Format\Video\X264();
    $format->on('progress', function ($video, $format, $percentage) {
        echo "$percentage % transcoded";
    });
    $format->setKiloBitrate(1000)
        ->setAudioChannels(2)
        ->setAudioKiloBitrate(256);
   $video->save($format, ROOT_PATH.'public/upload/test/test.avi');
 
   $video->save(new FFMpeg\Format\Video\X264(), ROOT_PATH.'public/upload/test/export-x264.mp4')
        ->save(new FFMpeg\Format\Video\WMV(), ROOT_PATH.'public/upload/test/export-wmv.wmv')
        ->save(new FFMpeg\Format\Video\WebM(), ROOT_PATH.'public/upload/test/export-webm.webm');
 
// 2.2将视频按指定点剪切,并调整大小---失败-Encoding failed
    $video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(15));
    $video->filters()->resize(new FFMpeg\Coordinate\Dimension(320, 240), FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_INSET, true);
    $video->save(new FFMpeg\Format\Video\X264(), ROOT_PATH.'public/upload/test/cut_and_resize.mp4');
 
//2.3将视频旋转90角度
    $angle = FFMpeg\Filters\Video\RotateFilter::ROTATE_90;
    $video->filters()->rotate($angle);
    $video->save(new FFMpeg\Format\Video\X264(), ROOT_PATH.'public/upload/test/ROTATE_90.mpg');
 
//2.4给视频添加水印
    $video->filters()
        ->watermark(ROOT_PATH.'public/upload/test/water.png', array(
            'position' => 'relative',
            'bottom' => 50,
            'right' => 50,
        ));
    $video->save(new FFMpeg\Format\Video\X264(), ROOT_PATH.'public/upload/test/watermark.mp4');
 
//2.5拼接不同视频,生成新的视频
    $video
        ->concat(array(ROOT_PATH.'public/upload/test/watermark.mp4', ROOT_PATH.'public/upload/test/export-x264.mp4'))
        ->saveFromSameCodecs(ROOT_PATH.'public/upload/test/contact.mp4', TRUE);
 
//3.1从视频里提取音频,并生成波形图片
    $audio_format = new FFMpeg\Format\Audio\Mp3();//Set an audio format
    $video->save($audio_format, ROOT_PATH.'public/upload/test/audio.mp3');//Extract the audio into a new file as mp3
    $audio = $ffmpeg->open( ROOT_PATH.'public/upload/test/audio.mp3' );//Set the audio file
    $waveform = $audio->waveform(640, 120, array('#00FF00')); // Create the waveform
    $waveform->save(ROOT_PATH.'public/upload/test/waveform.png' );
 
//3.2从视频里提取gif图片
   $video->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(2), new FFMpeg\Coordinate\Dimension(640, 480), 3)
       ->save(ROOT_PATH.'public/upload/test/gif.gif');
 
//4.音频转码
   $format = new FFMpeg\Format\Audio\Flac();
    $format->on('progress', function ($audio, $format, $percentage) {
        echo "$percentage % transcoded";
    });
    $format
        ->setAudioChannels(2)
        ->setAudioKiloBitrate(256);
    $audio->save($format, ROOT_PATH.'public/upload/test/audio.flac');
//5.媒体相关信息
    $ffprobe = FFMpeg\FFProbe::create($configuration);
    $ffprobe
        ->format(ROOT_PATH.'public/upload/test/contact.mp4') // extracts file informations
        ->get('duration');             // returns the duration property
print_r($ffprobe);exit;
//下面是打印的数据
 
[properties:FFMpeg\FFProbe\DataMapping\AbstractData:private] => Array
(
[filename] => /Applications/MAMP/htdocs/manfan_app/public/upload/test/contact.mp4
[nb_streams] => 1
[nb_programs] => 0
[format_name] => mov,mp4,m4a,3gp,3g2,mj2
[format_long_name] => QuickTime / MOV
[start_time] => 0.000000
[duration] => 22.000000
[size] => 2779382
[bit_rate] => 1010684
[probe_score] => 100
[tags] => Array
(
    [major_brand] => isom
    [minor_version] => 512
    [compatible_brands] => isomiso2avc1mp41
    [encoder] => Lavf58.20.100
 )
)
}

Notes on the above methods:

1. The files processed above are all in the public/upload/test/ folder. For more examples and small functions, visit github/packagist to view.

2. Among them, there are two test errors: Encoding failed, you can refer to this point: Discussion , it is basically a problem of environment configuration or dependence on other packages that are not loaded.

Finally, if you have any questions, you are welcome to discuss them, and try to avoid jumping into the pit.

Original  FFMpeg-from installation to use_use ffmpeg\ffmpeg;_Adam_Lu's Blog-CSDN Blog

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/131646841