PHP code example of modularavel / cloudflare-stream-video

1. Go to this page and download the library: Download modularavel/cloudflare-stream-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/ */

    

modularavel / cloudflare-stream-video example snippets


use Modularavel\CloudflareStreamVideo\Facades\CloudflareStreamVideo;

// Upload a local video file using the TUS resumable protocol.
$video = CloudflareStreamVideo::upload('/path/to/video.mp4', [
    'meta' => ['name' => 'My Awesome Video', 'source' => 'laravel'],
    'maxDurationSeconds' => 3600,
]);

echo $video->uid;    // e.g. "cab807e0c477d01baq20f66c3d1dfc26cf"
echo $video->status; // e.g. "ready"

// Fetch and process a video from a publicly accessible URL.
$video = CloudflareStreamVideo::copy('https://example.com/video.mp4', [
    'meta' => ['name' => 'Imported Video'],
]);

// Generate a one-time upload URL for client-side use.
$upload = CloudflareStreamVideo::createDirectUpload([
    'maxDurationSeconds' => 300,
    'expiry' => now()->addHours(1)->toIso8601String(),
]);

// Pass $upload->uploadUrl to your frontend client.
// The upload URL is valid until expiry or until the video is uploaded.

// Retrieve all videos with optional filtering.
$videos = CloudflareStreamVideo::listVideos([
    'status' => 'ready',
    'limit' => 100,
]);

foreach ($videos as $video) {
    echo $video->uid . ' - ' . $video->status;
}

$video = CloudflareStreamVideo::getVideo('cab807e0c477d01baq20f66c3d1dfc26cf');

echo $video->uid;
echo $video->status;
echo $video->duration;
echo $video->getHlsUrl();   // e.g. "https://customer.cloudflarestream.com/.../manifest/video.m3u8"
echo $video->getDashUrl();  // e.g. "https://customer.cloudflarestream.com/.../manifest/video.mpd"
echo $video->getEmbedUrl(); // e.g. "https://embed.videodelivery.net/embed/iframe/..."

$video = CloudflareStreamVideo::updateVideo('cab807e0c477d01baq20f66c3d1dfc26cf', [
    'meta' => ['name' => 'Updated Title', 'tags' => 'tutorial,php'],
    '

$deleted = CloudflareStreamVideo::deleteVideo('cab807e0c477d01baq20f66c3d1dfc26cf');

use Modularavel\CloudflareStreamVideo\Facades\CloudflareStreamVideo;

// Generate a full signed embed URL (valid for 2 hours by default).
$signedUrl = CloudflareStreamVideo::signedEmbedUrl(
    'cab807e0c477d01baq20f66c3d1dfc26cf',
    now()->addHours(2)->toDateTimeString(),
);

// Generate only the signed token (for use in custom embed URLs or HLS/DASH manifests).
$token = CloudflareStreamVideo::signedToken(
    'cab807e0c477d01baq20f66c3d1dfc26cf',
    now()->addHours(2)->toDateTimeString(),
);

use Modularavel\CloudflareStreamVideo\Facades\CloudflareStreamVideo;

$watermark = CloudflareStreamVideo::createWatermark(
    name: 'Brand Logo',
    url: 'https://example.com/watermark.png',
    position: 'bottom-right',
    opacity: 0.8,
    width: 200,
    height: 50,
);

echo $watermark->uid; // e.g. "wm-brand-123"

$watermarks = CloudflareStreamVideo::listWatermarks();

foreach ($watermarks as $wm) {
    echo $wm->uid . ' - ' . $wm->name;
}

$watermark = CloudflareStreamVideo::getWatermark('wm-brand-123');

$watermark = CloudflareStreamVideo::updateWatermark('wm-brand-123', [
    'opacity' => 0.5,
    'position' => 'top-left',
]);

$deleted = CloudflareStreamVideo::deleteWatermark('wm-brand-123');

$video = CloudflareStreamVideo::applyWatermark(
    'cab807e0c477d01baq20f66c3d1dfc26cf',
    'wm-brand-123',
);

$video = CloudflareStreamVideo::removeWatermark('cab807e0c477d01baq20f66c3d1dfc26cf');

// In a service provider or event subscriber:
use Modularavel\CloudflareStreamVideo\Events\StreamVideoProcessed;

Event::listen(StreamVideoProcessed::class, function (StreamVideoProcessed $event) {
    // $event->eventType  — e.g. "video.ready", "video.error"
    // $event->videoUid   — the Cloudflare video UID
    // $event->payload    — the full raw webhook payload

    if ($event->eventType === 'video.ready') {
        // Video encoding completed — update your database, notify users, etc.
        $video = Video::where('cloudflare_uid', $event->videoUid)->first();
        if ($video) {
            $video->update(['status' => 'ready']);
        }
    }

    if ($event->eventType === 'video.error') {
        // Video encoding failed — log the error, notify admins, etc.
        Log::error('Cloudflare Stream encoding failed for video: ' . $event->videoUid, [
            'payload' => $event->payload,
        ]);
    }
});
bash
php artisan vendor:publish --tag="cloudflare-stream-video"
bash
php artisan vendor:publish --tag="cloudflare-stream-video-config"
bash
php artisan vendor:publish --tag="cloudflare-stream-video-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="cloudflare-stream-video-views"
bash
php artisan vendor:publish --tag="cloudflare-stream-video-assets"
bash
php artisan vendor:publish --tag="cloudflare-stream-video-migrations"
php artisan migrate
bash
php artisan cloudflare-stream-video:placeholder
bash
php artisan cloudflare-stream-video:sync