Ruby on Rails: download YouTube, TikTok and Instagram videos
In Rails, a plain Ruby service object calls CrawlFetch, and an ActiveJob attaches the downloaded file to an Active Storage record.
How it fits together
Controllers enqueue a job with the source URL; the job resolves it, downloads the file and attaches it — users never wait on a large download.
Ruby on Rails example
require "open-uri"class SaveVideoJob < ApplicationJobretry_on Net::HTTPError, wait: :polynomially_longer, attempts: 5def perform(video)data = CrawlFetch.resolve(video.source_url) # see the Ruby guide for the clientURI.parse(data["downloadUrl"]).open do |io|video.file.attach(io: io, filename: "#{data["id"]}.mp4", content_type: "video/mp4")endvideo.update!(title: data["title"])endend
Notes for Ruby on Rails
- Store the key in Rails credentials:
Rails.application.credentials.crawlfetch_key. - Use Solid Queue or Sidekiq for the job backend.
FAQ
Where is the CrawlFetch client?
Copy the resolve method from the Ruby guide into app/services/crawl_fetch.rb.
Related
Last updated 2026-10-25