Skip to content

Commit

Permalink
Merge pull request #40 from alchemy-fr/master
Browse files Browse the repository at this point in the history
PHRAS-2821 #merge backport to 0.5 branch fix video orientation
  • Loading branch information
nmaillat authored Dec 11, 2019
2 parents 83645b4 + c97b37a commit 3bd3204
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 101 deletions.
25 changes: 25 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2
jobs:
build:
shell: /bin/bash --login
docker:
- image: circleci/php:7.0-fpm-stretch
steps:
- checkout
- save_cache:
key: dependency-cache
paths:
- ~/.composer
- run: sudo apt-get update
- run: sudo apt-get install -y zlib1g-dev ghostscript gpac imagemagick libav-tools libfreetype6-dev libicu-dev libmagickwand-dev libmcrypt-dev librabbitmq-dev libssl-dev libxslt-dev libzmq3-dev ufraw mcrypt swftools unoconv unzip xpdf
- run: sudo apt-get clean
- run: wget http://www.swftools.org/swftools-0.9.1.tar.gz
- run: sh -c "tar xzvf swftools-0.9.1.tar.gz && cd swftools-0.9.1 && ./configure && make && sudo make install"
- run: yes '' | sudo pecl install imagick
- run: sudo docker-php-ext-enable imagick
- run: sudo docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
- run: sudo docker-php-ext-install -j$(nproc) gd
- run: composer install --prefer-source --no-interaction
- run:
name: unit test
command: ./vendor/bin/phpunit
33 changes: 0 additions & 33 deletions .travis.yml

This file was deleted.

5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# MediAlchemyst

A PHP 5.3+ lib to transmute media files.
A PHP 7.0+ lib to transmute media files.

