Skip to content

Commit 3a6957f

Browse files
Bug fixes and other minor improvements
1 parent 5aad620 commit 3a6957f

File tree

14 files changed

+132
-77
lines changed

14 files changed

+132
-77
lines changed

src/Filters/AudioDASHFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ class AudioDASHFilter extends StreamFilter
2424
*/
2525
public function streamFilter(StreamInterface $stream): void
2626
{
27-
// TODO: Implement streamFilter() method.
27+
// @TODO: Implement streamFilter() method.
2828
}
2929
}

src/Filters/AudioHLSFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Streaming\StreamInterface;
1717

18-
class AudioHLSFilter extends StreamFilter
18+
class AudioHLSFilter extends FormatFilter
1919
{
2020

2121
/**

src/Filters/FormatFilter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
4+
namespace Streaming\Filters;
5+
6+
7+
use FFMpeg\Format\VideoInterface;
8+
use Streaming\Utiles;
9+
10+
abstract class FormatFilter extends StreamFilter
11+
{
12+
/**
13+
* @param VideoInterface $format
14+
* @return array
15+
*/
16+
protected function getFormatOptions(VideoInterface $format): array
17+
{
18+
$basic = Utiles::arrayToFFmpegOpt([
19+
'c:v' => $format->getVideoCodec(),
20+
'c:a' => $format->getAudioCodec(),
21+
]);
22+
23+
$options = Utiles::arrayToFFmpegOpt(
24+
array_merge($format->getInitialParameters() ?? [], $format->getAdditionalParameters() ?? [])
25+
);
26+
27+
return array_merge($basic, $options);
28+
}
29+
}

src/Filters/HLSFilter.php

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Streaming\Representation;
1717
use Streaming\Utiles;
1818

19-
class HLSFilter extends StreamFilter
19+
class HLSFilter extends FormatFilter
2020
{
2121
/** @var \Streaming\HLS */
2222
private $hls;
@@ -36,17 +36,6 @@ class HLSFilter extends StreamFilter
3636
/** @var string */
3737
private $seg_filename;
3838

39-
/**
40-
* @return array
41-
*/
42-
private function getFormats(): array
43-
{
44-
$format = ['-c:v', $this->hls->getFormat()->getVideoCodec()];
45-
$audio_format = $this->hls->getFormat()->getAudioCodec();
46-
47-
return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format;
48-
}
49-
5039
/**
5140
* @param Representation $rep
5241
* @param bool $not_last
@@ -63,28 +52,28 @@ private function playlistPath(Representation $rep, bool $not_last): array
6352
*/
6453
private function getAudioBitrate(Representation $rep): array
6554
{
66-
return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : [];
55+
return $rep->getAudioKiloBitrate() ? ["b:a" => $rep->getAudioKiloBitrate() . "k"] : [];
6756
}
6857

6958
/**
7059
* @return array
7160
*/
7261
private function getBaseURL(): array
7362
{
74-
return $this->base_url ? ["-hls_base_url", $this->base_url] : [];
63+
return $this->base_url ? ["hls_base_url" => $this->base_url] : [];
7564
}
7665

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

8271
/**
8372
* @return array
8473
*/
8574
private function getKeyInfo(): array
8675
{
87-
return $this->hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $this->hls->getHlsKeyInfoFile()] : [];
76+
return $this->hls->getHlsKeyInfoFile() ? ["hls_key_info_file" => $this->hls->getHlsKeyInfoFile()] : [];
8877
}
8978

9079
/**
@@ -112,21 +101,22 @@ private function getSegmentFilename(Representation $rep): string
112101
*/
113102
private function initArgs(Representation $rep): array
114103
{
115-
return [
116-
"-s:v", $rep->size2string(),
117-
"-crf", "20",
118-
"-sc_threshold", "0",
119-
"-g", "48",
120-
"-keyint_min", "48",
121-
"-hls_list_size", $this->hls->getHlsListSize(),
122-
"-hls_time", $this->hls->getHlsTime(),
123-
"-hls_allow_cache", (int)$this->hls->isHlsAllowCache(),
124-
"-b:v", $rep->getKiloBitrate() . "k",
125-
"-maxrate", intval($rep->getKiloBitrate() * 1.2) . "k",
126-
"-hls_segment_type", $this->hls->getHlsSegmentType(),
127-
"-hls_fmp4_init_filename", $this->getInitFilename($rep),
128-
"-hls_segment_filename", $this->getSegmentFilename($rep)
104+
$init = [
105+
"hls_list_size" => $this->hls->getHlsListSize(),
106+
"hls_time" => $this->hls->getHlsTime(),
107+
"hls_allow_cache" => (int)$this->hls->isHlsAllowCache(),
108+
"hls_segment_type" => $this->hls->getHlsSegmentType(),
109+
"hls_fmp4_init_filename" => $this->getInitFilename($rep),
110+
"hls_segment_filename" => $this->getSegmentFilename($rep),
111+
"s:v" => $rep->size2string(),
112+
"b:v" => $rep->getKiloBitrate() . "k"
129113
];
114+
115+
return array_merge($init,
116+
$this->getAudioBitrate($rep),
117+
$this->getBaseURL(),
118+
$this->flags(),
119+
$this->getKeyInfo());
130120
}
131121

132122
/**
@@ -137,12 +127,8 @@ private function getArgs(Representation $rep, bool $not_last): void
137127
{
138128
$this->filter = array_merge(
139129
$this->filter,
140-
$this->getFormats(),
141-
$this->initArgs($rep),
142-
$this->getAudioBitrate($rep),
143-
$this->getBaseURL(),
144-
$this->flags(),
145-
$this->getKeyInfo(),
130+
$this->getFormatOptions($this->hls->getFormat()),
131+
Utiles::arrayToFFmpegOpt($this->initArgs($rep)),
146132
Utiles::arrayToFFmpegOpt($this->hls->getAdditionalParams()),
147133
["-strict", $this->hls->getStrict()],
148134
$this->playlistPath($rep, $not_last)

src/Filters/HLSFilterV2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Streaming\StreamInterface;
1515

16-
class HLSFilterV2 extends StreamFilter
16+
class HLSFilterV2 extends FormatFilter
1717
{
1818
/**
1919
* This is a new version of HLSFilter that the master playlist will be created by FFmpeg

src/Filters/StreamToFileFilter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Streaming\StreamInterface;
1717

18-
class StreamToFileFilter extends StreamFilter
18+
class StreamToFileFilter extends FormatFilter
1919
{
2020

2121
/**
@@ -24,6 +24,9 @@ class StreamToFileFilter extends StreamFilter
2424
*/
2525
public function streamFilter(StreamInterface $media): void
2626
{
27-
$this->filter = array_merge(['-c', 'copy'], $media->getParams());
27+
$this->filter = array_merge(
28+
$this->getFormatOptions($media->getFormat()),
29+
$media->getParams()
30+
);
2831
}
2932
}

