Serving m3u8/ts/vtt HLS files with Active Storage

An HLS video composed of a collection of .m3u8, .ts, .vtt files can be stored in a static bucket and served by referencing the master m3u8 URL. What would be the best way to modify this setup so that the files are uploaded, stored and served through Active Storage, considering that a single video may be composed of many files? Is there a way to ensure files remain grouped and with all the URLs for the individual files still correctly generated for distribution? How would this look in practice in the Active storage storage config and Rails model code?

One of the people on the Rails Slack has done something just like this. He has a popular webcast called https://driftingruby.com and he does a whole lot of behind-the-scenes work to split those videos down into multiple resolutions and time blocks for efficient streaming and hosting (kinda like how Netflix works). I think he may have one or more episodes that cover it, or he at least has linked to discussions of that topic in the past (the history of the Slack is truncated, because it’s on a free plan–and only occasionally, like now, on a pro plan because of some free trial or another–or I’d dig some out for you). Home · railslink/resources Wiki · GitHub

Walter

1 Like

Thanks Walter, perhaps this is the episode that you are referring to:

I didn’t check the Drifting Ruby video which requires a pro account, but I guess that the files are attached with has_many_attached and thus all the files will get mixed in the storage with the HLS files of other records. It’s a bit annoying as each video might have hundreds of files or more. Active Storage has a way to add a custom key (e.g. add a directory prefix) when calling .attach(...), but how to do so when using an upload form (same question as here)?

Do you want the files to remain individually accessible (e.g., .m3u8, .ts, .vtt), or are you looking for a way to group them together as a single entity in Active Storage?

@MarkAlpha I was looking for a way for the files to stay grouped in the storage, e.g in the same folder. They couldn’t be served as a single file I suppose, otherwise it would probably defeat the purpose of using a streaming format as the entire data would need to be downloaded at once instead of into chunks. On the other hand, if separate files are used, then it must at least be done in a way where the references to the files in the master m3u8 remains correct.