Passed
Push — master ( 8e077c...e6cc46 )
by Amin
03:50
created

FFMpeg::fromURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 11
rs 10
c 1
b 1
f 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\Clouds\CloudManager;
19
use Streaming\Exception\Exception;
20
use Streaming\Exception\InvalidArgumentException;
21
use Streaming\Exception\RuntimeException;
22
23
class FFMpeg
24
{
25
    /** @var BFFMpeg */
26
    protected $ffmpeg;
27
28
    /**
29
     * @param $ffmpeg
30
     */
31
    public function __construct(BFFMpeg $ffmpeg)
32
    {
33
        $this->ffmpeg = $ffmpeg;
34
    }
35
36
    /**
37
     * @param string $path
38
     * @param bool $is_tmp
39
     * @return Media
40
     */
41
    public function open(string $path, bool $is_tmp = false): Media
42
    {
43
        if (!is_file($path)) {
44
            throw new InvalidArgumentException("There is no file in this path: " . $path);
45
        }
46
47
        try {
48
            return new Media($this->ffmpeg->open($path), $path, $is_tmp);
49
        } catch (ExceptionInterface $e) {
50
            if ($is_tmp) {
51
                sleep(1);
52
                unlink($path);
53
            }
54
55
            throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e);
56
        }
57
    }
58
59
    /**
60
     * @param array $cloud
61
     * @param string|null $save_to
62
     * @return Media
63
     * @throws Exception
64
     */
65
    public function openFromCloud(array $cloud, string $save_to = null): Media
66
    {
67
        return call_user_func_array([$this, 'open'], CloudManager::download($cloud, $save_to));
68
    }
69
70
    /**
71
     * @param $method
72
     * @param $parameters
73
     * @return mixed
74
     */
75
    public function __call($method, $parameters)
76
    {
77
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
78
    }
79
80
    /**
81
     * @param array $config
82
     * @param LoggerInterface $logger
83
     * @param FFProbe|null $probe
84
     * @return FFMpeg
85
     */
86
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
87
    {
88
        return new static(BFFMpeg::create($config, $logger, $probe));
89
    }
90
}