[![Build Status](https://travis-ci.org/alchemy-fr/Media-Alchemyst.png?branch=master)](http://travis-ci.org/alchemy-fr/Media-Alchemyst)
[![CircleCI](https://circleci.com/gh/alchemy-fr/Media-Alchemyst.svg?style=svg)](https://circleci.com/gh/alchemy-fr/Media-Alchemyst)

* Want to extract audio from a video file ?
* Want to convert an office document to an image?
Expand All @@ -15,7 +15,6 @@ media-type.
## Usage example

```php

use MediaAlchemyst\Alchemyst;
use MediaAlchemyst\Specification\Animation;
use MediaAlchemyst\Specification\Image;
Expand Down
17 changes: 5 additions & 12 deletions src/MediaAlchemyst/Specification/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Image extends AbstractSpecification
protected $resolution_units = self::RESOLUTION_PIXELPERINCH;
protected $flatten = false;
protected $imageCodec = 'jpeg';
protected $pageStart = 1;
protected $pageQuantity = 1;
protected $page = 1;

const RESIZE_MODE_INBOUND = ImageInterface::THUMBNAIL_INSET;
const RESIZE_MODE_INBOUND_FIXEDRATIO = 'inset_fixedRatio';
Expand Down Expand Up @@ -156,19 +155,13 @@ public function isFlatten()
return $this->flatten;
}

public function fromPages($pageStart, $pageQuantity)
public function fromPage($page)
{
$this->pageStart = $pageStart;
$this->pageQuantity = $pageQuantity;
$this->page = $page;
}

public function getPageStart()
public function getPage()
{
return $this->pageStart;
}

public function getPageQuantity()
{
return $this->pageQuantity;
return $this->page;
}
}
4 changes: 2 additions & 2 deletions src/MediaAlchemyst/Transmuter/Document2Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d

try {
if ($source->getFile()->getMimeType() != 'application/pdf') {
$pageRange = $spec->getPageStart() . '-' . $spec->getPageQuantity();
$pageRange = $spec->getPage() . '-' . $spec->getPage();
$this->container['unoconv']->transcode(
$source->getFile()->getPathname(), Unoconv::FORMAT_PDF, $tmpDest, $pageRange
);
Expand All @@ -46,7 +46,7 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
$tmpDestSinglePage = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'unoconv-single');

$this->container['ghostscript.transcoder']->toPDF(
$tmpDest, $tmpDestSinglePage, $spec->getPageStart(), $spec->getPageQuantity()
$tmpDest, $tmpDestSinglePage, 1, 1
);

$image = $this->container['imagine']->open($tmpDestSinglePage);
Expand Down
7 changes: 4 additions & 3 deletions src/MediaAlchemyst/Transmuter/Video2Animation.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
foreach ($files as $file) {
$image = $this->container['imagine']->open($file);

if (0 !== $rotate) {
$image->rotate($rotate);
}
// the image frame from imagine is already rotated if the video source is also rotated
// if (0 !== $rotate) {
// $image->rotate($rotate);
// }

if ($spec->getWidth() && $spec->getHeight()) {
if (0 !== $rotate / 90 % 2) {
Expand Down
37 changes: 21 additions & 16 deletions src/MediaAlchemyst/Transmuter/Video2Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,30 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d

$image = $this->container['imagine']->open($tmpDest);

// the image frame from imagine is already rotated if the video source is also rotated
$rotated = false;
if (true === static::$autorotate && method_exists($source, 'getOrientation')) {
switch ($source->getOrientation()) {
case MediaVorusVideo::ORIENTATION_90:
$image->rotate(90);
$rotated = true;
break;
case MediaVorusVideo::ORIENTATION_270:
$image->rotate(-90);
$rotated = true;
break;
case MediaVorusVideo::ORIENTATION_180:
$image->rotate(180);
break;
default:
break;
}
if (method_exists($source, 'getOrientation') && !empty($source->getOrientation())) {
$rotated = true;
}

// if (true === static::$autorotate && method_exists($source, 'getOrientation')) {
// switch ($source->getOrientation()) {
// case MediaVorusVideo::ORIENTATION_90:
// $image->rotate(90);
// $rotated = true;
// break;
// case MediaVorusVideo::ORIENTATION_270:
// $image->rotate(-90);
// $rotated = true;
// break;
// case MediaVorusVideo::ORIENTATION_180:
// $image->rotate(180);
// break;
// default:
// break;
// }
// }

if ($spec->getWidth() && $spec->getHeight()) {
if (!$rotated) {
$box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
Expand Down
62 changes: 41 additions & 21 deletions src/MediaAlchemyst/Transmuter/Video2Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,50 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
$resizeMode = $spec->getResizeMode();
}


if (true === static::$autorotate && method_exists($source, 'getOrientation')) {
switch ($source->getOrientation()) {
case MediaVorusVideo::ORIENTATION_90:
$video->addFilter(new RotateFilter(RotateFilter::ROTATE_90));
break;
case MediaVorusVideo::ORIENTATION_270:
$video->addFilter(new RotateFilter(RotateFilter::ROTATE_270));
break;
case MediaVorusVideo::ORIENTATION_180:
$video->addFilter(new RotateFilter(RotateFilter::ROTATE_180));
break;
default:
break;
}
}
// ffmpeg rotate automatically the generated video based on the rotate metadata
// https://trac.ffmpeg.org/ticket/515#comment:12

// if (true === static::$autorotate && method_exists($source, 'getOrientation')) {
// switch ($source->getOrientation()) {
// case MediaVorusVideo::ORIENTATION_90:
// $video->addFilter(new RotateFilter(RotateFilter::ROTATE_90));
// break;
// case MediaVorusVideo::ORIENTATION_270:
// $video->addFilter(new RotateFilter(RotateFilter::ROTATE_270));
// break;
// case MediaVorusVideo::ORIENTATION_180:
// $video->addFilter(new RotateFilter(RotateFilter::ROTATE_180));
// break;
// default:
// break;
// }
// }

$video->addFilter(new SynchronizeFilter());
if ($source->getWidth() > $spec->getWidth() || $source->getHeight() > $spec->getHeight()) {
$video->addFilter(
new ResizeFilter(
new Dimension($spec->getWidth(), $spec->getHeight()), $resizeMode
)
);
// if it is a portrait video ,get reverse ratio from the source to calculate dimension
if (method_exists($source, 'getOrientation')
&& in_array($source->getOrientation(), [MediaVorusVideo::ORIENTATION_90, MediaVorusVideo::ORIENTATION_270])) {

// reverse origin dimension
$reverseOriginalDimension = new Dimension($source->getHeight(), $source->getWidth());
$reverseRatio = $reverseOriginalDimension->getRatio();

// resizes the video to fit the dimension height to avoid anamorphosis
$width = $reverseRatio->calculateWidth($spec->getHeight(), $format->getModulus());

$video->addFilter(
new ResizeFilter(
new Dimension($width, $spec->getHeight()), ResizeFilter::RESIZEMODE_FIT
)
);
} else {
$video->addFilter(
new ResizeFilter(
new Dimension($spec->getWidth(), $spec->getHeight()), $resizeMode
)
);
}
}

if ($spec->getAudioCodec()) {
Expand Down
2 changes: 1 addition & 1 deletion tests/MediaAlchemyst/Tests/AlchemystNoBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testOpenUnknownFile()
$driversContainer = new DriversContainer();
$object = new Alchemyst($driversContainer, $this->getFsManager());

$object->turnInto(__DIR__ . '/../../files/invalid.file', 'dest.mpg', $this->getMock('MediaAlchemyst\Specification\SpecificationInterface'));
$object->turnInto(__DIR__ . '/../../files/invalid.file', 'dest.mpg', $this->createMock('MediaAlchemyst\Specification\SpecificationInterface'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/MediaAlchemyst/Tests/AlchemystTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function setUp()
*/
public function testOpenUnknownFile()
{
$this->object->turnInto(__DIR__ . '/../../files/invalid.file', 'here.mpg', $this->getMock('MediaAlchemyst\Specification\SpecificationInterface'));
$this->object->turnInto(__DIR__ . '/../../files/invalid.file', 'here.mpg', $this->createMock('MediaAlchemyst\Specification\SpecificationInterface'));
}

/**
Expand Down
12 changes: 5 additions & 7 deletions tests/MediaAlchemyst/Tests/Specification/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,12 @@ public function testSetFlatten()
}

/**
* @covers MediaAlchemyst\Specification\Image::setfromPages
* @covers MediaAlchemyst\Specification\Image::getPageStart
* @covers MediaAlchemyst\Specification\Image::getPageQuantity
* @covers MediaAlchemyst\Specification\Image::setfromPage
* @covers MediaAlchemyst\Specification\Image::getPage
*/
public function testFromPages()
public function testFromPage()
{
$this->object->fromPages(1, 2);
$this->assertEquals(1, $this->object->getPageStart());
$this->assertEquals(2, $this->object->getPageQuantity());
$this->object->fromPage(2);
$this->assertEquals(2, $this->object->getPage());
}
}
4 changes: 2 additions & 2 deletions tests/MediaAlchemyst/Tests/Transmuter/Image2ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function testExecuteOverCR2()

$MediaDest = $this->getMediaVorus()->guess($this->dest);

$this->assertEquals(1884, $MediaDest->getHeight());
$this->assertEquals(2807, $MediaDest->getWidth());
$this->assertEquals(1876, $MediaDest->getHeight());
$this->assertEquals(2808, $MediaDest->getWidth());
}

/**
Expand Down

0 comments on commit 3bd3204

Please sign in to comment.