mirror of
https://github.com/sigmasternchen/MyTube
synced 2025-03-15 04:48:55 +00:00
fixes strange constant bitrate problem;
turns out: php-ffmpeg is stupid and doesn't support CRF at all because they insist on 2-pass encoding which requires a constant bitrate. morons.
This commit is contained in:
parent
6c9b2928c8
commit
f5a3cf0c81
2 changed files with 153 additions and 5 deletions
134
src/Extension/NoStupidDefaultsVideo.php
Normal file
134
src/Extension/NoStupidDefaultsVideo.php
Normal file
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Extension;
|
||||
|
||||
|
||||
use FFMpeg\Exception\InvalidArgumentException;
|
||||
use FFMpeg\Filters\Audio\SimpleFilter;
|
||||
use FFMpeg\Format\AudioInterface;
|
||||
use FFMpeg\Format\FormatInterface;
|
||||
use FFMpeg\Format\VideoInterface;
|
||||
use FFMpeg\Media\Video;
|
||||
use Neutron\TemporaryFilesystem\Manager as FsManager;
|
||||
|
||||
class NoStupidDefaultsVideo extends Video
|
||||
{
|
||||
|
||||
public function __construct(Video $video)
|
||||
{
|
||||
parent::__construct($video->getPathfile(), $video->getFFMpegDriver(), $video->getFFProbe());
|
||||
}
|
||||
|
||||
protected function buildCommand(FormatInterface $format, $outputPathfile)
|
||||
{
|
||||
$commands = $this->basePartOfCommand($format);
|
||||
|
||||
$filters = clone $this->filters;
|
||||
$filters->add(new SimpleFilter($format->getExtraParams(), 10));
|
||||
|
||||
if ($this->driver->getConfiguration()->has('ffmpeg.threads')) {
|
||||
$filters->add(new SimpleFilter(array('-threads', $this->driver->getConfiguration()->get('ffmpeg.threads'))));
|
||||
}
|
||||
if ($format instanceof VideoInterface) {
|
||||
if (null !== $format->getVideoCodec()) {
|
||||
$filters->add(new SimpleFilter(array('-vcodec', $format->getVideoCodec())));
|
||||
}
|
||||
}
|
||||
if ($format instanceof AudioInterface) {
|
||||
if (null !== $format->getAudioCodec()) {
|
||||
$filters->add(new SimpleFilter(array('-acodec', $format->getAudioCodec())));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filters as $filter) {
|
||||
$commands = array_merge($commands, $filter->apply($this, $format));
|
||||
}
|
||||
|
||||
// If the user passed some additional parameters
|
||||
if ($format instanceof VideoInterface) {
|
||||
if (null !== $format->getAdditionalParameters()) {
|
||||
foreach ($format->getAdditionalParameters() as $additionalParameter) {
|
||||
$commands[] = $additionalParameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge Filters into one command
|
||||
$videoFilterVars = $videoFilterProcesses = array();
|
||||
for ($i = 0; $i < count($commands); $i++) {
|
||||
$command = $commands[$i];
|
||||
if ($command === '-vf') {
|
||||
$commandSplits = explode(";", $commands[$i + 1]);
|
||||
if (count($commandSplits) == 1) {
|
||||
$commandSplit = $commandSplits[0];
|
||||
$command = trim($commandSplit);
|
||||
if (preg_match("/^\[in\](.*?)\[out\]$/is", $command, $match)) {
|
||||
$videoFilterProcesses[] = $match[1];
|
||||
} else {
|
||||
$videoFilterProcesses[] = $command;
|
||||
}
|
||||
} else {
|
||||
foreach ($commandSplits as $commandSplit) {
|
||||
$command = trim($commandSplit);
|
||||
if (preg_match("/^\[[^\]]+\](.*?)\[[^\]]+\]$/is", $command, $match)) {
|
||||
$videoFilterProcesses[] = $match[1];
|
||||
} else {
|
||||
$videoFilterVars[] = $command;
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($commands[$i]);
|
||||
unset($commands[$i + 1]);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$videoFilterCommands = $videoFilterVars;
|
||||
$lastInput = 'in';
|
||||
foreach ($videoFilterProcesses as $i => $process) {
|
||||
$command = '[' . $lastInput . ']';
|
||||
$command .= $process;
|
||||
$lastInput = 'p' . $i;
|
||||
if ($i === (count($videoFilterProcesses) - 1)) {
|
||||
$command .= '[out]';
|
||||
} else {
|
||||
$command .= '[' . $lastInput . ']';
|
||||
}
|
||||
|
||||
$videoFilterCommands[] = $command;
|
||||
}
|
||||
$videoFilterCommand = implode(';', $videoFilterCommands);
|
||||
|
||||
if ($videoFilterCommand) {
|
||||
$commands[] = '-vf';
|
||||
$commands[] = $videoFilterCommand;
|
||||
}
|
||||
|
||||
$this->fs = FsManager::create();
|
||||
$this->fsId = uniqid('ffmpeg-passes');
|
||||
$passPrefix = $this->fs->createTemporaryDirectory(0777, 50, $this->fsId) . '/' . uniqid('pass-');
|
||||
$passes = array();
|
||||
$totalPasses = $format->getPasses();
|
||||
|
||||
if (!$totalPasses) {
|
||||
throw new InvalidArgumentException('Pass number should be a positive value.');
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $totalPasses; $i++) {
|
||||
$pass = $commands;
|
||||
|
||||
if ($totalPasses > 1) {
|
||||
$pass[] = '-pass';
|
||||
$pass[] = $i;
|
||||
$pass[] = '-passlogfile';
|
||||
$pass[] = $passPrefix;
|
||||
}
|
||||
|
||||
$pass[] = $outputPathfile;
|
||||
|
||||
$passes[] = $pass;
|
||||
}
|
||||
|
||||
return $passes;
|
||||
}
|
||||
}
|
|
@ -5,12 +5,14 @@ namespace App\Service;
|
|||
|
||||
use App\Controller\WatchController;
|
||||
use App\Entity\Video;
|
||||
use App\Extension\NoStupidDefaultsVideo;
|
||||
use FFMpeg\Coordinate\Dimension;
|
||||
use FFMpeg\Coordinate\TimeCode;
|
||||
use FFMpeg\FFMpeg;
|
||||
use FFMpeg\FFProbe;
|
||||
use FFMpeg\Filters\Video\ResizeFilter;
|
||||
use FFMpeg\Format\Video\X264;
|
||||
use FFMpeg\Media\Video as FFVideo;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class TranscodingService
|
||||
|
@ -20,25 +22,25 @@ class TranscodingService
|
|||
"height" => 1080,
|
||||
"crf" => 23,
|
||||
"playlistResolution" => "1920x1080",
|
||||
"playlistBandwidth" => "800000",
|
||||
"maxBandwidth" => 6000000,
|
||||
],
|
||||
[
|
||||
"height" => 720,
|
||||
"crf" => 23,
|
||||
"playlistResolution" => "1280x720",
|
||||
"playlistBandwidth" => "1400000",
|
||||
"maxBandwidth" => 3000000,
|
||||
],
|
||||
[
|
||||
"height" => 480,
|
||||
"crf" => 23,
|
||||
"playlistResolution" => "842x480",
|
||||
"playlistBandwidth" => "2800000",
|
||||
"maxBandwidth" => 1100000,
|
||||
],
|
||||
[
|
||||
"height" => 360,
|
||||
"crf" => 23,
|
||||
"playlistResolution" => "640x360",
|
||||
"playlistBandwidth" => "5000000",
|
||||
"maxBandwidth" => 500000,
|
||||
]
|
||||
];
|
||||
|
||||
|
@ -103,17 +105,29 @@ class TranscodingService
|
|||
}
|
||||
|
||||
$ffvideo = $this->ffmpeg->open($this->rawPath($video->getId()));
|
||||
|
||||
if (!$ffvideo instanceof FFVideo) {
|
||||
$video->setState(Video::FAIL);
|
||||
$this->videoService->update($video);
|
||||
return;
|
||||
}
|
||||
|
||||
$ffvideo = new NoStupidDefaultsVideo($ffvideo);
|
||||
|
||||
$ffvideo->filters()->resize(new Dimension(1, $quality["height"]), ResizeFilter::RESIZEMODE_SCALE_WIDTH)->synchronize();
|
||||
|
||||
$format = new X264("aac");
|
||||
$format->setAdditionalParameters([
|
||||
"-crf", $quality["crf"],
|
||||
//"-maxrate", ($quality["maxBandwidth"] / 1000) . "k",
|
||||
//"-bufsize", "6M",
|
||||
"-hls_segment_filename", $this->contentDir($video->getId()) . $quality["height"] . "p/" . WatchController::TS_FILE_FORMAT,
|
||||
"-hls_playlist_type", "vod",
|
||||
"-keyint_min", "48",
|
||||
"-g", "48",
|
||||
"-sc_threshold", "0",
|
||||
]);
|
||||
$format->setPasses(1);
|
||||
$format->on('progress', function ($v, $f, $percentage) use ($i, $total, $video) {
|
||||
$percentage = (($i) * 100.0 + $percentage) / ($total);
|
||||
$video->setTranscodingProgress($percentage);
|
||||
|
@ -129,7 +143,7 @@ class TranscodingService
|
|||
$globalPlaylist .= "#EXT-X-VERSION:3\n";
|
||||
for ($i = $countQuality - $total; $i < $countQuality; $i++) {
|
||||
$quality = self::QUALITY[$i];
|
||||
$globalPlaylist .= "#EXT-X-STREAM-INF:BANDWIDTH=" . $quality["playlistBandwidth"] . ",RESOLUTION=" . $quality["playlistResolution"] . "\n";
|
||||
$globalPlaylist .= "#EXT-X-STREAM-INF:BANDWIDTH=" . $quality["maxBandwidth"] . ",RESOLUTION=" . $quality["playlistResolution"] . "\n";
|
||||
$globalPlaylist .= $quality["height"] . "/playlist\n";
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue