Php-ffmpeg: How to resize a frame that is pulled from a video?

Created on 29 Mar 2017  路  10Comments  路  Source: PHP-FFMpeg/PHP-FFMpeg

$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->resize(new FFMpeg\Coordinate\Dimension(162, 78))
    ->save($Save_Location."/".$FileName[0].".jpg");

I have everything working fine but I had trouble finding how to resize thumbnails that are pulled from video. Above is my attempt. Can anyone point me in the right direction?

Thanks,

Travis

Feature

Most helpful comment

Filter class:

namespace FFMpeg\Filters\Frame;

use FFMpeg\Exception\RuntimeException;
use FFMpeg\Media\Frame;

class ImageDimensionFilter implements FrameFilterInterface
{
    /** @var \FFMpeg\Coordinate\Dimension */
    private $dimension;

    /** @var integer */
    private $priority;

    public function __construct($priority = 0)
    {
        $this->priority = $priority;

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getPriority()
    {
        return $this->priority;
    }

    /**
     * {@inheritdoc}
     */
    public function getDimension()
    {
        return $this->dimension;
    }

    public function setDimension(\FFMpeg\Coordinate\Dimension $dimension) {
        $this->dimension = $dimension;

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function apply(Frame $frame)
    {
        $dimensions = null;
        $commands = array();

        foreach ($frame->getVideo()->getStreams() as $stream) {
            if ($stream->isVideo()) {
                try {
                    $commands[] = '-s';
                    $commands[] = $this->dimension->getWidth() . 'x' . $this->dimension->getHeight();
                    break;
                } catch (RuntimeException $e) {

                }
            }
        }

        return $commands;
    }
}

How to use:

$frame->addFilter(
    (new \FFMpeg\Filters\Frame\ImageDimensionFilter())
        ->setDimension(
            new \FFMpeg\Coordinate\Dimension($width, $height)
        )
);

All 10 comments

That's not possible with the current code. I would recommend saving the file, loading it with the image adapter of your choice(gd or imagick) and resize it using this.

But I'll do a pr after my previous have been merged(so I can still focus on them, before making 100 of pull requests...)

Filter class:

namespace FFMpeg\Filters\Frame;

use FFMpeg\Exception\RuntimeException;
use FFMpeg\Media\Frame;

class ImageDimensionFilter implements FrameFilterInterface
{
    /** @var \FFMpeg\Coordinate\Dimension */
    private $dimension;

    /** @var integer */
    private $priority;

    public function __construct($priority = 0)
    {
        $this->priority = $priority;

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getPriority()
    {
        return $this->priority;
    }

    /**
     * {@inheritdoc}
     */
    public function getDimension()
    {
        return $this->dimension;
    }

    public function setDimension(\FFMpeg\Coordinate\Dimension $dimension) {
        $this->dimension = $dimension;

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function apply(Frame $frame)
    {
        $dimensions = null;
        $commands = array();

        foreach ($frame->getVideo()->getStreams() as $stream) {
            if ($stream->isVideo()) {
                try {
                    $commands[] = '-s';
                    $commands[] = $this->dimension->getWidth() . 'x' . $this->dimension->getHeight();
                    break;
                } catch (RuntimeException $e) {

                }
            }
        }

        return $commands;
    }
}

How to use:

$frame->addFilter(
    (new \FFMpeg\Filters\Frame\ImageDimensionFilter())
        ->setDimension(
            new \FFMpeg\Coordinate\Dimension($width, $height)
        )
);

@FlexIDK Do you want to file a pull request?

@FlexIDK Do you want to file a pull request?

I'm don't know, how make this. If you want, you can add this file.

any plan to merge this into release?

@foobarhe Feel free to make a pull request. :)

This doesn't works for me, as the resizing is done after the frame video filter:

array:8 [
  0 => "-y"
  1 => "-i"
  2 => "/var/www/html/storage/app/upload-tmp/vod-channel-PWGubwymi2wkCkEg5hZu8j/XEXYEAyAMwm5HHp1CgeiRj"
  3 => "-vf"
  4 => "fps=2.9411764705882/1"
  5 => "/tmp/vod-49418d94da11d227/frame-%06d.jpg"
  6 => "-s"
  7 => "170x100"
]

but if I manage to put the -s 170x100 switch before the -vf it works.

How can I achieve this with with this lib ? as the filters are always concatenated AFTER the frame I don't know how to do that..

Unfortunately, this is not yet possible, as it is not possible to place filters whereever you like.

It very easy like this:

$ffmpeg = FFMpeg::create();
$ffmpeg->open('video path')
       ->frame(TimeCode::fromSeconds(1))
       ->addFilter(new CustomFrameFilter('scale=320x160')) //resize output frame image
       ->save('save path');

It very easy like this:

$ffmpeg = FFMpeg::create();
$ffmpeg->open('video path')
       ->frame(TimeCode::fromSeconds(1))
       ->addFilter(new CustomFrameFilter('scale=320x160')) //resize output frame image
       ->save('save path');

Really thank you !

Was this page helpful?
0 / 5 - 0 ratings