Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activestorage integration? #74

Open
adenta opened this issue Dec 7, 2023 · 7 comments
Open

Activestorage integration? #74

adenta opened this issue Dec 7, 2023 · 7 comments

Comments

@adenta
Copy link

adenta commented Dec 7, 2023

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
Copy link
Contributor

dylanjha commented Dec 7, 2023

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
Copy link
Author

adenta commented Dec 7, 2023 via email

@dylanjha
Copy link
Contributor

dylanjha commented Dec 7, 2023

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
Copy link
Author

adenta commented Dec 7, 2023 via email

@dylanjha
Copy link
Contributor

dylanjha commented Dec 7, 2023

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?

@adenta
Copy link
Author

adenta commented Dec 7, 2023 via email

@dylanjha
Copy link
Contributor

dylanjha commented Dec 7, 2023

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:

  • The original video file (has_one_attached)
  • Any application metadata specific to that video (title, description, etc..)
  • Any Mux metadata specific to that video that you'll need (asset_id, playback_id, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants