Firstly install PHP-FFMpeg
project (https://github.com/PHP-FFMpeg/PHP-FFMpeg)
(just run for install: composer require php-ffmpeg/php-ffmpeg
)
And then you can use of this simple code:
<?php
require 'vendor/autoload.php';
$sec = 10;
$movie = 'test.mp4';
$thumbnail = 'thumbnail.png';
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($movie);
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($sec));
$frame->save($thumbnail);
echo '<img src="'.$thumbnail.'">';
--code2
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('./xxx_1544.mp4');
$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe
->format('./xxx1544.mp4') // extracts file informations
->get('duration');
//得到视频的长度,单位为秒
Description: It’s newer and more modern project and works with latest version of FFMpeg
and PHP
. Note that it’s required to proc_open()
PHP function.
<?php declare(strict_types=1); require_once './../vendor/autoload.php'; $ffmpeg = FFMpeg\FFMpeg::create(); $video = $ffmpeg->open('intro.mp4'); $video ->filters() ->resize(new FFMpeg\Coordinate\Dimension(320, 240)) ->synchronize(); $video->save(new FFMpeg\Format\Video\X264('aac'), 'minified-intro.mp4'); $video = $ffmpeg->open('minified-intro.mp4'); $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1)) ->save('save.jpg');
–change dimension
$dimension = new FFMpeg\Coordinate\Dimension('-1', '720');
$mode = FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_SCALE_HEIGHT;
$video->filters()->resize($dimension, $mode)->synchronize();
— get mimension
$ffprobe = FFMpeg\FFProbe::create();
$video_dimensions = $ffprobe
->streams( $full_video_path ) // extracts streams informations
->videos() // filters video streams
->first() // returns the first video stream
->getDimensions();