Skip to content

Commit

Permalink
Add ffmpeg example
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Aug 22, 2023
1 parent f587b63 commit 50b18c8
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/ffmpeg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Amp\Pipeline\Pipeline;
use Amp\Process\Process;
use function Amp\ByteStream\getStdout;

require dirname(__DIR__) . "/vendor/autoload.php";

$ffmpeg = getenv('FFMPEG_BIN') ?: 'ffmpeg';
$concurrency = $argv[1] ?? 3;
$start = microtime(true);

Pipeline::fromIterable(new DirectoryIterator('.'))
->concurrent(3)
->filter(fn ($item) => $item->getExtension() === 'php')
->map(fn ($item) => createVideoClip($ffmpeg, $item->getPathname(), getTempDestination()))
->forEach(fn ($result) => getStdout()->write('Successfully created clip from ' . $result[0] . ' => ' . $result[1] . PHP_EOL));

$end = microtime(true);
echo 'Directory processed in ' . round($end - $start, 1) . ' seconds' . PHP_EOL;

function getTempDestination(): string
{
$destination = tempnam(sys_get_temp_dir(), 'video');
unlink($destination);
$dir = dirname($destination);
$file = basename($destination, '.tmp');

return $dir . DIRECTORY_SEPARATOR . $file . '.mp4';
}

function createVideoClip(string $ffmpeg, string $source, string $destination): array
{
$cmd = sprintf('%s -threads 1 -i %s -t 30 -crf 26 -c:v h264 -c:a ac3 %s', $ffmpeg, $source, $destination);

$success = Process::start($cmd)->join() === 0;

if ($success) {
return [$source, $destination];
} else {
throw new \RuntimeException('Unable to perform conversion');
}
}

0 comments on commit 50b18c8

Please sign in to comment.