how to best store a thumbnail generated via ffmpeg?

i am making a plugin that adds a video player to the elgg file plugin.

as part of that i am adding support for ffmpeg to grab a screenshot of the video.. and so far i have completed to code for grabbing the screenshot. however, i am not clear on what is the best approach for storing the screenshot.

i am duplicating the code in the upload.php action for the file plugin, since it already contains code for creating multiple versions of a thumbnail for the image formats. what i need to know is how to deal with the file that ffmpeg outputs and then how to best convert it to thumbnails etc.

i considered opening a temporary file using tmpfile() and then passing that to the already existing elgg code to be converted into different thumbnail sizes. i notice though, that most of the elgg functions appear to expect the file to already be in the elgg filestore.. so i looked at moving the ffmpeg screenshot directly into the elgg filestore.. so far though i have not been successful. 

i haven't found any clear documentation on this and the examples in code that i am looking at from other plugins have so far not got me to the point of success!

any tips/links welcome.

  • You should be able to use the code from upload.php, just instead of $_FILES['upload']['tmp_name'] use the location of your screenshot

  • so are you thinking i would create a new elggfile object to hold the thumbnail image temporarily here?

    i ask since there are various lines in the upload.php file which relate to writing and copying/moving the tmp_name variable, e.g.

    move_uploaded_file($_FILES['upload']['tmp_name'], $file->getFilenameOnFilestore());

    as i comprehend how the filestore functions, i would be over-writing the originally uploaded file (the full video, in this case) if i were to replace the first variable with the thumbnail created by ffmpeg.
    so would i be creating a new elggfile object to store the ffmpeg thumbnail, then using that to create multiple versions/sizes of the thumbnail - then saving those thumbnails as assets of the original elggfile object.. and then finally removing the elggfile object i created as part of the process to temporarily store the ffmpeg icon?

  • You don't need to overwrite the video, you can save it as it's own entity.  Or you can just save the image file to the filestore without registering it to the database, but have it in a known relative location so you can retrieve it with something like

    $file = new ElggFile();

    $file->owner_guid = $video->guid;

    $file->setFilename('screenshot.jpg');

    $image = $file->getFilenameOnFilestore();

  • ok thanks - i wasn't sure if the new elggfile would create a new entity that would be visible in the elgg file list in the front-end or not.

    i will play with your code here.. i also want to ensure that the sub-files (thumbnails) are removed if the main video file is deleted.

  • It will only if you call the save() method on it, otherwise it can be used as a tool for reading/writing to the data directory without necessarily saving anything to the db

  • after playing with this a while.. i am still stuck..

    from what i am seeing, the ElggFile requires me to write the image data to the hard drive using:

    $file->write($image_data);

    without this, the file is not located at the hard drive location that '$file->getFilenameOnFilestore()' points to...

    however, i am not clear on how to 'grab' the image on the hard drive that is created via ffmpeg into a php variable that the write method can use correctly.

    what am i missing? 

  • you don't need to use the write() method, you can move/copy the file directly.  You know the path to the image created from ffmpeg, and you can use the ElggFile to create the path to where it will live in the filestore.  Then you can use the native php copy() or rename() to get it from ffmpeg to elgg

  • after much fiddling and gnashing of teeth.. i used the rename() approach and now the thumbnails are go. i needed to set the owner to be the uploader, rather than the file itself.. which took me a while to debug and correct.

    see here: https://www.infiniteeureka.com/file/view/7369/nasa-mars-anomolies-fact-or-fiction

    next i will activate the mp4 and flv streaming modules on my server and then look to code support for playlists and whatever other features i can find that are useful. there are many options.