algoo / preview-generator

generates previews of files with cache management
https://pypi.org/project/preview-generator/
MIT License
226 stars 50 forks source link

How to generate preview of video files? #232

Closed otavs closed 3 years ago

otavs commented 3 years ago

Hello!

I tried to generate a jpeg preview of a mp4 file, but got an exception Unsupported mimetype: video/mp4

I used the example code from 'Basic Usage' session of README.rst (replacing the an_image.png with my video file sample.mp4):

from preview_generator.manager import PreviewManager

cache_path = '/tmp/preview_cache'
file_to_preview_path = '/tmp/sample.mp4'

manager = PreviewManager(cache_path, create_folder= True)
path_to_preview_image = manager.get_jpeg_preview(file_to_preview_path)

This code works fine to generate jpeg previews of other file types such as pdf, docx and jpg, but for mp4 it is giving the exception. Maybe this is not the right method to generate previews of video files, but I didn't find documentation or an example about video files, so could someone please explain how it should be done?

raphj commented 3 years ago

Your code works on my setup, using the mp4 file e2e/cypress/fixtures/mp4-video-file.mp4 included in the Mattermost webapp folder.

raphj commented 3 years ago

Just to be sure, can you also give us the version of preview generator you are using?

raphj commented 3 years ago

Another path to explore: mp4 is patent-encumbered and your version of ffmpeg could be built without support for this format.This happens in some distribution, where you need to enable a specific multimedia repository with support for mp4.

otavs commented 3 years ago

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "main2.py", line 7, in path_to_preview_image = manager.get_jpeg_preview(file_to_preview_path) File "/home/ota/.local/lib/python3.8/site-packages/preview_generator/manager.py", line 99, in get_jpeg_preview builder = self._factory.get_preview_builder(mimetype) File "/home/ota/.local/lib/python3.8/site-packages/preview_generator/preview/builder_factory.py", line 41, in get_preview_builder raise UnsupportedMimeType('Unsupported mimetype: {}'.format(mimetype)) preview_generator.exception.UnsupportedMimeType: Unsupported mimetype: video/mp4

inkhey commented 3 years ago
* Ubuntu 20.04.2 LTS

* Python 3.8.5

* preview generator version: 0.2.3

* ffmpeg is installed

* mp4 file: https://user-images.githubusercontent.com/27082143/122568005-aa9e5980-d01f-11eb-806a-5cde424495e5.mp4

* I also tried with WebM and Ogg, but same error of Unsupported mimetype happens

* `ffmpeg -i /tmp/sample.mp4 /tmp/sample.webm` converts normally

* complete backtrace:
Skipping builder class [<class 'preview_generator.preview.generic_preview.OnePagePreviewBuilder'>]: method get_supported_mimetypes is not implemented
Skipping builder class [<class 'preview_generator.preview.generic_preview.ImagePreviewBuilder'>]: method get_supported_mimetypes is not implemented
Traceback (most recent call last):
  File "/home/ota/.local/lib/python3.8/site-packages/preview_generator/preview/builder_factory.py", line 39, in get_preview_builder
    return self._builder_classes[mimetype]()  # nopep8 get class and instantiate it
KeyError: 'video/mp4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main2.py", line 7, in <module>
    path_to_preview_image = manager.get_jpeg_preview(file_to_preview_path)
  File "/home/ota/.local/lib/python3.8/site-packages/preview_generator/manager.py", line 99, in get_jpeg_preview
    builder = self._factory.get_preview_builder(mimetype)
  File "/home/ota/.local/lib/python3.8/site-packages/preview_generator/preview/builder_factory.py", line 41, in get_preview_builder
    raise UnsupportedMimeType('Unsupported mimetype: {}'.format(mimetype))
preview_generator.exception.UnsupportedMimeType: Unsupported mimetype: video/mp4

Hello, last version of preview generator is 0.20 , 0.2.3 is a very old version which doesn't have proper video preview support.

otavs commented 3 years ago

Aah so that's it! I really don't know why it was using 0.2.3 as I only did pip install preview-generator on the day I posted the issue, but now after uninstalling and installing again it downloads the most recent 0.21, and works very well 😄

Thank you very much @inkhey and @raphj!