public function testGuessNameFile() { $map = ['video_h264_qt_tag.mp4' => 'video']; foreach ($map as $file => $name) { $this->assertEquals($name, Type::guessName($this->_files . '/' . $file), "File `{$file}`."); } }
public function convert($mimeType) { if (Type::guessName($mimeType) != 'image') { return true; } if (!isset($this->_formatMap[$mimeType])) { throw new OutOfBoundsException("MIME type `{$mimeType}` cannot be mapped to a format."); } return $this->_object->setFormat($this->_formatMap[$mimeType]); }
public function convert($mimeType) { if (Type::guessName($mimeType) != 'audio') { return true; // others care about inter media type conversions } $sourceType = $this->_objectType; $targetType = $this->_type($mimeType); $modify = null; if ($this->_sampleRate) { $modify .= " --rate {$this->_sampleRate}"; } if ($this->_channels) { $modify .= " --channels {$this->_channels}"; } rewind($this->_object); $error = fopen('php://temp', 'w+b'); $sourceTemp = $this->_objectTemp; $targetTemp = $this->_tempFile(); // Since SoX 14.3.0 multi threading is enabled which // paradoxically can cause huge slowdowns. $command = "{$this->_command} -q --single-threaded"; $command .= " -t {$sourceType} {$sourceTemp}{$modify} -t {$targetType} {$targetTemp}"; $descr = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; $process = proc_open($command, $descr, $pipes); $output = stream_get_contents($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); $return = proc_close($process); if ($return != 0) { $message = "Command `{$command}` returned `{$return}`:"; $message .= "\nOutput was:\n" . ($output ?: 'n/a'); $message .= "\nError output was:\n" . ($error ?: 'n/a'); throw new RuntimeException($message); } // Workaround for header based formats which require the output stream to be seekable. $target = fopen($targetTemp, 'r'); $buffer = fopen('php://temp', 'w+'); stream_copy_to_stream($target, $buffer); fclose($target); unlink($targetTemp); unlink($this->_objectTemp); $this->_load($buffer); return true; }
/** * This factory method takes a source or an instance of an adapter, * guesses the type of media maps it to a media information class * and instantiates it. * * @param array $config Valid values are: * - `'source'`: An absolute path to a file. * - `'adapters'`: Names or instances of media adapters (i.e. `['Gd']`). * @return \mm\Media\Info\Generic An instance of a subclass of `\mm\Media\Info\Generic` or * if type could not be mapped an instance of the that class itself. */ public static function factory(array $config = []) { $default = ['source' => null, 'adapters' => []]; extract($config + $default); if (!$source) { throw new BadMethodCallException("No source given."); } $name = Type::guessName($source); $class = "\\mm\\Media\\Info\\" . ucfirst($name); if (!$adapters) { if (!isset(static::$_config[$name])) { throw new Exception("No adapters configured for media name `{$name}`."); } $adapters = static::$_config[$name]; } return new $class(compact('source', 'adapters')); }
public function convert($mimeType) { switch (Type::guessName($mimeType)) { case 'image': $this->_options = ['vcodec' => '-vcodec ' . $this->_type($mimeType), 'vframes' => '-vframes 1', 'seek' => '-ss ' . intval($this->duration() / 4), 'noAudio' => '-an'] + $this->_options; if ($mimeType == 'image/jpeg') { // Get highest quality jpeg as possible; will // be compressed later. $this->_options['qscale:v'] = '-qscale:v 1'; } $this->_targetType = 'rawvideo'; break; case 'video': $this->_targetType = $this->_type($mimeType); break; } return true; }
public function convert($mimeType) { if (Type::guessName($mimeType) != 'image') { return true; } if (!isset($this->_formatMap[$mimeType])) { throw new OutOfBoundsException("Conversion to MIME type `{$mimeType}` not supported."); } return $this->_format = $this->_formatMap[$mimeType]; }
/** * Converts the media to given MIME type. * * @param string $mimeType * @return boolean|object false on error or a Media object on success */ public function convert($mimeType) { $this->_adapter->convert($mimeType); if ($this->name() != Type::guessName($mimeType)) { // Crosses media (i.e. document -> image). $config = Process::config(); if ($config[$this->name()] == $config[Type::guessName($mimeType)]) { // ...but using the same adapter. $media = Process::factory(['source' => $mimeType, 'adapter' => $this->_adapter]); } else { // ...using different adapters. $handle = fopen('php://temp', 'w+'); $this->_adapter->store($handle); $media = Process::factory(['source' => $handle]); fclose($handle); } return $media; } // Stays entirely in same media (i.e. image -> image). return $this; }
public function testGuessNameResource() { $handleA = fopen("{$this->_files}/application_pdf.pdf", 'r'); $handleB = fopen('php://temp', 'r+'); stream_copy_to_stream($handleA, $handleB); $this->assertEquals('document', Type::guessName($handleA)); $this->assertEquals('document', Type::guessName($handleB)); fclose($handleA); fclose($handleB); }