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'
]);

$filter->setParameters([
	'video_codec' => 'libx264'
]);

// ArrayAccess
$filter['video_codec'] = 'x264';

use Arhitector\Transcoder\Filter\Cut;

public Cut::__construct(TimeInterval|int $start [, TimeInterval $duration = null])

$filter = new Cut(new TimeInterval(20), new TimeInterval(60));

use \Arhitector\Transcoder\Filter\Volume;

public Volume::__construct(float $volume [, string $precision = null])

$filter = new Volume(0.5);
$filter = new Volume(1/2);
$filter = new Volume('6.0206dB');

$filter = new Volume('6dB', Volume::PRECISION_FIXED);

use \Arhitector\Transcoder\Filter\Fade;

public Fade::__construct(TimeInterval|int $startTime = 0 [, TimeInterval|int $duration = null [, string $effectType = null]])

new Fade(2, 10, Fade::FADE_OUT)

use \Arhitector\Transcoder\Filter\AudioDelay;

use \Arhitector\Transcoder\Filter\Rotate;

public Rotate::__construct($angle = null)

use \Arhitector\Transcoder\Filter\Crop;

public Crop::__construct(Point $start, Dimension $dimension)