php技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > PHP编程 > php技巧 > PHP FFMpeg音视频操作

PHP调用FFMpeg实现音视频操作的示例详解

作者:极客飞兔

这篇文章主要为大家详细介绍了PHP如何调用FFMpeg实现简单的音视频操作,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解下

安装PHP-FFMpeg

composer require php-ffmpeg/php-ffmpeg

视频中提取一张图片

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('image.jpg');

视频中提取多张图片

require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->extractMultipleFrames(FFMpeg\Filters\Video\ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, '/path/to/destination/folder/')
    ->synchronize();

$video
    ->save(new FFMpeg\Format\Video\X264(), '/path/to/new/file');

调整视频大小

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(160,90), FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_FIT, true);

$video
    ->save(new FFMpeg\Format\Video\X264(), 'video_new.mp4');

视频添加水印

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->watermark('watermark.png', array(
        'position' => 'relative',
        'bottom' => 10,
        'right' => 10,
    ));

$video->save(new FFMpeg\Format\Video\X264(), 'video_watermark_new.mp4');

生成音频波形

<?php
// 直接处理音频
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$audio = $ffmpeg->open('audio.mp3');

$waveform = $audio->waveform(640, 120, array('#00FF00'));

$waveform->save('waveform.png');
<?php
// 从视频中提取音频后再生成波形图
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

// Open your video file / 打开视频文件
$video = $ffmpeg->open( 'video.mp4' );

// Set an audio format / 设置音频格式
$audio_format = new FFMpeg\Format\Audio\Mp3();

// Extract the audio into a new file as mp3 / 保存成 mp3 文件
$video->save($audio_format, 'audio.mp3');

// Set the audio file / 打开 mp3 文件
$audio = $ffmpeg->open( 'audio.mp3' );

// Create the waveform / 生成波形图
$waveform = $audio->waveform();
$waveform->save( 'waveform.png' );

音频转换

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$audio = $ffmpeg->open('audio.mp3');

$format = new FFMpeg\Format\Audio\Flac();

$format->on('progress', function ($audio, $format, $percentage) {
    echo "$percentage % 进度";
});

$format
    ->setAudioChannels(2)
    ->setAudioKiloBitrate(256);

$audio->save($format, 'audio.flac');

给音频添加元数据

支持给音频添加标题、艺术家、专辑、艺术家、作曲家、曲目、年份、描述、艺术品等

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$audio = $ffmpeg->open('audio.mp3');

$audio->filters()->addMetadata([
    "title" => "this is a song title", // 标题
    "artist" => "tinygeeker", // 艺术家
    "album" => "1024", // 专辑
    "composer" => "tinygeeker", // 作曲家
    "track" => 1, // 音轨
    "year" => 2023, // 年份
    "description" => "this is a song description", // 描述
    "artwork" => "artwork.jpg" // 艺术品
]);

$audio->save(new \FFMpeg\Format\Audio\Mp3(), 'audio_new.mp3');

拼接多个音视频

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video_1.mp4');

$video
    ->concat(array('video_1.mp4', 'video_2.mp4', 'video_3.mp4'))
    ->saveFromSameCodecs('video.mp4', TRUE);

截取音视频

TimeCode::fromSeconds: 有两个参数,第一个是从第几秒开始截取,第二个是截取多少秒,如果不填,则截取到最后

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30));

$video
    ->save(new FFMpeg\Format\Video\X264(), 'video_clip.mp4');

提取 gif 动图

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(30), new FFMpeg\Coordinate\Dimension(640, 480), 3)
    ->save('video.gif');

裁剪视频

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->crop(new FFMpeg\Coordinate\Point("t*100", 0, true), new FFMpeg\Coordinate\Dimension(200, 600));

$video->save(new FFMpeg\Format\Video\X264(), 'video_crop.mp4');

转换视频格式

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$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, 'video.avi');

调整视频帧率

<?php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

$video = $ffmpeg->open('video.mp4');

$video
    ->filters()
    ->framerate(new \FFMpeg\Coordinate\FrameRate(1000), 120);

$video
    ->save(new FFMpeg\Format\Video\X264(), 'video_rate.mp4');

获取音视频信息

<?php
require 'vendor/autoload.php';

$ffprobe = FFMpeg\FFProbe::create([
    'ffmpeg.binaries' => 'F:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'F:\ffmpeg\bin\ffprobe.exe',
]);

// 获取音视频信息
$videoInfo = $ffprobe->format('video.mp4');

// 也可以通过 get 获取指定信息的内容,第二个参数为默认值,如果获取不到该信息的话返回默认值
$duration = $ffprobe
    ->format('audio.mp3')
    ->get('duration', 100);

var_dump($videoInfo);
var_dump($duration);

以上就是PHP调用FFMpeg实现音视频操作的示例详解的详细内容,更多关于PHP FFMpeg音视频操作的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文