Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
Display uploaded videos with VideoJS

Display uploaded videos with VideoJS

Posted on August 1, 2021August 1, 2021 by Rails Hero

What is VideoJS ?

VideoJS is a client-side javascript library for displaying/showing/playing/embedding videos with a video player on the web browser, with controls such as volume controls, forward, skip, play, pause, etc. We can also display uploaded videos directly without even using this library as HTML5 already supports videos. But with videoJS we can have more controls for the video player and also we can customize the video player with skins custom buttons, etc.

VideoJS: https://videojs.com

Video Attachment

I will be using active storage to manage the attachments of my video model, you can use gems such as carrierwave, shrine, etc; if you don’t want to use active storage. As for me i like to use active storage because it comes out of the box with rails6, and also because it’s easier to implement. I’ll skip the part of setting up active storage, you can follow rails guides if you don’t know how to use active storage.

Active Storage Rails Guides: https://edgeguides.rubyonrails.org/active_storage_overview.html

The model

For my video model, i added two active storage attachments; one for the video thumbnails and the other for the actual video file. And then, I associated my video model with my user model.

#File: app/models/video.rb
class Video < ApplicationRecord
has_one_attached :video_thumb
has_one_attached :video_data
belongs_to :user
end

The controller

In my controller i permitted the new parameters for my attachments, i.e; video_thumb and video_data.

#File: app/controllers/videos_controller.rb
class VideosController < ApplicationController
#...Other methods
private
# Use callbacks to share common setup or constraints between actions.
def set_video
@video = Video.find(params[:id])
end

# Only allow a list of trusted parameters through.
def video_params
params.require(:video).permit(:title, :description, :video_data, :video_thumb, :user_id)
end
end

Adding VideoJS

To use videoJS we have to add the videoJS library to our rails apps, this can be done in many ways. But i wanted to make it quick and easy to use, so i added them directly in my views.

The VideoJS assets

In my video model’s show page i added the videoJS styesheet and javascript directly. It’s not the most efficient way, a better way would be to place it in your application layout. But, i wanted it to do it quickly so i added them directly.

#File: app/views/videos/show.html.erb 
#videoJS 
<link href="https://vjs.zencdn.net/7.11.4/video-js.css" rel="stylesheet" /> 
<script src="https://vjs.zencdn.net/7.11.4/video.min.js"></script>

Thumbnails

After that i added the video thumbnail(video_thumb) with a image variant of 640×350 and the main video file(video_data), this will resize the video thumbnail of any size to 640×350. I added the thumbnail to the poster attribute, the poster attribute displays the thumbnail in the video player.

#For Thumbnail
poster="<% if @video.video_thumb.present? %><%= @video.video_thumb.variant(resize_to_fill: [640, 350]).processed.service_url %><% end %>"

Video Source

To add the main video file/source, i just called for the service URL of the video source file. Please note that i added ‘@video.video_data.present?‘ to verify the presence of the attachment. Because, sometimes the files fail to upload, this usually happens with large files and end up with an empty record. This will result in our video’s show page to end up with an error.

#For Video Source
<source src="<% if @video.video_data.present? %> <%= @video.video_data.service_url %><% end %>" type="video/mp4" />

The Show Video Page

This is what everything looks like in the show page of my video. You can customize the video size, player height, width, etc, if you want to.

#File: app/views/videos/show.html.erb
#videoJS
<link href="https://vjs.zencdn.net/7.11.4/video-js.css" rel="stylesheet" />
<script src="https://vjs.zencdn.net/7.11.4/video.min.js"></script>
<video
id="my-video"
class="video-js"
controls
height= "350"
width="640"
preload="auto"
poster="<% if @video.video_thumb.present? %><%= @video.video_thumb.variant(resize_to_fill: [640, 350]).processed.service_url %><% end %>"
data-setup='{"fluid": true}'
>
<source src="<% if @video.video_data.present? %>
<%= @video.video_data.service_url %><% end %>" type="video/mp4" />
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a
web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video>

Video thumbnail Processing

As for the video thumbnails, i could process them in the background from the video file, but that would use up a lot of resources, and with HD or 4k videos, this could take up several hours to process, so instead i decided to just add video thumbnails manually by adding a video thumbnail attachment via active storage.

Player Demo/ Preview

Below is the demo of the videoJS player(video). Click to play video. The used a royalty free video from pexels.com for the demo. The link has been given below, if you want to download the video.

https://railshero.pw/wp-content/uploads/2021/07/railshero-time-square-videojs-demo.mp4

Royalty Free Video Source: https://www.pexels.com/video/footage-of-people-and-surroundings-of-the-busy-time-square-street-3202634/

Final Thoughts

With VideoJS we can even use various plugins like a playlist plugin or quality selector or even add watermarks. Also, to make the videos load faster i recommend using a CDN. And to upload large video files it will be better to use direct uploads with javascript libraries like dropzoneJS. Please checkout VideoJS documentation for the complete guide.

VideoJS Docs: https://docs.videojs.com

2 thoughts on “Display uploaded videos with VideoJS”

  1. homer davis says:
    August 28, 2021 at 11:10 pm

    any other js libs that do this in better ways ?

    Reply
    1. Rails Hero says:
      September 12, 2021 at 6:16 am

      plyr, indigo player. fluid player, shaka player, etc

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT