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.
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(),
);
// 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,
]);
}
});