Skip to content

Commit 022e95a

Browse files
authored
feat: allow remote URLs as multimedia files (#56)
Co-authored-by: Vladimir Gorej <vladimir.gorej@gmail.com> Closes #55
1 parent 0e81382 commit 022e95a

5 files changed

Lines changed: 96 additions & 4 deletions

File tree

src/OutputProviders/FFMpegProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getOutput()
3434
}
3535

3636
// File doesn't exist
37-
if (!file_exists($this->movieFile)) {
37+
if (@file_get_contents($this->movieFile, false, null, 0, 0) === false) {
3838
throw new \UnexpectedValueException('Movie file not found', self::$EX_CODE_FILE_NOT_FOUND);
3939
}
4040

src/OutputProviders/FFProbeProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getOutput()
3434
}
3535

3636
// File doesn't exist
37-
if (!file_exists($this->movieFile)) {
37+
if (@file_get_contents($this->movieFile, false, null, 0, 0) === false) {
3838
throw new \UnexpectedValueException('Movie file not found', self::$EX_CODE_FILE_NOT_FOUND);
3939
}
4040

tests/MovieTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class MovieTest extends TestCase
1212
{
1313

1414
protected static $moviePath;
15+
16+
protected static $movieUrl;
1517
/**
1618
* @var Movie
1719
*/
@@ -30,6 +32,12 @@ public static function setUpBeforeClass(): void
3032
self::$moviePath = dirname(__FILE__).DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'test.mp4';
3133
self::$audioPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'test.wav';
3234
self::$noMediaPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'test1.txt';
35+
self::$movieUrl = array(
36+
'base' => 'https://github.com/char0n/ffmpeg-php/blob/master/tests/data/',
37+
'fileName' => 'test',
38+
'fileExtension' => '.mp4',
39+
'query' => '?raw=true'
40+
);
3341
}
3442

3543
public static function tearDownAfterClass(): void
@@ -59,6 +67,22 @@ public function testFileDoesNotExistException()
5967
new Movie(uniqid('test', true));
6068
}
6169

70+
public function testFileFromUrlDoesNotExistException()
71+
{
72+
$this->expectException(\UnexpectedValueException::class);
73+
$this->expectExceptionCode(334561);
74+
75+
$nonExistingUrlFile = sprintf(
76+
'%s%s%s%s',
77+
self::$movieUrl['base'],
78+
uniqid('test', true),
79+
self::$movieUrl['fileExtension'],
80+
self::$movieUrl['query']
81+
);
82+
83+
new Movie($nonExistingUrlFile);
84+
}
85+
6286
public function testPersistentResourceSimulation()
6387
{
6488
Timer::start();

tests/OutputProviders/FFMpegProviderTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class FFMpegProviderTest extends TestCase
99
{
1010

1111
protected static $moviePath;
12+
13+
protected static $movieUrl;
1214
/**
1315
* @var FFMpegProvider
1416
*/
@@ -18,6 +20,12 @@ public static function setUpBeforeClass(): void
1820
{
1921
$path = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR;
2022
self::$moviePath = realpath($path.'test.mp4');
23+
self::$movieUrl = array(
24+
'base' => 'https://github.com/char0n/ffmpeg-php/blob/master/tests/data/',
25+
'fileName' => 'test',
26+
'fileExtension' => '.mp4',
27+
'query' => '?raw=true'
28+
);
2129
}
2230

2331
public static function tearDownAfterClass(): void
@@ -42,7 +50,7 @@ public function testGetOutput()
4250
$this->assertEquals(1, preg_match('/FFmpeg version/i', $output));
4351
}
4452

45-
public function testGetOutputFileDoesntExist()
53+
public function testGetOutputFileDoesNotExist()
4654
{
4755
$this->expectException(\UnexpectedValueException::class);
4856
$this->expectExceptionCode(334561);
@@ -52,6 +60,32 @@ public function testGetOutputFileDoesntExist()
5260
$provider->getOutput();
5361
}
5462

63+
public function testGetOutputUrl()
64+
{
65+
$provider = new FFMpegProvider();
66+
$provider->setMovieFile(implode(self::$movieUrl));
67+
$output = $provider->getOutput();
68+
69+
$this->assertEquals(1, preg_match('/FFmpeg version/i', $output));
70+
}
71+
72+
public function testGetOutputUrlFileDoesNotExist()
73+
{
74+
$this->expectException(\UnexpectedValueException::class);
75+
$this->expectExceptionCode(334561);
76+
77+
$provider = new FFMpegProvider();
78+
$nonExistingUrlFile = sprintf(
79+
'%s%s%s%s',
80+
self::$movieUrl['base'],
81+
uniqid('test', true),
82+
self::$movieUrl['fileExtension'],
83+
self::$movieUrl['query']
84+
);
85+
$provider->setMovieFile($nonExistingUrlFile);
86+
$provider->getOutput();
87+
}
88+
5589
public function testPersistentResourceSimulation()
5690
{
5791
Timer::start();

tests/OutputProviders/FFProbeProviderTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class FFProbeProviderTest extends TestCase
99
{
1010

1111
protected static $moviePath;
12+
13+
protected static $movieUrl;
1214
/**
1315
* @var FFProbeProvider
1416
*/
@@ -18,6 +20,12 @@ public static function setUpBeforeClass(): void
1820
{
1921
$path = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR;
2022
self::$moviePath = realpath($path.'test.mp4');
23+
self::$movieUrl = array(
24+
'base' => 'https://github.com/char0n/ffmpeg-php/blob/master/tests/data/',
25+
'fileName' => 'test',
26+
'fileExtension' => '.mp4',
27+
'query' => '?raw=true'
28+
);
2129
}
2230

2331
public static function tearDownAfterClass(): void
@@ -42,7 +50,7 @@ public function testGetOutput()
4250
$this->assertEquals(1, preg_match('/FFprobe version/i', $output));
4351
}
4452

45-
public function testGetOutputFileDoesntExist()
53+
public function testGetOutputFileDoesNotExist()
4654
{
4755
$this->expectException(\UnexpectedValueException::class);
4856
$this->expectExceptionCode(334561);
@@ -52,6 +60,32 @@ public function testGetOutputFileDoesntExist()
5260
$provider->getOutput();
5361
}
5462

63+
public function testGetOutputUrl()
64+
{
65+
$provider = new FFProbeProvider();
66+
$provider->setMovieFile(implode(self::$movieUrl));
67+
$output = $provider->getOutput();
68+
69+
$this->assertEquals(1, preg_match('/FFprobe version/i', $output));
70+
}
71+
72+
public function testGetOutputUrlFileDoesNotExist()
73+
{
74+
$this->expectException(\UnexpectedValueException::class);
75+
$this->expectExceptionCode(334561);
76+
77+
$provider = new FFProbeProvider();
78+
$nonExistingUrlFile = sprintf(
79+
'%s%s%s%s',
80+
self::$movieUrl['base'],
81+
uniqid('test', true),
82+
self::$movieUrl['fileExtension'],
83+
self::$movieUrl['query']
84+
);
85+
$provider->setMovieFile($nonExistingUrlFile);
86+
$provider->getOutput();
87+
}
88+
5589
public function testPersistentResourceSimulation()
5690
{
5791
Timer::start();

0 commit comments

Comments
 (0)