No thumnail for Video which is less than 1 second long

Created on 15 March 2023, over 1 year ago
Updated 16 March 2024, 8 months ago

Problem/Motivation

Drupal shows an error in its log, when a new media content was created.

TypeError: imagescale(): Argument #1 ($image) must be of type GdImage, bool given in imagescale() (line 52 of /var/www/web/modules/contrib/media_thumbnails_video/src/Plugin/MediaThumbnail/MediaThumbnailVideo.php)
#0 /var/www/web/modules/contrib/media_thumbnails_video/src/Plugin/MediaThumbnail/MediaThumbnailVideo.php(52): imagescale(false, '500')
#1 /var/www/web/modules/contrib/media_thumbnails/src/Plugin/MediaThumbnailManager.php(94): Drupal\media_thumbnails_video\Plugin\MediaThumbnail\MediaThumbnailVideo->createThumbnail('public://47_167...')
#2 /var/www/web/modules/contrib/media_thumbnails/media_thumbnails.module(33): Drupal\media_thumbnails\Plugin\MediaThumbnailManager->createThumbnail(Object(Drupal\media\Entity\Media))
#3 [internal function]: media_thumbnails_media_presave(Object(Drupal\media\Entity\Media))
#4 /var/www/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('media_thumbnail...', Array)
#5 /var/www/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(249): Drupal\Core\Extension\ModuleHandler->invokeAll('media_presave', Array)

After further investigation the problem is with the ffmpeg command generated:
/usr/bin/ffmpeg -y -ss 00:00:01.00 -i /var/www/web/sites/default/files/47_1678886099.mp4 -vframes 1 -f image2 /var/www/web/sites/default/files/47_1678886099.mp4.png
This did not generate a thumbnail (ffmpeg output is: Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used))

By modifying it to
/usr/bin/ffmpeg -y -ss 00:00:00.00 -i /var/www/web/sites/default/files/47_1678886099.mp4 -vframes 1 -f image2 /var/www/web/sites/default/files/47_1678886099.mp4.png
ffmpeg was able to generate the thumbnail image.

Steps to reproduce

Uploading a mp4 Video with a duration of < 1 second leads to the generation of an empty thumbnail.

Proposed resolution

My suggestion is to use -ss 00:00:00.00 instead of 00:00:01.00, although I haven't even looked up what -ss does, seeking i guess.

Remaining tasks

-

User interface changes

-

API changes

-

Data model changes

-

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Michael Maaß
  • πŸ‡΅πŸ‡±Poland lamp5 Rzeszow

    xD I have 2 questions. How often you upload video with duration less then 1 second?
    How often video first frame is black frame?

  • How often you upload video with duration less then 1 second?

    I didn't even think about it, while i was testing my App, tbh. It happended quite naturally during testing.

    How often video first frame is black frame?

    I don't know.

  • πŸ‡ΊπŸ‡ΈUnited States joewhitsitt Iowa

    Could this be configurable site-wide or per media entity? Is there a technical reason why it is set to the first second besides avoiding black/fade-in frames?

  • πŸ‡ΊπŸ‡ΈUnited States HenryHartley

    I'd also like to have this as a configuration setting. While I don't have a need for videos less than a second long, I have a site with videos that have a single frame at the beginning that's different to the frames immediately following it. The content folks want that first frame to be used for the thumbnail. If this were a setting, defaulting to 1 but with the option to change it to 0 (zero) (or some other integer) that would be ideal.

  • πŸ‡¬πŸ‡§United Kingdom gMaximus

    I've run into this whilst using instagram feeds module. Turn out the client uploads short videos to Instagram. I changed a line of code that specifies the 1 second

    /src/Plugin/MediaThumbnail/MediaThumbnailVideo.php line 48

    Change
    $video->frame(TimeCode::fromSeconds(1))->save($thumbnail_path);

    to

    $video->frame(TimeCode::fromSeconds(0))->save($thumbnail_path);

    I think this should be configuration too. Ordinarily 1 second is great, I've never run into this before tonight. However, there are always edge cases. This client seems to be one of them.

Production build 0.71.5 2024