A simple but effective command-line tool for generating thumbnails of a video and corresponding VTT file for use within JW Player to generate the toolbar thumbnail previews.
FFmpeg is required to generate the thumbnails. If you have libav-tools install and the binary is a different name to ffmpeg (or even if you have ffmpeg but installed in a different location) then you can modify the $params
information to point to your particular ffmpeg install.
PHP 5.4+ is required as the code uses short array syntax and the CallbackFilterIterator.
There are two ways to getting started using this script:
- Simply to copy/paste or download the thumbnails.php to your server
- Install via composer by running the command:
composer require amnuts/jwplayer-thumbnail-preview-generator
Typical usage would look like:
php thumbnails.php -i "/input/video.mp4"
This would generate the thumbnails for the video given coalesced into one file (a sprite sheet) and the required VTT file that specifies which thumbnail to display and where in the sprite sheet it is located.
If you do not wish to generate a sprite sheet then include the -v
switch to generate verbose output - ie, all the thumbnails are separate files and the VTT file points to each.
php thumbnails.php -i "/input/video.mp4" -v
It is recommended that you use the default coalesced file as this is more optimal when loading than individual images.
You can change the output directory with the -o
switch:
php thumbnails.php -i "/input/video.mp4" -o "/output/directory"
This will write the images and VTT file in the provided directory (default is just to write into the same directory as the generator script).
To alter the default time between thumbnails use the -t
switch with the number of seconds you'd like between each:
php thumbnails.php -i "/input/video.mp4" -t 30
That will generate one thumbnail for every 30 seconds of video.
To change the width of the thumbnail use the -w
switch with the size in pixels:
php thumbnails.php -i "/input/video.mp4" -w 75
That will generate thumbnails that are 75 pixels in width. The height is automatic and proportional to the video size.
The tool also provides the ability to generate a poster file of the video from a random frame of the video at the same time it's generating the thumbnails. To do this, use the -p
switch:
php thumbnails.php -i "/input/video.mp4" -p
The code you would use for JW Player would be something like:
<div id="video">video loading...</div>
<script>
jwplayer("video").setup({
playlist: [{
file: "/input/video.mp4",
image: "poster.jpg",
tracks: [{
file: "thumbnails.vtt",
kind: "thumbnails"
}]
}]
});
</script>