Download video from the URL and get video information in Rails 7
- Nobody
- Jan 16
- 2 min read
In this guide, I will show you how to build an application to fetch video information and download videos via a URL.
To do this, we will use the yt-dlp command-line tool for extracting video details and downloading content.
Important
Please use code provided in this tutorial responsibly and in accordance with the terms of service of the video hosting platforms you access. Ensure compliance with copyright laws by downloading only content you have the right to access or permission to use.
Setting up Ruby on Rails project
Create new rails project and add ytdl gem to your Gemfile
Installing yt-dlp
How to Install yt-dlp Locally on Linux (Optional)
If you're using Linux, you can download the yt-dlp script and place it in your project's root/bin directory. Next, configure the executable path for the ytdl Ruby library by creating a config/initializers/ytdlp.rb file and specifying the path to your local yt-dlp executable.
For MacOs and Windows users
Please follow instructions for your platform on the project page.
Downloading the Video
Create a VideoDownloadController class with a create action to handle the video download functionality.
Add new action to the routes.rb
Then navigate to http://127.0.0.1:3000/download, and you should see json response with the downloaded video file name.
Extracting Video Information Without Downloading
To extract video details without downloading, we need to pass additional arguments to the YoutubeDL#download method.
Create the info action in the VideoDownloadController class with the following code:
Let's Review the info method:
We use the same YoutubeDL#download` method, but with the `download: false` option. This option tells yt-dlp to skip the actual file download and only fetch metadata.
After the download process completes, we need to load the JSON data from the output file.
Once the JSON is loaded, you can use the info method to access the actual video properties and details.
Next register the info action in your routes.rb file
Then navigate to http://127.0.0.1:3000/video-info, and you should see json response with the video information.
That's it!
If you want to add more functionality, you can simply create a new method with a different set of options.
留言