ruby-on-railsrails-apirails-activestoragevideo-thumbnails

How can I create a Video preview for an API only app


I have an API only application that renders JSON responses with 'fast_jsonapi' GEM.

I managed to upload videos using ActiveStorage and I can send the video URL on the JSON response. But i need to send a Thumbnail URL on the same response.

I check on the Rails documentation and Google and I found that there are ways to do it on a Rails App

<ul>
  <% @message.files.each do |file| %>
    <li>
      <%= image_tag file.preview(resize_to_limit: [100, 100]) %>
    </li>
  <% end %>
</ul>

But I couldn't find how I can include that into an API only application and send the Thumbnail URL on the JSON response.

Right now this is my serializer where I was trying a solution found on a Blog, but so far it's not working. I'm not sure if this is the right approach or how can I address this problem.

class EjercicioSerializer
  include FastJsonapi::ObjectSerializer
  #include ActionView::AssetPaths
  include ActionView::Helpers::AssetTagHelper

  set_id :id
  attributes :nombre, :descripcion

  attribute :video_url do |object|
    Rails.application.routes.url_helpers.rails_blob_path(object.video, only_path: true) if object.video.attachment
  end

  attribute :video_thumbnail do |object|

    link_to(image_tag(object.video.preview(resize: "200x200>")),  
      Rails.application.routes.url_helpers.rails_blob_path(object.video, disposition: "attachment"))  

  end

end

Solution

  • If anyone is interested, I manage to get a Solution

    attribute :video_thumbnail do |object|
        Rails.application.routes.url_helpers.rails_representation_url(object.video.preview(resize: "200x200").processed, only_path: true)
    
    end
    
    

    And in the server FFmpeg and ImageMagick need to be installed.