Can you make Video variants, transformations of video, with ActiveStorage?

Viewed 560

ActiveStorage seems to be mostly focused on images. While it does offer "preview" support to get a thumbnail from a video -- what about "variants"?

With images, maybe it's uploaded as a JPG, but I can use the variants feature to convert to a PNG, and/or resize it, whatever.

But what if I want to do something similar with videos? Let's say the video was uploaded as an mp4, but I want to transform it into webm, or vice versa, or downsample it, or whatever.

How would one do this if one wanted to use ActiveStorage to keep track of the original uploaded file?

1 Answers

I needed something to convert quicktime (mov) files into mp4.

This works locally, but uses way too much memory in production to be useful.

Still, it seems to work, so may help you it you have a lot of RAM to spare.

Example: for a ~4.4mb video, it uses >1GB RAM to transcode it!

# Transcoding quicktime to mp4
if post_params[:files][1].content_type == "video/quicktime"

  # Transcode
  require 'streamio-ffmpeg'
  movie = FFMPEG::Movie.new(post_params[:files][1].tempfile.path)

  # Write new movie
  require 'tempfile'
  tempfile = Tempfile.new(["", ".mp4"]) 
  movie.transcode(tempfile.path)

  # Update tempfile path in ActiveStorage
  post_params[:files][1].tempfile = tempfile

  # Update headers
  post_params[:files][1].headers = post_params[:files][1].headers.sub("\nContent-Type: video/quicktime", "\nContent-Type: video/mp4")

end

So what I intend to do is offload these workloads to AWS lambdas, which could be triggered to run when an new file arrives in ActiveStorage.

I will update if I get it working successfully.

Related