php-vfxapply/plugins/ffmpeg/plugin.php

89 lines
2.5 KiB
PHP

<?php
namespace VfxApply\Plugin\Ffmpeg;
use VfxApply\Plugin;
use VfxApply\Input;
use VfxApply\Output;
use VfxApply\Preset;
class FfmpegPlugin extends Plugin
{
public function getName()
{
return "ffmpeg";
}
private function appendFilterOption($filter, $key, $value=null)
{
if (strpos($filter,"=")===false) {
$filter.= "=";
} else {
$filter.= ":";
}
$filter.= $key . ($value?"={$value}":"");
return $filter;
}
public function applyPreset(Preset $preset, Input $input, Output $output)
{
// Get filter string from preset
$filter = $preset->get("filter");
$type = $preset->get("type")?:"complex";
$extra = $preset->get("extra");
switch ($type) {
case 'audio':
$filter_type = "-af";
$filter_extra = "";
break;
case 'video':
$filter_type = "-vf";
$filter_extra = "";
break;
case 'complex':
default:
$filter_type = "-filter_complex";
$filter_extra = "";
break;
}
/*if (($size = $preset->getParam("size"))) {
$filter = $this->appendFilterOption($filter, "s", $size);
}*/
// Create a progress dialog
$dialog = $this->createDialog(DIALOG_PROGRESS, "Applying filter");
$dialog->show();
// Create command line to run ffmpeg
$cmdline = sprintf(
"ffmpeg -loglevel error -y -stats -i %s %s %s %s %s 2>&1",
escapeshellarg($input->getFilename()),
$filter_type,
escapeshellarg($filter),
trim($extra." ".$filter_extra),
escapeshellarg($output->getFilename())
);
// Create a coprocess for ffmpeg
$frames = $input->getVideoDuration()->frames;
$ffmpeg = $this->createProcess($cmdline, function ($read) use ($dialog,$frames) {
$dialog->setText(trim($read));
if (preg_match('/^frame=[\s]*([0-9]+)\s/', trim($read), $match)) {
$frame = intval($match[1]);
$pc = round($frame/$frames*100);
$dialog->setProgress($pc);
}
if ($dialog->isCancelled()) return false;
// frame= 4140 fps=102 q=-1.0 Lsize= 4559kB time=00:00:48.04 bitrate= 777.3kbits/s speed=1.18x
});
$ffmpeg->run();
unset($dialog);
}
}
return new FfmpegPlugin();