You are here

class MediaThumbnailVideo in Media Thumbnails Video 8

Media thumbnail plugin for videos.

Plugin annotation


@MediaThumbnail(
  id = "media_thumbnail_video",
  label = @Translation("Media Thumbnail Video"),
  mime = {
    "video/mp4",
  }
)

Hierarchy

Expanded class hierarchy of MediaThumbnailVideo

File

src/Plugin/MediaThumbnail/MediaThumbnailVideo.php, line 24

Namespace

Drupal\media_thumbnails_video\Plugin\MediaThumbnail
View source
class MediaThumbnailVideo extends MediaThumbnailBase {

  /**
   * {@inheritdoc}
   */
  public function createThumbnail($sourceUri) {
    $config = $this->config
      ->getEditable('media_thumbnails_video.settings');
    $path = $this->fileSystem
      ->realpath($sourceUri);
    try {

      // Create ffmpeg container.
      $ffmpeg = FFMpeg::create([
        'ffmpeg.binaries' => $config
          ->get('ffmpeg'),
        'ffprobe.binaries' => $config
          ->get('ffprobe'),
        'timeout' => $config
          ->get('timeout'),
        'ffmpeg.threads' => $config
          ->get('threads'),
      ]);
      try {

        // Try open file form real path.
        $video = $ffmpeg
          ->open($path);
        $thumbnail_path = $path . '.png';
        $width = $this->configuration['width'];
        $video
          ->frame(TimeCode::fromSeconds(1))
          ->save($thumbnail_path);
        if (!empty($width)) {
          $image = imagecreatefrompng($thumbnail_path);
          $image = imagescale($image, $width);
          imagepng($image, $thumbnail_path);
        }

        // Create a managed file object.
        $file = File::create([
          'uri' => $sourceUri . '.png',
          'status' => 1,
        ]);
        try {
          $file
            ->save();
          return $file;
        } catch (EntityStorageException $e) {
          $this->logger
            ->warning(t('Could not create thumbnail file entity.'));
          return NULL;
        }
      } catch (\Exception $e) {
        $this->logger
          ->warning($e
          ->getMessage());
        return NULL;
      }
    } catch (ExecutableNotFoundException $e) {
      $this->logger
        ->warning($e
        ->getMessage());
      return NULL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaThumbnailBase::$config protected property The config factory.
MediaThumbnailBase::$fileSystem protected property The file system interface.
MediaThumbnailBase::$logger protected property The logger interface.
MediaThumbnailBase::create public static function Injects some default services. Overrides ContainerFactoryPluginInterface::create
MediaThumbnailBase::__construct public function Constructs a new Media Thumbnail class. Overrides PluginBase::__construct
MediaThumbnailVideo::createThumbnail public function Create a thumbnail file using the passed source uri. Overrides MediaThumbnailInterface::createThumbnail
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.