Passed
Push — master ( 17b30e...88ad78 )
by Amin
03:09
created

FFMpeg::fromS3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 8
rs 10
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
namespace Streaming;
13
14
use FFMpeg\FFMpeg as BFFMpeg;
15
use FFMpeg\FFProbe;
16
use Psr\Log\LoggerInterface;
17
use Streaming\Exception\Exception;
18
19
class FFMpeg
20
{
21
    /** @var BFFMpeg */
22
    protected $ffmpeg;
23
24
    /**
25
     * @param $ffmpeg
26
     */
27
    public function __construct(BFFMpeg $ffmpeg)
28
    {
29
        $this->ffmpeg = $ffmpeg;
30
    }
31
32
    /**
33
     * @param string $path
34
     * @param bool $is_tmp
35
     * @return Media
36
     * @throws Exception
37
     */
38
    public function open(string $path, bool $is_tmp = false): Media
39
    {
40
        if (!is_file($path)) {
41
            throw new Exception("There is no file in this path: " . $path);
42
        }
43
44
        return new Media($this->ffmpeg->open($path), $path, $is_tmp);
45
    }
46
47
    /**
48
     * @param string $url
49
     * @param string|null $save_to
50
     * @param string $method
51
     * @param $request_options
52
     * @return Media
53
     * @throws Exception
54
     */
55
    public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media
56
    {
57
        Helper::isURL($url);
58
        list($is_tmp, $save_to) = $this->isTmp($save_to);
59
60
        $file_manager = new FileManager($url, $method, $request_options);
61
        $file_manager->downloadFile($save_to);
62
63
        return $this->open($save_to, $is_tmp);
64
    }
65
66
    /**
67
     * @param array $config
68
     * @param string $bucket
69
     * @param string $key
70
     * @param string|null $save_to
71
     * @return Media
72
     * @throws Exception
73
     */
74
    public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media
75
    {
76
        list($is_tmp, $save_to) = $this->isTmp($save_to);
77
78
        $aws = new AWS($config);
79
        $aws->downloadFile($bucket, $key, $save_to);
80
81
        return $this->open($save_to, $is_tmp);
82
    }
83
84
    /**
85
     * @param $path
86
     * @return array
87
     * @throws Exception
88
     */
89
    private function isTmp($path)
90
    {
91
        $is_tmp = false;
92
93
        if (null === $path) {
94
            $is_tmp = true;
95
            $path = FileManager::tmpFile();
96
        }
97
98
        return [$is_tmp, $path];
99
    }
100
101
    /**
102
     * @param $method
103
     * @param $parameters
104
     * @return mixed
105
     */
106
    public function __call($method, $parameters)
107
    {
108
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
109
    }
110
111
    /**
112
     * @param array $config
113
     * @param LoggerInterface $logger
114
     * @param FFProbe|null $probe
115
     * @return FFMpeg
116
     */
117
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
118
    {
119
        return new static(BFFMpeg::create($config, $logger, $probe));
120
    }
121
}