PHP code example of utopia-php / video

1. Go to this page and download the library: Download utopia-php/video library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

utopia-php / video example snippets




use Utopia\Video\Encoder;

$encoder = new Encoder();

$info = $encoder->probe('/path/to/video.mp4');

$info->duration;      // 64.5 seconds
$info->milliseconds(); // 64500
$info->width;         // 1920
$info->videoCodec;    // 'h264'
$info->tags['title']; // container metadata
$info->chapters;      // list<Chapter>
$info->tracks;        // every stream, embedded subtitles 

use Utopia\Video\Encoder;
use Utopia\Video\Format\X264;
use Utopia\Video\Representation;

$path = (new Encoder())
    ->open('/path/to/video.mp4')
    ->format((new X264())->crf(22)->bframes(3))
    ->add(new Representation(width: 1280, height: 720, video: 2538, audio: 128))
    ->encode('/path/to/720p.mp4');

use Utopia\Video\Format\X264;
use Utopia\Video\Output\Cmaf;
use Utopia\Video\Packager;
use Utopia\Video\Progress;
use Utopia\Video\Representation;

$package = (new Packager())
    ->open('/path/to/video.mp4')
    ->format((new X264())->crf(22)->bframes(3))
    ->add(
        new Representation(width: 640, height: 360, video: 800, audio: 96),
        new Representation(width: 1280, height: 720, video: 2538, audio: 128),
    )
    ->output((new Cmaf())->segment(6))
    ->on(Packager::PROGRESS, fn (Progress $p) => print("{$p->percent}%\n"))
    ->pack('/path/to/output');

$package->segments();  // every segment, with duration, size and init flag
$package->variants();  // per rendition metadata
$package->manifests(); // the playlists written, if any were kept
$package->files();     // everything produced, ready to upload

$package = (new Packager())->open('/path/to/movie.mkv')   // 4 dubs
    ->format(new X264())
    ->add($rep)
    ->output(new Cmaf())
    ->pack('/out');

foreach ($package->variants() as $variant) {
    $variant->type;      // 'video' | 'audio'
    $variant->language;  // 'eng', 'spa', 'fra', 'jpn', …
}

->output((new Dash())->template(false)->timeline(false)->manifests(false))

use Utopia\Video\Thumb;
use Utopia\Video\Tile;

$encoder = new Encoder();

$encoder->grab('/path/to/video.mp4', '/out/poster.jpg', (new Thumb())->width(640));
$sheet = $encoder->tile('/path/to/video.mp4', '/out/timeline');
$encoder->subtitle('/path/to/video.mkv', 2, '/out/eng.vtt', timeout: 60); // stream 2 as WebVTT

$sheet->cues();   // list<Cue> with start, end, x, y, width, height
$sheet->render(fn (string $file) => "/preview/{$file}"); // WebVTT with your own URLs

use Utopia\Video\Adapter\FFmpeg;

// One backend, shared by both facades.
$ffmpeg = new FFmpeg(threads: 4);

$encoder = new Encoder($ffmpeg);
$packager = new Packager($ffmpeg);

$encoder = new Encoder(probe: new MyProbe());

use Utopia\Video\Adapter;
use Utopia\Video\Adapter\FFmpeg;

$encoder = new Encoder(new FFmpeg(level: Adapter::VERBOSE));

$encoder->on(Encoder::LOG, fn (string $line) => error_log($line));

use Utopia\Video\Reporter;

$packager = new Packager(reporter: new Reporter\Silent());       // nothing at all
$encoder = new Encoder(reporter: new MyPsrLoggerReporter($log)); // two methods to implement

$encoder->getName();   // 'ffmpeg'
$packager->getName();  // 'ffmpeg'

$preset = (new X264())->crf(22);          // shared, safe
$output = (new Cmaf())->segment(6);       // shared, safe

go(function () use ($preset, $output, $in) {
    // One facade per coroutine. Constructing one is a handful of assignments —
    // nothing runs until open() — so there is nothing to save by sharing it,
    // and a facade holds the job chain, which two coroutines would corrupt.
    (new Packager())
        ->open($in)
        ->format($preset->keyframe(2.0))  // a copy; $preset is untouched
        ->add(new Representation(width: 1280, height: 720, video: 2538, audio: 128))
        ->output($output)
        ->pack('/out/a');
});

Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);

use Utopia\Video\Adapter;

$encoder = new Encoder(
    Adapter::encoder($config['encoder'], level: $config['level']),   // 'ffmpeg' | 'mock'
    Adapter::probe($config['probe']),                                // 'ffprobe' | 'mock'
);

use Utopia\Video\Adapter;
use Utopia\Video\Adapter\Packager;
use Utopia\Video\Package;

class Shaka extends Adapter implements Packager
{
    protected const NAME = 'shaka';

    protected const BINARY = 'packager';

    // Optional: override the default display level for this backend only.
    protected const LEVEL = self::WARNING;

    public function pack(string $dir): Package
    {
        // ...
    }
}
bash
composer