Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FFmpeg adapter, fix travis and add fields #77

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ phpunit.xml
tests/log
vendor
composer.phar
.DS_Store
24 changes: 11 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- nightly

env:
Expand All @@ -17,6 +14,7 @@ env:
matrix:
allow_failures:
- php: nightly
- php: 7.4snapshot
- env: DEPENDENCIES=beta
- env: DEPENDENCIES=low

Expand All @@ -27,21 +25,21 @@ script:
- composer run-tests

before_script:
- if [ "$DEPENDENCIES" = "low" ]; then composer --prefer-lowest --prefer-stable update; else composer update; fi;
- if [ "$DEPENDENCIES" = "low" ]; then composer -vvv --prefer-lowest --prefer-stable update; else composer update; fi;

after_script:
- php vendor/bin/coveralls -v
- php vendor/bin/php-coveralls -v

before_install:
- sudo apt-get update
- sudo apt-get install ffmpeg
- composer self-update
- wget http://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-9.90.tar.gz
- tar -zxvf Image-ExifTool-9.90.tar.gz
- cd Image-ExifTool-9.90 && perl Makefile.PL && make test && sudo make install
- cd .. && rm -rf Image-ExifTool-9.90
- wget http://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.77.tar.gz
- tar -zxvf Image-ExifTool-11.77.tar.gz
- cd Image-ExifTool-11.77 && perl Makefile.PL && make test && sudo make install
- cd .. && rm -rf Image-ExifTool-11.77
# Set composer minimum-stability configuration filter to beta versions
- if [ "$DEPENDENCIES" = "beta" ]; then perl -pi -e 's/^}$/,"minimum-stability":"beta"}/' composer.json; fi;
# Disable xdebug, there is no use of it being enabled
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
# Prevent Travis throwing an out of memory error
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
# Test if we have a token for github in case the project hits the 60 rph limit
Expand Down
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
"role": "Developer"
}
],
"keywords": ["EXIF", "IPTC", "jpeg", "tiff", "exiftool"],
"keywords": ["EXIF", "IPTC", "jpeg", "tiff", "exiftool", "FFmpeg", "FFprobe"],
"require": {
"php": ">=5.4"
"php": ">=7.1",
"php-ffmpeg/php-ffmpeg": "^0.14.0"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "^1.0",
"phpmd/phpmd": "~2.2",
"phpunit/phpunit": ">=4.0 <6.0",
"satooshi/php-coveralls": "~0.6",
"sebastian/phpcpd": "1.4.*@stable",
"squizlabs/php_codesniffer": "1.4.*@stable"
"php-coveralls/php-coveralls": "^2.2",
"phpmd/phpmd": "^2.7",
"phpunit/phpunit": ">=8.4",
"sebastian/phpcpd": "^4.1",
"friendsofphp/php-cs-fixer": "^2.16",
"squizlabs/php_codesniffer": "^3.5"
},
"suggest": {
"lib-exiftool": "Use perl lib exiftool as adapter",
"ext-exif": "Use exif PHP extension as adapter"
"ext-exif": "Use exif PHP extension as adapter",
"FFmpeg": "Use FFmpeg/FFprobe as adapter"
},
"autoload": {
"psr-0": {
Expand Down
123 changes: 123 additions & 0 deletions lib/PHPExif/Adapter/FFprobe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* PHP Exif Native Reader Adapter
*
* @link http://github.com/miljar/PHPExif for the canonical source repository
* @copyright Copyright (c) 2013 Tom Van Herreweghe <[email protected]>
* @license http://github.com/miljar/PHPExif/blob/master/LICENSE MIT License
* @category PHPExif
* @package Reader
*/

namespace PHPExif\Adapter;

use PHPExif\Exif;
use InvalidArgumentException;
use FFMpeg;

/**
* PHP Exif Native Reader Adapter
*
* Uses native PHP functionality to read data from a file
*
* @category PHPExif
* @package Reader
*/
class FFprobe extends AdapterAbstract
{

const TOOL_NAME = 'ffprobe';

/**
* Path to the exiftool binary
*
* @var string
*/
protected $toolPath;

/**
* @var string
*/
protected $mapperClass = '\\PHPExif\\Mapper\\FFprobe';


/**
* Setter for the exiftool binary path
*
* @param string $path The path to the exiftool binary
* @return \PHPExif\Adapter\FFprobe Current instance
* @throws \InvalidArgumentException When path is invalid
*/
public function setToolPath($path)
{
if (!file_exists($path)) {
throw new InvalidArgumentException(
sprintf(
'Given path (%1$s) to the ffprobe binary is invalid',
$path
)
);
}

$this->toolPath = $path;

return $this;
}



/**
* Getter for the ffprobe binary path
* Lazy loads the "default" path
*
* @return string
*/
public function getToolPath()
{
if (empty($this->toolPath)) {
$path = exec('which ' . self::TOOL_NAME);
$this->setToolPath($path);
}

return $this->toolPath;
}

/**
* Reads & parses the EXIF data from given file
*
* @param string $file
* @return \PHPExif\Exif|boolean Instance of Exif object with data
*/
public function getExifFromFile($file)
{
$mimeType = mime_content_type($file);

// file is not a video -> wrong adapter
if (strpos($mimeType, 'video') !== 0) {
return false;
}

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


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

$data = array_merge($stream, $format, array('MimeType' => $mimeType, 'filesize' => filesize($file)));


// map the data:
$mapper = $this->getMapper();
$mappedData = $mapper->mapRawData($data);

// hydrate a new Exif object
$exif = new Exif();
$hydrator = $this->getHydrator();
$hydrator->hydrate($exif, $mappedData);
$exif->setRawData($data);

return $exif;
}
}
28 changes: 20 additions & 8 deletions lib/PHPExif/Adapter/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PHPExif\Adapter;

use PHPExif\Exif;
use FFMpeg;

/**
* PHP Exif Native Reader Adapter
Expand Down Expand Up @@ -70,14 +71,18 @@ class Native extends AdapterAbstract
* @var array
*/
protected $iptcMapping = array(
'title' => '2#005',
'keywords' => '2#025',
'copyright' => '2#116',
'caption' => '2#120',
'headline' => '2#105',
'credit' => '2#110',
'source' => '2#115',
'jobtitle' => '2#085'
'title' => '2#005',
'keywords' => '2#025',
'copyright' => '2#116',
'caption' => '2#120',
'headline' => '2#105',
'credit' => '2#110',
'source' => '2#115',
'jobtitle' => '2#085',
'city' => '2#090',
'sublocation' => '2#092',
'state' => '2#095',
'country' => '2#101'
);


Expand Down Expand Up @@ -173,6 +178,11 @@ public function getSectionsAsArrays()
*/
public function getExifFromFile($file)
{
$mimeType = mime_content_type($file);



// Photo
$sections = $this->getRequiredSections();
$sections = implode(',', $sections);
$sections = (empty($sections)) ? null : $sections;
Expand All @@ -191,6 +201,7 @@ public function getExifFromFile($file)
$xmpData = $this->getIptcData($file);
$data = array_merge($data, array(self::SECTION_IPTC => $xmpData));


// map the data:
$mapper = $this->getMapper();
$mappedData = $mapper->mapRawData($data);
Expand All @@ -204,6 +215,7 @@ public function getExifFromFile($file)
return $exif;
}


/**
* Returns an array of IPTC data
*
Expand Down
Loading