Completed
Push — master ( b96d25...64cc65 )
by Amin
03:00
created

FFMpeg   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 29
c 1
b 1
f 1
dl 0
loc 115
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromS3() 0 8 1
A __construct() 0 3 1
A open() 0 10 3
A fromURL() 0 9 1
A create() 0 3 1
A isTmp() 0 10 2
A fromGCS() 0 8 1
A __call() 0 3 1
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\Exception\ExceptionInterface;
15
use FFMpeg\FFMpeg as BFFMpeg;
16
use FFMpeg\FFProbe;
17
use Psr\Log\LoggerInterface;
18
use Streaming\Exception\Exception;
19
use Streaming\Exception\InvalidArgumentException;
20
use Streaming\Exception\RuntimeException;
21
22
class FFMpeg
23
{
24
    /** @var BFFMpeg */
25
    protected $ffmpeg;
26
27
    /**
28
     * @param $ffmpeg
29
     */
30
    public function __construct(BFFMpeg $ffmpeg)
31
    {
32
        $this->ffmpeg = $ffmpeg;
33
    }
34
35
    /**
36
     * @param string $path
37
     * @param bool $is_tmp
38
     * @return Media
39
     */
40
    public function open(string $path, bool $is_tmp = false): Media
41
    {
42
        if (!is_file($path)) {
43
            throw new InvalidArgumentException("There is no file in this path: " . $path);
44
        }
45
46
        try{
47
            return new Media($this->ffmpeg->open($path), $path, $is_tmp);
48
        }catch (ExceptionInterface $e){
49
            throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()));
50
        }
51
    }
52
53
    /**
54
     * @param string $url
55
     * @param string|null $save_to
56
     * @param string $method
57
     * @param $request_options
58
     * @return Media
59
     * @throws Exception
60
     */
61
    public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media
62
    {
63
        Helper::isURL($url);
64
        list($is_tmp, $save_to) = $this->isTmp($save_to);
65
66
        $file_manager = new FileManager($url, $method, $request_options);
67
        $file_manager->downloadFile($save_to);
68
69
        return $this->open($save_to, $is_tmp);
70
    }
71
72
    /**
73
     * @param array $config
74
     * @param string $bucket
75
     * @param string $key
76
     * @param string|null $save_to
77
     * @return Media
78
     * @throws Exception
79
     */
80
    public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media
81
    {
82
        list($is_tmp, $save_to) = $this->isTmp($save_to);
83
84
        $aws = new AWS($config);
85
        $aws->downloadFile($bucket, $key, $save_to);
86
87
        return $this->open($save_to, $is_tmp);
88
    }
89
90
91
    public function fromGCS(array $config, string $bucket, string $name, string $save_to = null, $userProject = false): Media
92
    {
93
        list($is_tmp, $save_to) = $this->isTmp($save_to);
94
95
        $google_cloud = new GoogleCloudStorage($config, $bucket, $userProject);
96
        $google_cloud->download($name, $save_to);
97
98
        return $this->open($save_to, $is_tmp);
99
    }
100
101
    /**
102
     * @param $path
103
     * @return array
104
     * @throws Exception
105
     */
106
    private function isTmp($path)
107
    {
108
        $is_tmp = false;
109
110
        if (null === $path) {
111
            $is_tmp = true;
112
            $path = FileManager::tmpFile();
113
        }
114
115
        return [$is_tmp, $path];
116
    }
117
118
    /**
119
     * @param $method
120
     * @param $parameters
121
     * @return mixed
122
     */
123
    public function __call($method, $parameters)
124
    {
125
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
126
    }
127
128
    /**
129
     * @param array $config
130
     * @param LoggerInterface $logger
131
     * @param FFProbe|null $probe
132
     * @return FFMpeg
133
     */
134
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
135
    {
136
        return new static(BFFMpeg::create($config, $logger, $probe));
137
    }
138
}