Skip to content

Commit

Permalink
Specify temporary directory + fix for default ffmpeg-passes* tempor…
Browse files Browse the repository at this point in the history
…ary directory (#855)
  • Loading branch information
pascalbaljet committed Feb 22, 2022
1 parent 368def9 commit bda300b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All Notable changes to the library will be documented in this file

## 1.0.1 - 2022-02-22

- Added configuration key to customize the temporary directory used for passes.
- Fix for the path of the default `ffmpeg-passes*` temporary directory.

## 1.0.0 - 2022-02-09

Upgraded dependencies, integrated the Alchemy Binary library, and dropped support for anything below PHP 8.0
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ $ffmpeg = FFMpeg\FFMpeg::create(array(
), $logger);
```

You may pass a `temporary_directory` key to specify a path for temporary files.

```php
$ffmpeg = FFMpeg\FFMpeg::create(array(
'temporary_directory' => '/var/ffmpeg-tmp'
), $logger);
```

### Manipulate media

`FFMpeg\FFMpeg` creates media based on URIs. URIs could be either a pointer to a
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"php-ffmpeg/extras": "A compilation of common audio & video drivers for PHP-FFMpeg"
},
"require-dev": {
"phpunit/phpunit": "^9.5.10"
"phpunit/phpunit": "^9.5.10",
"mockery/mockery": "^1.5"
},
"autoload": {
"psr-4": {
Expand All @@ -71,4 +72,4 @@
"Tests\\FFMpeg\\": "tests/FFMpeg"
}
}
}
}
13 changes: 13 additions & 0 deletions src/FFMpeg/Media/AbstractMediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FFMpeg\Driver\FFMpegDriver;
use FFMpeg\FFProbe;
use FFMpeg\Filters\FiltersCollection;
use Spatie\TemporaryDirectory\TemporaryDirectory;

abstract class AbstractMediaType implements MediaTypeInterface
{
Expand Down Expand Up @@ -96,6 +97,18 @@ public function getFiltersCollection()
return $this->filters;
}

/**
* Returns a new instance of TemporaryDirectory with the optionally configured directory.
*
* @return \Spatie\TemporaryDirectory\TemporaryDirectory
*/
public function getTemporaryDirectory(): TemporaryDirectory
{
return new TemporaryDirectory(
$this->driver->getConfiguration()->get('temporary_directory') ?: ''
);
}

protected function cleanupTemporaryFile($filename)
{
if (file_exists($filename) && is_writable($filename)) {
Expand Down
2 changes: 1 addition & 1 deletion src/FFMpeg/Media/AbstractVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected function buildCommand(FormatInterface $format, $outputPathfile)
}

$this->fsId = uniqid('ffmpeg-passes');
$this->fs = (new TemporaryDirectory($this->fsId))->create();
$this->fs = $this->getTemporaryDirectory()->name($this->fsId)->create();
$passPrefix = $this->fs->path(uniqid('pass-'));
touch($passPrefix);
$passes = [];
Expand Down
2 changes: 1 addition & 1 deletion src/FFMpeg/Media/Concat.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function saveFromSameCodecs($outputPathfile, $streamCopy = true)
*/

// Create the file which will contain the list of videos
$fs = (new TemporaryDirectory())->create();
$fs = $this->getTemporaryDirectory()->create();
$sourcesFile = $fs->path('ffmpeg.concat');

// Set the content of this file
Expand Down
44 changes: 41 additions & 3 deletions tests/FFMpeg/Unit/Media/AdvancedMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AdvancedMediaTest extends AbstractMediaTestCase
{
public function testGetInputs()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$advancedMedia = new AdvancedMedia([__FILE__, __FILE__], $driver, $ffprobe);
Expand All @@ -17,7 +17,7 @@ public function testGetInputs()

public function testGetInputsCount()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$advancedMedia = new AdvancedMedia([__FILE__, __FILE__], $driver, $ffprobe);
Expand All @@ -26,10 +26,48 @@ public function testGetInputsCount()

public function testFiltersReturnFilters()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$advancedMedia = new AdvancedMedia([__FILE__, __FILE__], $driver, $ffprobe);
$this->assertInstanceOf('FFMpeg\Filters\AdvancedMedia\ComplexFilters', $advancedMedia->filters());
}

public function testGetTemporaryDirectoryWithoutCustomConfiguration()
{
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();
$configuration = $this->getConfigurationMock();

$driver->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));

$configuration->expects($this->once())
->method('get')
->with($this->equalTo('temporary_directory'))
->will($this->returnValue(null));

$advancedMedia = new AdvancedMedia([__FILE__, __FILE__], $driver, $ffprobe);
$this->assertEquals('', $advancedMedia->getTemporaryDirectory()->path());
}

public function testGetTemporaryDirectoryWithCustomConfiguration()
{
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();
$configuration = $this->getConfigurationMock();

$driver->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));

$configuration->expects($this->once())
->method('get')
->with($this->equalTo('temporary_directory'))
->will($this->returnValue('/var/ffmpeg'));

$advancedMedia = new AdvancedMedia([__FILE__, __FILE__], $driver, $ffprobe);
$this->assertEquals('/var/ffmpeg', $advancedMedia->getTemporaryDirectory()->path());
}
}
26 changes: 19 additions & 7 deletions tests/FFMpeg/Unit/Media/ConcatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Tests\FFMpeg\Unit\Media;

use FFMpeg\Media\Concat;
use Mockery;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class ConcatTest extends AbstractMediaTestCase
{
public function testGetSources()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$concat = new Concat([__FILE__, __FILE__], $driver, $ffprobe);
Expand All @@ -18,7 +19,7 @@ public function testGetSources()

public function testFiltersReturnFilters()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$concat = new Concat([__FILE__, __FILE__], $driver, $ffprobe);
Expand All @@ -27,7 +28,7 @@ public function testFiltersReturnFilters()

public function testAddFiltersAddsAFilter()
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$filters = $this->getMockBuilder('FFMpeg\Filters\FiltersCollection')
Expand All @@ -50,13 +51,24 @@ public function testAddFiltersAddsAFilter()
*/
public function testSaveFromSameCodecs($streamCopy, $commands)
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$pathfile = '/target/destination';

array_push($commands, $pathfile);

$configuration = Mockery::mock('Alchemy\BinaryDriver\ConfigurationInterface');

$driver->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));

$configuration->shouldReceive('get')
->once()
->with('temporary_directory')
->andReturnNull();

$driver->expects($this->exactly(1))
->method('command')
->with($this->isType('array'), false, $this->anything())
Expand All @@ -79,7 +91,7 @@ public function testSaveFromSameCodecs($streamCopy, $commands)

public function provideSaveFromSameCodecsOptions()
{
$fs = (new TemporaryDirectory())->create();
$fs = (new TemporaryDirectory())->create();
$tmpFile = $fs->path('ffmpeg-concat');
touch($tmpFile);

Expand Down Expand Up @@ -109,9 +121,9 @@ public function provideSaveFromSameCodecsOptions()
*/
public function testSaveFromDifferentCodecs($commands)
{
$driver = $this->getFFMpegDriverMock();
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();
$format = $this->getFormatInterfaceMock();
$format = $this->getFormatInterfaceMock();

$pathfile = '/target/destination';

Expand Down
49 changes: 28 additions & 21 deletions tests/FFMpeg/Unit/Media/VideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FFMpeg\Exception\RuntimeException;
use FFMpeg\Format\Video\X264;
use FFMpeg\Media\Video;
use Mockery;

class VideoTest extends AbstractStreamableTestCase
{
Expand Down Expand Up @@ -158,25 +159,27 @@ public function testSaveShouldSave($threads, $expectedCommands, $expectedListene
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$configuration = $this->getMockBuilder('Alchemy\BinaryDriver\ConfigurationInterface')->getMock();
$configuration = Mockery::mock('Alchemy\BinaryDriver\ConfigurationInterface');

$driver->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));

$configuration->expects($this->once())
->method('has')
->with($this->equalTo('ffmpeg.threads'))
->will($this->returnValue($threads));
$configuration->shouldReceive('has')
->once()
->with('ffmpeg.threads')
->andReturn($threads);

$configuration->shouldReceive('get')
->once()
->with('temporary_directory')
->andReturnNull();

if ($threads) {
$configuration->expects($this->once())
->method('get')
->with($this->equalTo('ffmpeg.threads'))
->will($this->returnValue(24));
} else {
$configuration->expects($this->never())
->method('get');
$configuration->shouldReceive('get')
->once()
->with('ffmpeg.threads')
->andReturn(24);
}

$capturedCommands = [];
Expand Down Expand Up @@ -585,21 +588,25 @@ public function testSaveShouldNotStoreCodecFiltersInTheMedia()
$driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock();

$configuration = $this->getMockBuilder('Alchemy\BinaryDriver\ConfigurationInterface')->getMock();
$configuration = Mockery::mock('Alchemy\BinaryDriver\ConfigurationInterface');

$driver->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));

$configuration->expects($this->any())
->method('has')
->with($this->equalTo('ffmpeg.threads'))
->will($this->returnValue(true));
$configuration->shouldReceive('has')
->with('ffmpeg.threads')
->andReturn(true);

$configuration->shouldReceive('get')
->once()
->with('ffmpeg.threads')
->andReturn(24);

$configuration->expects($this->any())
->method('get')
->with($this->equalTo('ffmpeg.threads'))
->will($this->returnValue(24));
$configuration->shouldReceive('get')
->once()
->with('temporary_directory')
->andReturnNull();

$capturedCommands = [];

Expand Down

0 comments on commit bda300b

Please sign in to comment.