定制 foxws/laravel-shaka 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

foxws/laravel-shaka

Composer 安装命令:

composer require foxws/laravel-shaka

包简介

A Laravel package to interact with Shaka Packager for media packaging.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel integration for Google's Shaka Packager, enabling you to create adaptive streaming content (HLS, DASH) with a fluent, Laravel-style API.

use Foxws\Shaka\Facades\Shaka;

$result = Shaka::fromDisk('s3')
    ->open('videos/input.mp4')
    ->addVideoStream('videos/input.mp4', 'video_1080p.mp4', ['bandwidth' => '5000000'])
    ->addVideoStream('videos/input.mp4', 'video_720p.mp4', ['bandwidth' => '3000000'])
    ->addAudioStream('videos/input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->withSegmentDuration(6)
    ->export()
    ->toDisk('export')
    ->save();

Features

  • 🎬 Fluent API - Laravel-style chainable methods
  • 📁 Multiple Disks - Works with local, S3, and custom filesystems
  • 🎯 Adaptive Bitrate - Create multi-quality streams easily
  • 🔒 Encryption & DRM - Built-in support for content protection
  • 📺 HLS & DASH - Support for both streaming protocols
  • 🧪 Testable - Clean architecture with mockable components
  • 📝 Type-Safe - Full PHP 8.1+ type declarations

Documentation

📚 Full Documentation

Requirements

  • PHP 8.3 or higher
  • Laravel 12.x or higher
  • Shaka Packager binary installed on your system or Docker container

Installation

Install the package via composer:

composer require foxws/laravel-shaka

Publish the config file:

php artisan vendor:publish --tag="shaka-config"

Installing Shaka Packager

Install Shaka Packager binary on your system. Visit the Shaka Packager releases page for installation instructions.

Verify Installation

After installation, verify that Shaka Packager is properly configured:

php artisan shaka:info

This will check:

  • Binary exists and is executable
  • Can retrieve version information
  • Configuration is properly set up
  • Temporary directory is accessible

Quick Start

Basic Usage

use Foxws\Shaka\Facades\Shaka;

$result = Shaka::open('input.mp4')
    ->addVideoStream('input.mp4', 'video.mp4')
    ->addAudioStream('input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->export()
    ->save();

Adaptive Bitrate Streaming

$result = Shaka::open('input.mp4')
    ->addVideoStream('input.mp4', 'video_1080p.mp4', ['bandwidth' => '5000000'])
    ->addVideoStream('input.mp4', 'video_720p.mp4', ['bandwidth' => '3000000'])
    ->addVideoStream('input.mp4', 'video_480p.mp4', ['bandwidth' => '1500000'])
    ->addAudioStream('input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->withSegmentDuration(6)
    ->export()
    ->save();

Working with Different Disks

$result = Shaka::fromDisk('s3')
    ->open('videos/input.mp4')
    ->addVideoStream('videos/input.mp4', 'video.mp4')
    ->addAudioStream('videos/input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->export()
    ->toDisk('export') // Save output to a different disk (e.g., local, s3, etc.)
    ->toPath('exports/') // (Optional) Save to a subdirectory on the target disk
    ->save();

HLS with Encryption

// Basic encryption with auto-generated AES-128 key
Shaka::open('input.mp4')
    ->addVideoStream('input.mp4', 'video.mp4')
    ->addAudioStream('input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->withAESEncryption()  // Auto-generates key with 'cbc1' scheme
    ->export()
    ->save();

// With key rotation (generates key_0.key, key_1.key, etc.)
Shaka::open('input.mp4')
    ->addVideoStream('input.mp4', 'video.mp4')
    ->addAudioStream('input.mp4', 'audio.mp4')
    ->withHlsMasterPlaylist('master.m3u8')
    ->withAESEncryption()
    ->withKeyRotationDuration(60)  // Rotate every 60 seconds
    ->export()
    ->toDisk('s3')
    ->save();

See AES Encryption Guide for complete documentation.

Dynamic URL Resolvers (HLS & DASH)

Serve encrypted streaming content with S3 signed URLs:

HLS Example:

use Foxws\Shaka\Http\DynamicHLSPlaylist;
use Illuminate\Support\Facades\Storage;

public function playlist(Video $video)
{
    return (new DynamicHLSPlaylist('s3'))
        ->open("videos/{$video->id}/master.m3u8")
        ->setKeyUrlResolver(fn ($key) => Storage::disk('s3')->temporaryUrl(
            "videos/{$video->id}/{$key}",
            now()->addHour()
        ))
        ->setMediaUrlResolver(fn ($file) => Storage::disk('s3')->temporaryUrl(
            "videos/{$video->id}/{$file}",
            now()->addHours(2)
        ))
        ->toResponse(request());
}

DASH Example:

use Foxws\Shaka\Http\DynamicDASHManifest;
use Illuminate\Support\Facades\Storage;

public function manifest(Video $video)
{
    return (new DynamicDASHManifest('s3'))
        ->open("videos/{$video->id}/manifest.mpd")
        ->setKeyUrlResolver(fn ($key) => Storage::disk('s3')->temporaryUrl(
            "videos/{$video->id}/{$key}",
            now()->addHour()
        ))
        ->setMediaUrlResolver(fn ($file) => Storage::disk('s3')->temporaryUrl(
            "videos/{$video->id}/{$file}",
            now()->addHours(2)
        ))
        ->setInitUrlResolver(fn ($file) => Storage::disk('s3')->temporaryUrl(
            "videos/{$video->id}/{$file}",
            now()->addHours(2)
        ))
        ->toResponse(request());
}

Use cases for URL resolvers:

  • 🔐 Generate signed URLs for secure content delivery
  • 🌐 Integrate with CDN services
  • 🏢 Support multi-tenant applications
  • 🔄 Implement dynamic key rotation
  • 📊 Track media access patterns

See URL Resolver Examples and Documentation for more details.

Available Methods

Disk Management

  • fromDisk(string $disk) - Set the disk to use
  • openFromDisk(string $disk, $paths) - Set disk and open files in one call
  • getDisk() - Get the current disk instance

Media Management

  • open($paths) - Open one or more media files
  • get() - Get the MediaCollection
  • streams() - Get auto-generated Stream objects

Stream Configuration

  • addVideoStream(string $input, string $output, array $options = []) - Add video stream
  • addAudioStream(string $input, string $output, array $options = []) - Add audio stream
  • addTextStream(string $input, string $output, array $options = []) - Add text/caption/subtitle stream
  • addStream(array $stream) - Add custom stream

Output Configuration

  • withHlsMasterPlaylist(string $path) - Set HLS master playlist output
  • withMpdOutput(string $path) - Set DASH manifest output
  • withSegmentDuration(int $seconds) - Set segment duration
  • withAESEncryption(string $keyFilename = 'key', ?string $protectionScheme = 'cbc1', ?string $label = null) - Enable AES-128 encryption
  • withKeyRotationDuration(int $seconds) - Enable key rotation for encryption
  • toDisk(string $disk) - Set the target disk for output
  • toPath(string $path) - Set the target output path (subdirectory)
  • withVisibility(string $visibility) - Set file visibility (e.g., 'public', 'private')

Execution & Utilities

  • export() - Execute the packaging operation (returns result object)
  • save(?string $path = null) - Save outputs to disk (optionally to a specific path)
  • getCommand() - Get the final command string (for debugging)
  • dd() - Dump the final command and end the script
  • afterSaving(callable $callback) - Register a callback to run after saving

Dynamic URL Resolvers

DynamicHLSPlaylist:

  • new DynamicHLSPlaylist(?string $disk) - Create HLS playlist processor
  • open(string $path) - Open a playlist file
  • setKeyUrlResolver(callable $resolver) - Set resolver for encryption key URLs
  • setMediaUrlResolver(callable $resolver) - Set resolver for media segment URLs
  • setPlaylistUrlResolver(callable $resolver) - Set resolver for sub-playlist URLs
  • get() - Get processed playlist content
  • all() - Get all processed playlists (master + segments)
  • toResponse($request) - Return as HTTP response

DynamicDASHManifest:

  • new DynamicDASHManifest(?string $disk) - Create DASH manifest processor
  • open(string $path) - Open a manifest file
  • setMediaUrlResolver(callable $resolver) - Set resolver for media segment URLs
  • setInitUrlResolver(callable $resolver) - Set resolver for initialization segment URLs
  • get() - Get processed manifest content
  • toResponse($request) - Return as HTTP response

See the Quick Reference for complete API documentation.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

If you discover a security vulnerability, please report it via a private channel (e.g., email or GitHub issues) rather than publicly disclosing it.

Acknowledgments

This package was inspired by and learned from:

Much of the existing logic and design patterns from these excellent packages helped shape this implementation. Many thanks to their authors and contributors!

Projects Built on Laravel Shaka Packager

  • Stry - A modern streaming platform built on top of Laravel Shaka Packager.

License

The MIT License (MIT). Please see License File for more information.

foxws/laravel-shaka 适用场景与选型建议

foxws/laravel-shaka 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.98k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「media」 「audio」 「video」 「streaming」 「laravel」 「packager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 foxws/laravel-shaka 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 foxws/laravel-shaka 我们能提供哪些服务?
定制开发 / 二次开发

基于 foxws/laravel-shaka 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18