【问题标题】:How can I create a Video preview for an API only app如何为仅限 API 的应用创建视频预览
【发布时间】:2019-06-05 22:31:39
【问题描述】:

我有一个仅 API 的应用程序,它使用“fast_jsonapi”GEM 呈现 JSON 响应。

我设法使用 ActiveStorage 上传视频,并且可以在 JSON 响应中发送视频 URL。但我需要在同一个响应中发送一个缩略图 URL。

我查看了 Rails 文档和 Google,发现有一些方法可以在 Rails 应用程序上完成

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

但我找不到如何将其包含到仅 API 的应用程序中并在 JSON 响应中发送缩略图 URL。

现在这是我的序列化程序,我正在尝试在博客上找到的解决方案,但到目前为止它不起作用。我不确定这是否是正确的方法,或者我该如何解决这个问题。

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

【问题讨论】:

    标签: ruby-on-rails rails-api rails-activestorage video-thumbnails


    【解决方案1】:

    如果有人有兴趣,我设法得到一个解决方案

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

    并且在服务端需要安装FFmpeg和ImageMagick。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2011-10-25
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      相关资源
      最近更新 更多