Passed
Push — master ( 2cd313...f3f6f1 )
by Amin
02:53
created

encrypted_hls()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 4
nop 6
dl 0
loc 18
rs 9.8333
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use Streaming\FFMpeg;
13
use FFMpeg\Exception\ExceptionInterface;
14
use Streaming\Format\HEVC;
15
use Streaming\Format\X264;
16
17
if (!function_exists('dash')) {
18
    /**
19
     * Auto generate dash MPD file
20
     *
21
     * @param string $input_path
22
     * @param callable $listener
23
     * @param string|null $save_path
24
     * @return mixed
25
     */
26
    function dash(string $input_path, string $save_path = null, callable $listener = null)
27
    {
28
        $format = new HEVC();
29
30
        if (is_callable($listener)) {
31
            $format->on('progress', $listener);
0 ignored issues
show
Bug introduced by
It seems like $listener can also be of type null; however, parameter $listener of Evenement\EventEmitter::on() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
            $format->on('progress', /** @scrutinizer ignore-type */ $listener);
Loading history...
32
        }
33
34
        try {
35
            return FFMpeg::create()
36
                ->open($input_path)
37
                ->DASH()
38
                ->setFormat($format)
39
                ->autoGenerateRepresentations()
40
                ->setAdaption('id=0,streams=v id=1,streams=a')
41
                ->save($save_path);
42
        } catch (ExceptionInterface $e) {
43
            return "Failed: error: " . $e->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on FFMpeg\Exception\ExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Streaming\Exception\StreamingExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            return "Failed: error: " . $e->/** @scrutinizer ignore-call */ getMessage();
Loading history...
44
        }
45
    }
46
}
47
48
if (!function_exists('hls')) {
49
    /**
50
     * Auto generate HLS M3U8 file
51
     *
52
     * @param string $input_path
53
     * @param string|null $save_path
54
     * @param callable|null $listener
55
     * @param string $hls_key
56
     * @return mixed
57
     */
58
    function hls(string $input_path, string $save_path = null, callable $listener = null, $hls_key = "")
59
    {
60
        $format = new X264();
61
62
        if (is_callable($listener)) {
63
            $format->on('progress', $listener);
0 ignored issues
show
Bug introduced by
It seems like $listener can also be of type null; however, parameter $listener of Evenement\EventEmitter::on() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $format->on('progress', /** @scrutinizer ignore-type */ $listener);
Loading history...
64
        }
65
66
        try {
67
            return FFMpeg::create()
68
                ->open($input_path)
69
                ->HLS()
70
                ->setFormat($format)
71
                ->autoGenerateRepresentations()
72
                ->setHlsKeyInfoFile($hls_key)
73
                ->save($save_path);
74
        } catch (ExceptionInterface $e) {
75
            return "Failed: error: " . $e->getMessage();
76
        }
77
    }
78
}
79
80
if (!function_exists('encrypted_hls')) {
81
    /**
82
     * Auto generate HLS M3U8 file
83
     *
84
     * @param string $input_path
85
     * @param string|null $save_path
86
     * @param callable|null $listener
87
     * @param null $url
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $url is correct as it would always require null to be passed?
Loading history...
88
     * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
89
     * @param string $binary
90
     * @return mixed
91
     */
92
    function encrypted_hls(string $input_path, string $save_path = null, callable $listener = null, $url = null, $path = null, $binary = 'openssl')
93
    {
94
        $format = new X264();
95
96
        if (is_callable($listener)) {
97
            $format->on('progress', $listener);
0 ignored issues
show
Bug introduced by
It seems like $listener can also be of type null; however, parameter $listener of Evenement\EventEmitter::on() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            $format->on('progress', /** @scrutinizer ignore-type */ $listener);
Loading history...
98
        }
99
100
        try {
101
            return FFMpeg::create()
102
                ->open($input_path)
103
                ->HLS()
104
                ->setFormat($format)
105
                ->autoGenerateRepresentations()
106
                ->generateRandomKeyInfo($url, $path, $binary)
107
                ->save($save_path);
108
        } catch (ExceptionInterface $e) {
109
            return "Failed: error: " . $e->getMessage();
110
        }
111
    }
112
}