src/Format/HEVC.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,24 @@ final class HEVC extends StreamFormat
1919
* HEVC constructor.
2020
* @param string $video_codec
2121
* @param string|null $audio_codec
22+
* @param bool $default_init_opts
2223
*/
23-
public function __construct(string $video_codec = 'libx265', string $audio_codec = null)
24+
public function __construct(string $video_codec = 'libx265', string $audio_codec = 'aac', bool $default_init_opts = true)
2425
{
25-
$this->setVideoCodec($video_codec);
26+
$this
27+
->setVideoCodec($video_codec)
28+
->setAudioCodec($audio_codec);
2629

27-
if ($audio_codec) {
28-
$this->setAudioCodec($audio_codec);
30+
/**
31+
* set the default value of h265 codec options
32+
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-29 for more information about options
33+
*/
34+
if ($default_init_opts) {
35+
$this->setInitialParameters([
36+
'keyint_min' => 25,
37+
'g' => 250,
38+
'sc_threshold' => 40
39+
]);
2940
}
3041
}
3142

src/Format/StreamFormat.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,4 @@ public function setAudioKiloBitrate($kiloBitrate)
3535
{
3636
throw new InvalidArgumentException("You can not set this option, use Representation instead");
3737
}
38-
39-
/**
40-
* @param array $additionalParamaters
41-
* @return DefaultVideo|void
42-
*/
43-
public function setAdditionalParameters($additionalParamaters)
44-
{
45-
throw new InvalidArgumentException("You can not set this option");
46-
}
4738
}

src/Format/VP9.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ final class VP9 extends StreamFormat
2222
* VP9 constructor.
2323
* @param string $video_codec
2424
* @param string|null $audio_codec
25+
* @param bool $default_init_opts
2526
*/
26-
public function __construct(string $video_codec = 'libvpx-vp9', string $audio_codec = null)
27+
public function __construct(string $video_codec = 'libvpx-vp9', string $audio_codec = 'aac', bool $default_init_opts = true)
2728
{
28-
$this->setVideoCodec($video_codec);
29+
$this
30+
->setVideoCodec($video_codec)
31+
->setAudioCodec($audio_codec);
2932

30-
if ($audio_codec) {
31-
$this->setAudioCodec($audio_codec);
33+
/**
34+
* set the default value of h265 codec options
35+
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-26 for more information about options
36+
*/
37+
if ($default_init_opts) {
38+
//@TODO: add default vp9
3239
}
3340
}
3441

src/Format/X264.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,27 @@ final class X264 extends StreamFormat
1818
/**
1919
* X264 constructor.
2020
* @param string $video_codec
21-
* @param null $audio_codec
21+
* @param string $audio_codec
22+
* @param bool $default_init_opts
2223
*/
23-
public function __construct($video_codec = 'libx264', $audio_codec = null)
24+
public function __construct($video_codec = 'libx264', string $audio_codec = 'aac', bool $default_init_opts = true)
2425
{
25-
$this->setVideoCodec($video_codec);
26+
$this
27+
->setVideoCodec($video_codec)
28+
->setAudioCodec($audio_codec);
2629

27-
if ($audio_codec) {
28-
$this->setAudioCodec($audio_codec);
30+
/**
31+
* set the default value of h264 codec options
32+
* see https://ffmpeg.org/ffmpeg-codecs.html#Options-28 for more information about options
33+
* return array
34+
*/
35+
if ($default_init_opts) {
36+
$this->setInitialParameters([
37+
'bf' => 1,
38+
'keyint_min' => 25,
39+
'g' => 250,
40+
'sc_threshold' => 40
41+
]);
2942
}
3043
}
3144

0 commit comments

Comments
 (0)