Skip to content

Commit

Permalink
Bug fixes and other minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aminyazdanpanah committed May 4, 2020
1 parent 5aad620 commit 3a6957f
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/Filters/AudioDASHFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class AudioDASHFilter extends StreamFilter
*/
public function streamFilter(StreamInterface $stream): void
{
// TODO: Implement streamFilter() method.
// @TODO: Implement streamFilter() method.
}
}
2 changes: 1 addition & 1 deletion src/Filters/AudioHLSFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Streaming\StreamInterface;

class AudioHLSFilter extends StreamFilter
class AudioHLSFilter extends FormatFilter
{

/**
Expand Down
29 changes: 29 additions & 0 deletions src/Filters/FormatFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace Streaming\Filters;


use FFMpeg\Format\VideoInterface;
use Streaming\Utiles;

abstract class FormatFilter extends StreamFilter
{
/**
* @param VideoInterface $format
* @return array
*/
protected function getFormatOptions(VideoInterface $format): array
{
$basic = Utiles::arrayToFFmpegOpt([
'c:v' => $format->getVideoCodec(),
'c:a' => $format->getAudioCodec(),
]);

$options = Utiles::arrayToFFmpegOpt(
array_merge($format->getInitialParameters() ?? [], $format->getAdditionalParameters() ?? [])
);

return array_merge($basic, $options);
}
}
58 changes: 22 additions & 36 deletions src/Filters/HLSFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Streaming\Representation;
use Streaming\Utiles;

class HLSFilter extends StreamFilter
class HLSFilter extends FormatFilter
{
/** @var \Streaming\HLS */
private $hls;
Expand All @@ -36,17 +36,6 @@ class HLSFilter extends StreamFilter
/** @var string */
private $seg_filename;

/**
* @return array
*/
private function getFormats(): array
{
$format = ['-c:v', $this->hls->getFormat()->getVideoCodec()];
$audio_format = $this->hls->getFormat()->getAudioCodec();

return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format;
}

/**
* @param Representation $rep
* @param bool $not_last
Expand All @@ -63,28 +52,28 @@ private function playlistPath(Representation $rep, bool $not_last): array
*/
private function getAudioBitrate(Representation $rep): array
{
return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : [];
return $rep->getAudioKiloBitrate() ? ["b:a" => $rep->getAudioKiloBitrate() . "k"] : [];
}

/**
* @return array
*/
private function getBaseURL(): array
{
return $this->base_url ? ["-hls_base_url", $this->base_url] : [];
return $this->base_url ? ["hls_base_url" => $this->base_url] : [];
}

private function flags(): array
{
return !empty($this->hls->getFlags()) ? ["-hls_flags", implode("+", $this->hls->getFlags())] : [];
return !empty($this->hls->getFlags()) ? ["hls_flags" => implode("+", $this->hls->getFlags())] : [];
}

/**
* @return array
*/
private function getKeyInfo(): array
{
return $this->hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $this->hls->getHlsKeyInfoFile()] : [];
return $this->hls->getHlsKeyInfoFile() ? ["hls_key_info_file" => $this->hls->getHlsKeyInfoFile()] : [];
}

