<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
edicleoberners / php-ffmpeg-video-streaming example snippets
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$config = [
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFmpeg should use
];
$log = new Logger('FFmpeg_Streaming');
$log->pushHandler(new StreamHandler('/var/log/ffmpeg-streaming.log')); // path to log file
$ffmpeg = Streaming\FFMpeg::create($config, $log);
$video = $ffmpeg->open('/var/media/video.mp4');
$video = $ffmpeg->open('https://www.aminyazdanpanah.com/?"PATH TO A VIDEO FILE" or "PATH TO A LIVE HTTP STREAM"');
$capture = $ffmpeg->capture("CAMERA NAME OR SCREEN NAME");
$video->dash()
->x264() // Format of the video. Alternatives: hevc() and vp9()
->autoGenerateRepresentations() // Auto generate representations
->save(); // It can be passed a path to the method or it can be null
use Streaming\Representation;
$r_144p = (new Representation)->setKiloBitrate(95)->setResize(256, 144);
$r_240p = (new Representation)->setKiloBitrate(150)->setResize(426, 240);
$r_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
$r_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
$r_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
$r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
$r_2k = (new Representation)->setKiloBitrate(6144)->setResize(2560, 1440);
$r_4k = (new Representation)->setKiloBitrate(17408)->setResize(3840, 2160);
$video->dash()
->x264()
->addRepresentations([$r_144p, $r_240p, $r_360p, $r_480p, $r_720p, $r_1080p, $r_2k, $r_4k])
->save('/var/media/dash-stream.mpd');
$video->hls()
->x264()
->autoGenerateRepresentations([720, 360]) // You can limit the number of representatons
->save();
use Streaming\Representation;
$r_360p = (new Representation)->setKiloBitrate(276)->setResize(640, 360);
$r_480p = (new Representation)->setKiloBitrate(750)->setResize(854, 480);
$r_720p = (new Representation)->setKiloBitrate(2048)->setResize(1280, 720);
$video->hls()
->x264()
->addRepresentations([$r_360p, $r_480p, $r_720p])
->save();
//A path you want to save a random key to your local machine
$save_to = '/home/public_html/"PATH TO THE KEY DIRECTORY"/key';
//An URL (or a path) to access the key on your website
$url = 'https://www.aminyazdanpanah.com/?"PATH TO THE KEY DIRECTORY"/key';
// or $url = '/"PATH TO THE KEY DIRECTORY"/key';
$video->hls()
->encryption($save_to, $url)
->x264()
->autoGenerateRepresentations([1080, 480, 240])
->save('/var/media/hls-stream.m3u8');
use Streaming\HLSSubtitle;
$persian = new HLSSubtitle('/var/subtitles/subtitles_fa.vtt', 'فارسی', 'fa');
$persian->default();
$english = new HLSSubtitle('/var/subtitles/subtitles_en.vtt', 'english', 'en');
$german = new HLSSubtitle('/var/subtitles/subtitles_de.vtt', 'Deutsch', 'de');
$chinese = new HLSSubtitle('/var/subtitles/subtitles_zh.vtt', '中文', 'zh');
$spanish = new HLSSubtitle('/var/subtitles/subtitles_es.vtt', 'Español', 'es');
$video->hls()
->subtitles([$persian, $english, $german, $chinese, $spanish])
->x264()
->autoGenerateRepresentations([1080, 720])
->save('/var/media/hls-stream.m3u8');
$format = new Streaming\Format\X264();
$format->on('progress', function ($video, $format, $percentage){
// You can update a field in your database or can log it to a file
// You can also create a socket connection and show a progress bar to users
echo sprintf("\rTranscoding...(%s%%) [%s%s]", $percentage, str_repeat('#', $percentage), str_repeat('-', (100 - $percentage)));
});
$video->dash()
->setFormat($format)
->autoGenerateRepresentations()
->save();