Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9764e6b

Browse files
committedDec 5, 2019
Added option to set path for FFprobe adapter
1 parent ac70db3 commit 9764e6b

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed
 

‎lib/PHPExif/Adapter/FFprobe.php

+54-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHPExif\Adapter;
1313

1414
use PHPExif\Exif;
15+
use InvalidArgumentException;
1516
use FFMpeg;
1617

1718
/**
@@ -25,12 +26,62 @@
2526
class FFprobe extends AdapterAbstract
2627
{
2728

29+
const TOOL_NAME = 'ffprobe';
30+
31+
/**
32+
* Path to the exiftool binary
33+
*
34+
* @var string
35+
*/
36+
protected $toolPath;
37+
2838
/**
2939
* @var string
3040
*/
3141
protected $mapperClass = '\\PHPExif\\Mapper\\FFprobe';
3242

3343

44+
/**
45+
* Setter for the exiftool binary path
46+
*
47+
* @param string $path The path to the exiftool binary
48+
* @return \PHPExif\Adapter\FFprobe Current instance
49+
* @throws \InvalidArgumentException When path is invalid
50+
*/
51+
public function setToolPath($path)
52+
{
53+
if (!file_exists($path)) {
54+
throw new InvalidArgumentException(
55+
sprintf(
56+
'Given path (%1$s) to the ffprobe binary is invalid',
57+
$path
58+
)
59+
);
60+
}
61+
62+
$this->toolPath = $path;
63+
64+
return $this;
65+
}
66+
67+
68+
69+
/**
70+
* Getter for the ffprobe binary path
71+
* Lazy loads the "default" path
72+
*
73+
* @return string
74+
*/
75+
public function getToolPath()
76+
{
77+
if (empty($this->toolPath)) {
78+
$path = exec('which ' . self::TOOL_NAME);
79+
$this->setToolPath($path);
80+
}
81+
82+
return $this->toolPath;
83+
}
84+
3485
/**
3586
* Reads & parses the EXIF data from given file
3687
*
@@ -46,8 +97,10 @@ public function getExifFromFile($file)
4697
return false;
4798
}
4899

100+
$ffprobe = FFMpeg\FFProbe::create(array(
101+
'ffprobe.binaries' => $this->getToolPath(),
102+
));
49103

50-
$ffprobe = FFMpeg\FFProbe::create();
51104

52105
$stream = $ffprobe->streams($file)->videos()->first()->all();
53106
$format = $ffprobe->format($file)->all();

‎lib/PHPExif/Mapper/Exiftool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Exiftool implements MapperInterface
5555
const GPSLONGITUDE = 'GPS:GPSLongitude';
5656
const GPSALTITUDE = 'GPS:GPSAltitude';
5757
const IMGDIRECTION = 'GPS:GPSImgDirection';
58-
const DESCRIPTION = 'IFD0:ImageDescription ';
58+
const DESCRIPTION = 'ExifIFD:ImageDescription ';
5959
const MAKE = 'IFD0:Make';
6060
const LENS = 'ExifIFD:LensModel';
6161
const SUBJECT = 'XMP-dc:Subject';

‎tests/PHPExif/Adapter/FFprobeTest.php

+51-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,57 @@ public function setUp(): void
1414
$this->adapter = new \PHPExif\Adapter\FFprobe();
1515
}
1616

17+
18+
/**
19+
* @group ffprobe
20+
* @covers \PHPExif\Adapter\FFprobe::getToolPath
21+
*/
22+
public function testGetToolPathFromProperty()
23+
{
24+
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\FFprobe', 'toolPath');
25+
$reflProperty->setAccessible(true);
26+
$expected = '/foo/bar/baz';
27+
$reflProperty->setValue($this->adapter, $expected);
28+
29+
$this->assertEquals($expected, $this->adapter->getToolPath());
30+
}
31+
32+
/**
33+
* @group ffprobe
34+
* @covers \PHPExif\Adapter\FFprobe::setToolPath
35+
*/
36+
public function testSetToolPathInProperty()
37+
{
38+
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\FFprobe', 'toolPath');
39+
$reflProperty->setAccessible(true);
40+
41+
$expected = '/tmp';
42+
$this->adapter->setToolPath($expected);
43+
44+
$this->assertEquals($expected, $reflProperty->getValue($this->adapter));
45+
}
46+
47+
/**
48+
* @group ffprobe
49+
* @covers \PHPExif\Adapter\FFprobe::setToolPath
50+
*/
51+
public function testSetToolPathThrowsException()
52+
{
53+
$this->expectException('InvalidArgumentException');
54+
$this->adapter->setToolPath('/foo/bar');
55+
}
56+
57+
/**
58+
* @group ffprobe
59+
* @covers \PHPExif\Adapter\FFprobe::getToolPath
60+
*/
61+
public function testGetToolPathLazyLoadsPath()
62+
{
63+
$this->assertIsString($this->adapter->getToolPath());
64+
}
65+
1766
/**
18-
* @group native
67+
* @group ffprobe
1968
* @covers \PHPExif\Adapter\FFprobe::getExifFromFile
2069
*/
2170
public function testGetExifFromFileHasData()
@@ -34,7 +83,7 @@ public function testGetExifFromFileHasData()
3483
}
3584

3685
/**
37-
* @group native
86+
* @group ffprobe
3887
* @covers \PHPExif\Adapter\FFprobe::getExifFromFile
3988
*/
4089
public function testErrorImageUsed()

0 commit comments

Comments
 (0)
Please sign in to comment.