/** * Convert media according to specifications in $this->outputInfo. * * @param string $filename - passed by reference because sometimes there is a need to change the filename, * or otherwise the backend will produce an error * @return Naf_Media_Info - information for the newly created * @throws Naf_Media_Exception */ function convert(&$filename, $start = 0, $duration = null) { $c = new Naf_ShellCmd($this->command); $format = $this->outputInfo->getFormat(); if ($format) { $ext = substr($filename, strrpos($filename, '.') + 1); if ($ext != $format) { $filename .= '.' . $format; } } else { $ext = substr($this->source, strrpos($this->source, '.') + 1); $filename .= '.' . $ext; } $c->addOption('-i', $this->source); $c->setTarget($filename); $c->addOptionIf($start, '-ss', $start); $c->addOptionIf($duration, '-t', $duration); if (($width = $this->outputInfo->getWidth()) && ($height = $this->outputInfo->getHeight())) { $c->addOption('-s', $width . 'x' . $height); } $bitrate = $this->outputInfo->getBitrate(); $c->addOptionIf($bitrate, '-b', (int) $bitrate . "k"); try { $c->exec(); } catch (Naf_Exception $e) { throw new Naf_Media_Exception("Ffmpeg call failed: " . $e->getMessage() . "\nCommand: " . $c->getLastCommannd()); } return Naf_Media::reader()->info($filename); }
function save($filename, $start = 0) { $c = new Naf_ShellCmd($this->command); $c->addOption('-i', $this->source); $c->addOption('-f', 'image2'); $c->addOption('-vframes', 1); $c->addOptionIf($start, '-ss', $start); $c->addOptionIf($this->width && $this->height, '-s', $this->width . 'x' . $this->height); if (null === $filename) { $c->setTarget('-'); // flush picture to STDOUT try { return $c->exec(); } catch (Naf_Exception $e) { throw new Naf_Media_Exception("Snapshot failed! " . $e->getMessage()); } } else { $c->setTarget($filename); // save to file try { $c->exec(); return; } catch (Naf_Exception $e) { throw new Naf_Media_Exception("Snapshot failed! " . $e->getMessage()); } } }
function save($filename, $start = 0) { $c = new Naf_ShellCmd($this->command); $c->setTarget($this->source); $c->addOption('-vo', 'jpeg:outdir=' . $this->tmpDir); $c->addOption('-nosound'); $c->addOption('-frames', 2); $c->addOptionIf($start, '-ss', $start); $c->addOptionIf($this->width && $this->height, '-vf', 'scale=' . $this->width . ':' . $this->height); try { $c->exec(); } catch (Naf_Exception $e) { throw new Naf_Media_Exception("Snapshot failed! " . $e->getMessage()); } @unlink($this->tmpDir . '/00000001.jpg'); $image = $this->tmpDir . '/00000002.jpg'; if ($filename) { rename($image, $filename); return; } else { $return = file_get_contents($image); @unlink($image); return $return; } }
/** * Configure shell-command * * @param Naf_ShellCmd $c */ function configure(Naf_ShellCmd $c) { $f = $this->info->getFormat(); $c->addOptionIf($f, '-of', $f); $c->addOption('-ovc', $this->info->getVideoCodec()); $c->addOption('-oac', $this->info->getAudioCodec()); }