PHP code example of aleksandr-kuporosov / transcoder
1. Go to this page and download the library: Download aleksandr-kuporosov/transcoder 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/ */
aleksandr-kuporosov / transcoder example snippets
public < ... >::__construct(string $filePath, ServiceFactoryInterface $service = null)
use Arhitector\Transcoder\Audio;
use Arhitector\Transcoder\Video;
use Arhitector\Transcoder\Frame;
use Arhitector\Transcoder\Subtitle;
// аудио
$audio = new Audio('sample.mp3');
// видео
$video = new Video('sample.avi');
// изображения
$frame = new Frame('sample.jpg');
// субтитры
$subtitle = new Subtitle('sample.srt');
use Arhitector\Transcoder\Service\ServiceFactory;
$service = new ServiceFactory([
'ffprobe.path' => 'E:\devtools\bin\ffprobe.exe',
'ffmpeg.path' => 'E:\devtools\bin\ffmpeg.exe'
]);
// используем это
$video = new Video('sample.avi', $service);
$format = new VideoFormat();
$format->addListener('*', function ($event) {
// обработчик сработает на любое событие
});
$format = new AudioFormat();
$format->addListener('before', function ($event) {
$event->stopPropagation();
});
$format = new VideoFormat();
$format->addListener('progress', function ($event) {
/* @var Arhitector/Transcoder/Event/EventProgress $event */
var_dump($event->getPercent());
});
$adapter = new SimpleQueue\Adapter\MemoryQueueAdapter();
$queue = new SimpleQueue\Queue($queue);
$service = new Arhitector\Transcoder\Service\ServiceFactory([
Arhitector\Transcoder\Service\ServiceFactory::OPTION_USE_QUEUE => $queue
]);
$audio = new Arhitector\Transcoder\Audio('sample.mp3', $service);
// задача будет отправлена в очередь `$queue`
$audio->save($audio->getFormat(), 'new-sample.mp3');
var_dump($queue->pull()); // запросить задачу из очереди
use Arhitector\Transcoder\Video;
use Arhitector\Transcoder\Audio;
$video = new Video('sample.avi');
var_dump($video->getWidth(), $video->getHeight());
$audio = new Audio(__DIR__.'/audio.mp3', $factory);
var_dump($audio->getAudioChannels());
var_dump($audio->getFormat()->getTags());
use Arhitector\Transcoder\Video;
use Arhitector\Transcoder\Stream\AudioStreamInterface;
use Arhitector\Transcoder\Format\Mp3;
$video = new Video('sample.mp4');
foreach ($video->getStreams() as $stream)
{
// тут выбираем только аудио канал
if ($stream instanceof AudioStreamInterface)
{
$stream->save(new Mp3(), __DIR__.'/only-audio.mp3');
break; // видео может иметь несколько аудио потоков
}
}
use Arhitector\Transcoder\Audio;
use Arhitector\Transcoder\Format\Mp3;
$audio = new Audio('audio-file.wav');
$audio->save(new Mp3(), 'audio-file.mp3');
use Arhitector\Transcoder\Video;
use Arhitector\Transcoder\Format\VideoFormat;
$video = new Video('video-file.avi');
$video->save(new VideoFormat('aac', 'h264'), 'video-file.mp4');
use Arhitector\Transcoder\Audio;
$audio = new Audio('file.mp3');
$format = $audio->getFormat();
$format['artist'] = 'Новый артист';
$auiod->save($format, 'new-file.mp3');
use Arhitector\Transcoder\Audio;
use Arhitector\Transcoder\Frame;
$audio = new Audio(__DIR__.'/sample.mp3');
$streams = $audio->getStreams();
$new_cover = (new Frame(__DIR__.'/sample.jpg'))
->getStreams()
->getFirst();
// индекс `0` - аудио-дорожка, `1` - обложка.
$streams[1] = $new_cover;
$audio->save($audio->getFormat(), 'sample-with-new-cover.mp3');
public < ... >::addFilter(FilterInterface $filter, $priority = 0);
// добавляем любой фильтр
$video->addFilter($filter);
// добавляем фильтр с приоритетом = 99.
$audio->addFilter($filter, 99);
use Arhitector\Transcoder\Filter\SimpleFilter;
public SimpleFilter::__construct(array $parameters = [])
//
$filter = new SimpleFilter([
'video_codec' => 'h264'
]);