| 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 FFMpeg\Media\Video; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Streaming\Clouds\Cloud; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Streaming\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  | /** @mixin BFFMpeg*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class FFMpeg | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** @var BFFMpeg */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private $ffmpeg; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param $ffmpeg | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(BFFMpeg $ffmpeg) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->ffmpeg = $ffmpeg; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param bool $is_tmp | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @return Media | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function open(string $path, bool $is_tmp = false): Media | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return new Media($this->ffmpeg->open($path), $is_tmp); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } catch (ExceptionInterface $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             if ($is_tmp) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                 sleep(.5); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 File::remove($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             throw new RuntimeException("An error occurred while opening the file: " . $e->getMessage(), $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param array $cloud | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string|null $save_to | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @return Media | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function openFromCloud(array $cloud, string $save_to = null): Media | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return call_user_func_array([$this, 'open'], Cloud::download($cloud, $save_to)); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $video | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @param string|null $audio | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param bool $screen | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @return Media | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     public function capture(string $video, string $audio = null, array $options = [], $screen = false): Media | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         list($path, $option) = (new Capture($video, $audio, $screen))->getOptions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $this->customInput($path, array_merge($option, $options)); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @return Media | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function customInput(string $path, array $options = []): Media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |         return new Media(new Video($path, $this->getFFMpegDriver(), $this->getFFProbe()), false, $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param $method | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     public function __call($method, $parameters) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         return call_user_func_array([$this->ffmpeg, $method], $parameters); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @param LoggerInterface $logger | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param FFProbe|null $probe | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @return FFMpeg | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public static function create(array $config = [], LoggerInterface $logger = null, FFProbe $probe = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |         return new static(BFFMpeg::create($config, $logger, $probe)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 108 |  |  | } |