Completed
Push — master ( 176514...705d93 )
by Amin
04:53
created

FFMpeg::fromURL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 11
c 1
b 1
f 1
nc 3
nop 4
dl 0
loc 21
rs 9.9
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
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
class FFMpeg
22
{
23
    /** @var BFFMpeg */
24
    protected $ffmpeg;
25
26
    /**
27
     * @param $ffmpeg
28
     */
29
    public function __construct(BFFMpeg $ffmpeg)
30
    {
31
        $this->ffmpeg = $ffmpeg;
32
    }
33
34
    /**
35
     * @param string $path
36
     * @param bool $is_tmp
37
     * @return Media
38
     * @throws Exception
39
     */
40
    public function open(string $path, bool $is_tmp = false): Media
41
    {
42
        if (!is_file($path)) {
43
            throw new Exception("There is no file in this path: " . $path);
44
        }
45
46
        return new Media($this->ffmpeg->open($path), $path, $is_tmp);
47
    }
48
49
    /**
50
     * @param string $url
51
     * @param string|null $save_to
52
     * @param string $method
53
     * @param $request_options
54
     * @return Media
55
     * @throws Exception
56
     */
57
    public function fromURL(string $url, string $save_to = null, string $method = "GET", $request_options = []): Media
58
    {
59
        $is_tmp = false;
60
61
        if (null === $save_to) {
62
            $is_tmp = true;
63
            $tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming";
64
65
            Helper::makeDir($tmp_path);
66
67
            $ext = "";
68
            if (isset(pathinfo($url)["extension"])) {
69
                $ext = "." . substr(explode("?", pathinfo($url)["extension"])[0], 0, 10);
70
            }
71
72
            $save_to = $tmp_path . DIRECTORY_SEPARATOR . Helper::randomString() . $ext;
73
        }
74
75
        Helper::downloadFile($url, $save_to, $method, $request_options);
76
77
        return $this->open($save_to, $is_tmp);
78
    }
79
80
    /**
81
     * @param $method
82
     * @param $parameters
83
     * @return mixed
84
     */
85
    public function __call($method, $parameters)
86
    {
87
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
88
    }
89
90
    /**
91
     * @param array $config
92
     * @param LoggerInterface $logger
93
     * @param FFProbe|null $probe
94
     * @return FFMpeg
95
     */
96
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
97
    {
98
        return new static(BFFMpeg::create($config, $logger, $probe));
99
    }
100
}