lakshmaji / Thumbnail

Thumbnail for a given video using FFMpeg
http://lakshmaji.github.io/Thumbnail
MIT License
110 stars 25 forks source link

Delay when generating video thumbnails #36

Open septierg opened 1 year ago

septierg commented 1 year ago

Hi every one, i noticed a mini delay when generating the thumbnail. Here is my situation: I 'm building a URL that accept media and from the media title I do a thumbnail from video or thumbnail from image depends of the mime_content_type. The plugin works fine but just asking is it normal to have let say 1 to 2 sec of delay. When i do the thumbnail from image it's super fast and when i do it with Thumbnails it take 1 to 2 sec to do it. Here is a video to take a look. Any help would help.

Thank you

https://user-images.githubusercontent.com/49695282/200943890-b0c63281-03d7-47c0-82bf-9cdd83fb7e12.MOV

lakshmaji commented 1 year ago

Hi @septierg, Thanks for raising the issue related to performance. I don't know the actual cause, could you be able to find the reason or when it is happening like when execution context is making the call to library, or when it is exiting ?

Could be able to provide me reproducible repo!

septierg commented 1 year ago

HI @lakshmaji thank you for your response, here is my code: Route: Route::get('/media/thumbnail/{size}/{file}', 'MediaController@getThumbnail')->name('media.get_thumbnail');

Controller:

public function getThumbnail($size, $file) {
    $media = Media::where('title', $file)->first();

    if(!is_numeric($size) || !isset($media)) {
        return response("", 400);
    }

    $thumb_size = $size > config('app.limit_size_thumbnail') ? config('app.limit_size_thumbnail') : ($size <= 0 
            ? config('app.default_size_thumbnail') : $size);

    return $this->generateThumbnail($media, $thumb_size);
}

private function generateThumbnail(Media $media, $size) {

    if(!\File::exists(storage_path("app/public/uploads/thumbnail"))) {
        \File::makeDirectory(storage_path("app/public/uploads/thumbnail"));
    }

    $file_source = storage_path($media->directory);
    $file_target = storage_path("app/public/uploads/thumbnail")."/". pathinfo($media->title, PATHINFO_FILENAME)."-".$size."-thumbnail.jpg";

    // generate video thumbnail
    if(strstr(mime_content_type($file_source), "video/")) {
        $thumbnail_status = Thumbnail::getThumbnail($file_source,pathinfo($file_target, PATHINFO_DIRNAME),pathinfo($file_target, PATHINFO_FILENAME).'.jpg');
        if($thumbnail_status)
  {
    echo "Thumbnail generated";
  }
  else
  {
    echo "thumbnail generation has failed";
  }

        }

    else if(strstr(mime_content_type($file_source), "image/")) {
        if (\File::copy($file_source , $file_target)) {
              return $this->createThumbnail($file_target, $size, $size);
        }
        return null;
    }
}

private function createThumbnail($path, $width, $height) {
    $img = Image::make($path)->resize($width, $height, function ($constraint) {
        $constraint->aspectRatio();
    });
    $img->save($path);  
    return $img->response();
}