Where to hook in to automate video thumbnail generation

I am using ffmpeg to generate thumbnails for the videos. Currently, I have a shell script which checks all the video files in a given directory, and creates a similarly named jpg image for the thumbnail. And, of course, I run this manually to get the job done. I'd like to automate the task to be done at the same time normal image thumbnails are generated, but I can't figure out where that is done in the code. I'm a C/C++ guy, so I have the general chops, but lack the familiarity with PHP/JS/etc. Thanks for any help. :)

Comments

  • acrylian Administrator, Developer
    `zp-core/i.php` is the actual image processor creating the cached resized and/or cropped images. But you can actually bypass it if you follow the naming requirements of these cached files.

    Actually Zenphoto will create those images when needed itself if needed. Some info about caching:
    http://www.zenphoto.org/news/caching/
  • After fiduddling with things for a bit, I've settled on a solution with some changes to class-video.php. Using the existing feature where putting an image (myvideo.jpg) with the same name as the video (myvideo.mp4) in the appropriate album 'does the right thing', I've modified getThumbImageFile to check for the image file, and run ffmpeg if it does not exist. I'm currently extracting a frame at an offset 3 seconds, and this seems to work perfectly. The website that I am doing this for will never (famous last words) have short videos on it, so the hardcoded time won't be an issue. Assuming I can resize the image in the same operation as grabbing the frame, I will probably do that too. Two questions, 1) Does this seem correct, in the spirit of how ZenPhoto works, and 2) should I put more time into this to make it a publicly usable feature? With configuration to enable it, and configuration options that might apply, such as the mentioned frame grab offset.
  • acrylian Administrator, Developer
    We appreaciate your efforts. General rules though: If some core plugin does not fit your needs, best create your own custom version with another name and set it up for working from `/plugins`. Otherwise your changes will be lost on the next update.

    Actually I would not recommend to hook into class-video as that will not be future save. Instead I suggest you create a plugin that hooks into the `new_image` filter which triggers on discovery of a new "image" from the file system. Extract the image there, the image processor then will work with the created sidecar image as with all else. Take a look here:
    http://www.zenphoto.org/news/zenphoto-plugin-architecture/#zenphoto-filters
  • Thanks for the help! And I totally understand, so I will rework within the best practices guidelines. I actually had started down the path of using a hook, but I can't recall why I moved away from it. Probably just me being lazy and wanting to get things working. :) I have updated the sidecar image generation, to both grab a keyframe and resize it, so it feels like a complete feature proof of concept now. I'm heading out of town for an interview, but will post when I have a working version of my plugin.
  • Two questions, 1) Can I leverage another part of ZenPhoto to know this image is a video file? ie. I don't want to re-do work that is already available. And 2) while class-video.php is a core plugin, doesn't this task seem very appropriate for that plugin? And can't I submit a pull request for changes I make to it?
  • An implementation question, the path I chose verifies the sidecar image exists whenever the thumbnail is being displayed, which is the same way the thumbnail cache is maintained. I'm concerned that there is an edge case that won't work by hooking new_image. ie. if the sidecar image gets deleted, then it won't get regenerated unless the video is removed and re-added, yes? I could see the use case where someone has used my plugin, has sidecar images created from it, and they decide they want to change the config of my plugin, ie. the frame grab time, or the resize param. To have this work, you would want to get rid of the previous sidecar images, and have new ones created. Or, maybe the user deletes some sidecar images by hand. I assume this is similar to why the thumbnails are not generated using new_image either, but instead do so at the time the thumbnail is being displayed.
  • Just thinking out-loud here, what about a new thumbnail_requested event? And have all code relating to thumbnail generation hook this?
  • acrylian Administrator, Developer
    First to above two questions:
    1) above: There is `isImagePhoto()`and `isImageVideo()` to check if the image is an actual image and not any "non image" image like video, audio or whatever.
    2) Theoretically you can send a pull request and we appreciate the thought. But for now I would prefer a separate plugin. One reason is I cannot even test your change as I don't have any server with ffmpeg available and don't manage my own. ZP is for standard hosting primarily so most people won't have it available.

    Also behind the scenes we are aworking on an major version (no not in public in case you wanted to ask, sorry) and that restructures a few things internally and that involves the video-class, too. So in 1.4.x we will not make such probably significant changes anymore unless it would be a bug (which this is sureyl not).

    To the other question: Cached images will be re-created on size changes etc. as long as the image to create from still exists. Can't your plugin just delete the existing sidecar image if it is decided to use something else? If it also clears the cache on that occasion it should be regenerated as normal.
  • Thanks for the quick reply. And again, I understand regarding me submitting code, and the new version.

    Regarding my concern about edge cases, yes, I could have my plugin delete all of the sidecar images if a parameter is changed, but this feels very brute force. As well, that doesn't handle the other edge case of the sidecar file being deleted behind the scenes. Which I suppose I could handle by putting an option in the plugin to iterate over the entire gallery and create any missing sidecar images. But, doing it at thumbnail request time is the perfect solution, just as it is for maintaining the thumbnail cache.

    Based on this discussion, I am going to keep my hacked version of class-video, since this is working exactly as I want it. And if the new version of ZP offers a more elegant solution, then I will proceed with a proper plugin.

    Also, if you are interested, here is my background: http://www.musetrap.com/cpr/resume
  • acrylian Administrator, Developer
    Regarding the edge case deleting on the file system. Generally zp then just assumes it is gone. Actually you should be able to test if there is a sidecar image using the `objectsThumb` property of the video class object. It is null if not so you then could request a reganeration.

    Okay, best keep your modified version or even provide it as a plugin for others to use for the time being. The filters will remain even if some other internal stuff changes.

    Thanks for your background ;-) Please be assured we welcome any kind of contribution and plugins for specific tasks are as well.
Sign In or Register to comment.