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
|
|
|
} |