Skip to content

Commit 3a6957f

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

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

src/StreamInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313
namespace Streaming;
1414

1515

16+
use FFMpeg\Format\VideoInterface;
17+
1618
interface StreamInterface
1719
{
1820
/**
1921
* @return Media
2022
*/
2123
public function getMedia(): Media;
2224

25+
/**
26+
* @return VideoInterface
27+
*/
28+
public function getFormat(): VideoInterface;
29+
2330
/**
2431
* @param int $option
2532
* @return string

src/Traits/Formats.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,36 @@ trait Formats
2626
/**
2727
* @param string $video_codec
2828
* @param string|null $audio_codec
29+
* @param bool $default_init_opts
2930
* @return $this
3031
*/
31-
public function x264(string $video_codec = 'libx264', string $audio_codec = null)
32+
public function x264(string $video_codec = 'libx264', string $audio_codec = 'aac', bool $default_init_opts = true)
3233
{
33-
$this->setFormat(new X264($video_codec, $audio_codec));
34+
$this->setFormat(new X264($video_codec, $audio_codec, $default_init_opts));
3435
return $this;
3536
}
3637

3738
/**
3839
* @param string $video_codec
3940
* @param string|null $audio_codec
41+
* @param bool $default_init_opts
4042
* @return $this
4143
*/
42-
public function hevc(string $video_codec = 'libx265', string $audio_codec = null)
44+
public function hevc(string $video_codec = 'libx265', string $audio_codec = 'aac', bool $default_init_opts = true)
4345
{
44-
$this->setFormat(new HEVC($video_codec, $audio_codec));
46+
$this->setFormat(new HEVC($video_codec, $audio_codec, $default_init_opts));
4547
return $this;
4648
}
4749

4850
/**
4951
* @param string $video_codec
5052
* @param string|null $audio_codec
53+
* @param bool $default_init_opts
5154
* @return $this
5255
*/
53-
public function vp9(string $video_codec = 'libvpx-vp9', string $audio_codec = null)
56+
public function vp9(string $video_codec = 'libvpx-vp9', string $audio_codec = 'aac', bool $default_init_opts = true)
5457
{
55-
$this->setFormat(new VP9($video_codec, $audio_codec));
58+
$this->setFormat(new VP9($video_codec, $audio_codec, $default_init_opts));
5659
return $this;
5760
}
5861

src/Utiles.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ public static function arrayToFFmpegOpt(array $array, string $start_with = "-"):
4444
{
4545
$new = [];
4646
foreach ($array as $key => $value) {
47-
array_push($new, $start_with . $key, $value);
47+
if(is_string($key)){
48+
array_push($new, $start_with . $key, $value);
49+
}else{
50+
$new = null;
51+
break;
52+
}
4853
}
4954

50-
return $new;
55+
return $new ?? $array;
5156
}
5257

5358
/**

tests/DASHFiltersTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public function testGetApply()
3030

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

0 commit comments

Comments
 (0)