/**
Expand Down Expand Up @@ -112,21 +101,22 @@ private function getSegmentFilename(Representation $rep): string
*/
private function initArgs(Representation $rep): array
{
return [
"-s:v", $rep->size2string(),
"-crf", "20",
"-sc_threshold", "0",
"-g", "48",
"-keyint_min", "48",
"-hls_list_size", $this->hls->getHlsListSize(),
"-hls_time", $this->hls->getHlsTime(),
"-hls_allow_cache", (int)$this->hls->isHlsAllowCache(),
"-b:v", $rep->getKiloBitrate() . "k",
"-maxrate", intval($rep->getKiloBitrate() * 1.2) . "k",
"-hls_segment_type", $this->hls->getHlsSegmentType(),
"-hls_fmp4_init_filename", $this->getInitFilename($rep),
"-hls_segment_filename", $this->getSegmentFilename($rep)
$init = [
"hls_list_size" => $this->hls->getHlsListSize(),
"hls_time" => $this->hls->getHlsTime(),
"hls_allow_cache" => (int)$this->hls->isHlsAllowCache(),
"hls_segment_type" => $this->hls->getHlsSegmentType(),
"hls_fmp4_init_filename" => $this->getInitFilename($rep),
"hls_segment_filename" => $this->getSegmentFilename($rep),
"s:v" => $rep->size2string(),
"b:v" => $rep->getKiloBitrate() . "k"
];

return array_merge($init,
$this->getAudioBitrate($rep),
$this->getBaseURL(),
$this->flags(),
$this->getKeyInfo());
}

/**
Expand All @@ -137,12 +127,8 @@ private function getArgs(Representation $rep, bool $not_last): void
{
$this->filter = array_merge(
$this->filter,
$this->getFormats(),
$this->initArgs($rep),
$this->getAudioBitrate($rep),
$this->getBaseURL(),
$this->flags(),
$this->getKeyInfo(),
$this->getFormatOptions($this->hls->getFormat()),
Utiles::arrayToFFmpegOpt($this->initArgs($rep)),
Utiles::arrayToFFmpegOpt($this->hls->getAdditionalParams()),
["-strict", $this->hls->getStrict()],
$this->playlistPath($rep, $not_last)
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/HLSFilterV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Streaming\StreamInterface;

class HLSFilterV2 extends StreamFilter
class HLSFilterV2 extends FormatFilter
{
/**
* This is a new version of HLSFilter that the master playlist will be created by FFmpeg
Expand Down
7 changes: 5 additions & 2 deletions src/Filters/StreamToFileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Streaming\StreamInterface;

class StreamToFileFilter extends StreamFilter
class StreamToFileFilter extends FormatFilter
{

/**
Expand All @@ -24,6 +24,9 @@ class StreamToFileFilter extends StreamFilter
*/
public function streamFilter(StreamInterface $media): void
{
$this->filter = array_merge(['-c', 'copy'], $media->getParams());
$this->filter = array_merge(
$this->getFormatOptions($media->getFormat()),
$media->getParams()
);
}
}
19 changes: 15 additions & 4 deletions src/Format/HEVC.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ final class HEVC extends StreamFormat
* HEVC constructor.
* @param string $video_codec
* @param string|null $audio_codec
* @param bool $default_init_opts
*/
public function __construct(string $video_codec = 'libx265', string $audio_codec = null)
public function __construct(string $video_codec = 'libx265', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setVideoCodec($video_codec);
$this
->setVideoCodec($video_codec)
->setAudioCodec($audio_codec);

if ($audio_codec) {
$this->setAudioCodec($audio_codec);
/**
* set the default value of h265 codec options
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-29 for more information about options
*/
if ($default_init_opts) {
$this->setInitialParameters([
'keyint_min' => 25,
'g' => 250,
'sc_threshold' => 40
]);
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/Format/StreamFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,4 @@ public function setAudioKiloBitrate($kiloBitrate)
{
throw new InvalidArgumentException("You can not set this option, use Representation instead");
}

/**
* @param array $additionalParamaters
* @return DefaultVideo|void
*/
public function setAdditionalParameters($additionalParamaters)
{
throw new InvalidArgumentException("You can not set this option");
}
}
15 changes: 11 additions & 4 deletions src/Format/VP9.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ final class VP9 extends StreamFormat
* VP9 constructor.
* @param string $video_codec
* @param string|null $audio_codec
* @param bool $default_init_opts
*/
public function __construct(string $video_codec = 'libvpx-vp9', string $audio_codec = null)
public function __construct(string $video_codec = 'libvpx-vp9', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setVideoCodec($video_codec);
$this
->setVideoCodec($video_codec)
->setAudioCodec($audio_codec);

if ($audio_codec) {
$this->setAudioCodec($audio_codec);
/**
* set the default value of h265 codec options
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-26 for more information about options
*/
if ($default_init_opts) {
//@TODO: add default vp9
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/Format/X264.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ final class X264 extends StreamFormat
/**
* X264 constructor.
* @param string $video_codec
* @param null $audio_codec
* @param string $audio_codec
* @param bool $default_init_opts
*/
public function __construct($video_codec = 'libx264', $audio_codec = null)
public function __construct($video_codec = 'libx264', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setVideoCodec($video_codec);
$this
->setVideoCodec($video_codec)
->setAudioCodec($audio_codec);

if ($audio_codec) {
$this->setAudioCodec($audio_codec);
/**
* set the default value of h264 codec options
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-28 for more information about options
* return array
*/
if ($default_init_opts) {
$this->setInitialParameters([
'bf' => 1,
'keyint_min' => 25,
'g' => 250,
'sc_threshold' => 40
]);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/StreamInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
namespace Streaming;


use FFMpeg\Format\VideoInterface;

interface StreamInterface
{
/**
* @return Media
*/
public function getMedia(): Media;

/**
* @return VideoInterface
*/
public function getFormat(): VideoInterface;

/**
* @param int $option
* @return string
Expand Down
15 changes: 9 additions & 6 deletions src/Traits/Formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,36 @@ trait Formats
/**
* @param string $video_codec
* @param string|null $audio_codec
* @param bool $default_init_opts
* @return $this
*/
public function x264(string $video_codec = 'libx264', string $audio_codec = null)
public function x264(string $video_codec = 'libx264', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setFormat(new X264($video_codec, $audio_codec));
$this->setFormat(new X264($video_codec, $audio_codec, $default_init_opts));
return $this;
}

/**
* @param string $video_codec
* @param string|null $audio_codec
* @param bool $default_init_opts
* @return $this
*/
public function hevc(string $video_codec = 'libx265', string $audio_codec = null)
public function hevc(string $video_codec = 'libx265', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setFormat(new HEVC($video_codec, $audio_codec));
$this->setFormat(new HEVC($video_codec, $audio_codec, $default_init_opts));
return $this;
}

/**
* @param string $video_codec
* @param string|null $audio_codec
* @param bool $default_init_opts
* @return $this
*/
public function vp9(string $video_codec = 'libvpx-vp9', string $audio_codec = null)
public function vp9(string $video_codec = 'libvpx-vp9', string $audio_codec = 'aac', bool $default_init_opts = true)
{
$this->setFormat(new VP9($video_codec, $audio_codec));
$this->setFormat(new VP9($video_codec, $audio_codec, $default_init_opts));
return $this;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Utiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public static function arrayToFFmpegOpt(array $array, string $start_with = "-"):
{
$new = [];
foreach ($array as $key => $value) {
array_push($new, $start_with . $key, $value);
if(is_string($key)){
array_push($new, $start_with . $key, $value);
}else{
$new = null;
break;
}
}

return $new;
return $new ?? $array;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/DASHFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function testGetApply()

$this->assertEquals(
[
"-bf", "1", "-keyint_min", "120", "-g", "120", "-sc_threshold", "0", "-b_strategy", "0", "-use_timeline",
"1", "-use_template", "1", "-init_seg_name", "test_init_\$RepresentationID$.\$ext$", "-media_seg_name",
"test_chunk_\$RepresentationID\$_\$Number%05d$.\$ext$", "-seg_duration", "10", "-hls_playlist", "0", "-f",
"dash", "-map", "0", "-b:v:0", "103k", "-s:v:0", "256x144", "-map", "0", "-b:v:1", "138k", "-s:v:1",
"426x240", "-map", "0", "-b:v:2", "207k", "-s:v:2", "640x360", "-c:v", "libx265", "-adaptation_sets",
"id=0,streams=v id=1,streams=a", "-strict", "-2"
"-c:v", "libx265", "-c:a", "aac", "-keyint_min", "25", "-g", "250", "-sc_threshold", "40",
"-use_timeline", "1", "-use_template", "1", "-init_seg_name", "test_init_\$RepresentationID$.\$ext$",
"-media_seg_name", "test_chunk_\$RepresentationID\$_\$Number%05d$.\$ext$", "-seg_duration", "10",
"-hls_playlist", "0", "-f", "dash", "-adaptation_sets", "id=0,streams=v id=1,streams=a", "-map", "0",
"-s:v:0", "256x144", "-b:v:0", "103k", "-map", "0", "-s:v:1", "426x240", "-b:v:1", "138k", "-map", "0",
"-s:v:2", "640x360", "-b:v:2", "207k", "-strict", "-2"
],
$apply);
}
Expand Down

0 comments on commit 3a6957f

Please sign in to comment.