bveml.net

TYPO3: Add poster images to videos

A few lines of code let TYPO3 display poster images as previews for videos.

From the archive. This guide applies to the software and hardware versions it describes.

First, override the Fluid Styled Content partials in your template extension. I added the following lines to my extension’s TypoScript setup:

lib.contentElement {
   partialRootPaths {
      20 = EXT:template_extension/Resources/ext/fluid_styled_content/Private/Partials/
   }
}

Next, create Video.html in typo3conf/ext/template_extension/Resources/ext/fluid_styled_content/Private/Partials/Media/Rendering. I copied the original file from typo3/sysext/fluid_styled_content/Resources/Private/Partials/Media/Rendering/Video.html and made these changes:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">

<f:media class="video-embed-item" file="{file}" width="{dimensions.width}" height="{dimensions.height}" alt="{file.alternative}" title="{file.title}" additionalConfig="{settings.media.additionalConfig}"
    additionalAttributes="{poster: '{v:format.replace(substring:file.originalFile.properties.extension,content:file.originalFile.identifier,replacement:\'jpg\') -> v:format.prepend(add: file.originalFile.storage.configuration.basePath)}'}"
/>

</html>

I have fluidtypo3/vhs installed, so the example uses its ViewHelpers. You can write your own ViewHelper as well. The poster attribute uses the same path and base name as the video, with the extension changed to .jpg:

additionalAttributes="{poster: '{v:format.replace(substring:file.originalFile.properties.extension,content:file.originalFile.identifier,replacement:\'jpg\') -> v:format.prepend(add: file.originalFile.storage.configuration.basePath)}'}"

Place Video.jpg in the same directory as Video.mp4. The two files must have exactly the same base name, and the image must end in .jpg. The template reads the video’s extension and replaces it with jpg.

Archived comment

André M. · 9 September 2022

Brilliant!
Very little code, and it works efficiently. :)

Thanks for sharing.