1414use FFMpeg \FFMpeg as BFFMpeg ;
1515use FFMpeg \FFProbe ;
1616use Psr \Log \LoggerInterface ;
17+ use Streaming \Exception \Exception ;
18+ use Symfony \Component \Filesystem \Exception \IOExceptionInterface ;
19+ use Symfony \Component \Filesystem \Filesystem ;
1720
1821class FFMpeg
1922{
@@ -29,12 +32,49 @@ public function __construct(BFFMpeg $ffmpeg)
2932 }
3033
3134 /**
32- * @param $path
35+ * @param string $path
36+ * @param bool $is_tmp
3337 * @return Media
38+ * @throws Exception
3439 */
35- public function open ($ path ): Media
40+ public function open (string $ path, bool $ is_tmp = false ): Media
3641 {
37- return new Media ($ this ->ffmpeg ->open ($ path ), $ path );
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 );
3878 }
3979
4080 /**
@@ -57,4 +97,4 @@ public static function create($config = array(), LoggerInterface $logger = null,
5797 {
5898 return new static (BFFMpeg::create ($ config , $ logger , $ probe ));
5999 }
60- }
100+ }
0 commit comments