muxinc / mux-ruby

Official Mux API wrapper for ruby projects, supporting both Mux Data and Mux Video.
MIT License
51 stars 14 forks source link

Activestorage integration? #74

Open adenta opened 11 months ago

adenta commented 11 months ago

Curious if the mux team has put any thought into integrating mux with activestorage. It is my understanding Mux only supports videos (no images), so probably does not make sense to build an activestorage adapter. Would be interested to hear if anyone has thought about this, and the best way to store videos in activestorage, and then mirror them to mux for fast playback.

dylanjha commented 11 months ago

Hey @adenta -- good question.

I do have some thoughts on this, let me know how this sounds. The rough workflow that you could do today is:

  1. Save video in active storage (just like you would with any file)
  2. Make API call to Mux using input with a URL to the video
class Video
  has_one_attached :file
end

# assuming this video already has a video file attached
video = Video.find(id)

assets_api = MuxRuby::AssetsApi.new
create_asset_req = MuxRuby::CreateAssetRequest.new
create_asset_req.playback_policy = [MuxRuby::PlaybackPolicy::PUBLIC]
create_asset_req.input = Rails.application.routes.url_helpers.rails_blob_url(video.file)
create_response = assets_api.create_asset(car)
#
# you'll want to save stuff in the create_response like asset `id`, `playback_id`, `duration`, `aspect_ratio`
# and you can set # up webhooks so you get notified when Mux is done processing the video
#

The important bit here is that the input value is set to the full URL path of the video. When Mux API receives that create asset request it will download the video file from the input URL.

Let me know if that helps or if you have any other ideas that could make this easier.

adenta commented 11 months ago

Where do you think it makes sense to save the playback id? The blob? The attachment? The record?

Wondering if there is a gem here to hide the complexity behind

On Thu, Dec 7, 2023 at 1:33 PM Dylan Jhaveri @.***> wrote:

Hey @adenta https://github.com/adenta -- good question.

I do have some thoughts on this, let me know how this sounds. The rough workflow that you could do today is:

  1. Save video in active storage (just like you would with any file)
  2. Make API call to Mux using input with a URL to the video

class Video has_one_attached :fileend

assuming this video already has a video file attachedvideo = Video.find(id)

assets_api = MuxRuby::AssetsApi.newcreate_asset_req = MuxRuby::CreateAssetRequest.newcreate_asset_req.playback_policy = [MuxRuby::PlaybackPolicy::PUBLIC]create_asset_req.input = Rails.application.routes.url_helpers.rails_blob_url(video.file)create_response = assets_api.create_asset(car)## you'll want to save stuff in the create_response like asset id, playback_id, duration, aspect_ratio# and you can set # up webhooks so you get notified when Mux is done processing the video#

The important bit here is that the input value is set to the full URL path of the video. When Mux API receives that create asset request it will download the video file from the input URL.

Let me know if that helps or if you have any other ideas that could make this easier.

— Reply to this email directly, view it on GitHub https://github.com/muxinc/mux-ruby/issues/74#issuecomment-1845900624, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGUU5SHSNR6VLNMIGXEQLTYIIDWNAVCNFSM6AAAAABALOYUW6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBVHEYDANRSGQ . You are receiving this because you were mentioned.Message ID: @.***>

dylanjha commented 11 months ago

Good q.

You'd want to save that on the model so that it's in your database. You'll need that playback_id in order to do playback on the client side.

adenta commented 11 months ago

Which model?

The record from the has_one_attached? ActiveStorage::Attachment? ActiveStorage::Blob?

On Thu, Dec 7, 2023 at 1:38 PM Dylan Jhaveri @.***> wrote:

Good q.

You'd want to save that on the model so that it's in your database. You'll need that playback_id in order to do playback on the client side.

— Reply to this email directly, view it on GitHub https://github.com/muxinc/mux-ruby/issues/74#issuecomment-1845907726, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGUU5WUS47U7CEVV2GOJK3YIIEJJAVCNFSM6AAAAABALOYUW6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBVHEYDONZSGY . You are receiving this because you were mentioned.Message ID: @.***>

dylanjha commented 11 months ago

You'd want to save extra metadata on the model that has_one_attached.

I would recommend something like this:

The Mux-specific metadata sits alongside the other metadata like title, description, etc. All of these pieces will be used when you show the Video on the client.

Does that help?

adenta commented 11 months ago

Yeah and I think I agree but it has one attached case. It gets tricky for the has many attached case, but I can't think of a good solution.

Because it seems pretty jank to edit the active storage tables

On Thu, Dec 7, 2023, 1:53 PM Dylan Jhaveri @.***> wrote:

You'd want to save extra metadata on the model that has_one_attached.

I would recommend something like this:

  • You have a model Video. The Video might have a title, a description, tags, a user_id that points to the user who created the video
  • The Video model also has_one_attached :file, which points to the raw uploaded video file
  • The Video model also has metadata associated with the Mux Asset: asset_id, playback_id, asset_status

The Mux-specific metadata sits alongside the other metadata like title, description, etc. All of these pieces will be used when you show the Video on the client.

Does that help?

— Reply to this email directly, view it on GitHub https://github.com/muxinc/mux-ruby/issues/74#issuecomment-1845928339, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGUU5W6XF6FKYONJ3TDSALYIIGCNAVCNFSM6AAAAABALOYUW6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBVHEZDQMZTHE . You are receiving this because you were mentioned.Message ID: @.***>

dylanjha commented 11 months ago

I definitely wouldn't edit the active storage tables. If you have a model that has many videos I would do something like the following:

class VideoSeries
  has_many :videos
  belongs_to :user
end

class Video
  belongs_to :video_series
  has_one_attached :file
end

And from there you can follow the same pattern. You key here is that you have 1 model (in this example, Video) that keeps track of: