├── .gitignore
├── Gemfile
├── Rakefile
├── lib
├── videojs_rails.rb
└── videojs_rails
│ ├── engine.rb
│ ├── railtie.rb
│ ├── tags.rb
│ ├── tags
│ ├── caption.rb
│ ├── source.rb
│ ├── tag.rb
│ └── video.rb
│ ├── version.rb
│ └── view_helpers.rb
├── readme.md
├── vendor
└── assets
│ ├── flash
│ └── video-js.swf
│ ├── fonts
│ ├── vjs.eot
│ ├── vjs.svg
│ ├── vjs.ttf
│ └── vjs.woff
│ ├── javascripts
│ ├── video-js.swf
│ └── video.js.erb
│ └── stylesheets
│ └── video-js.css.erb
└── videojs_rails.gemspec
/.gitignore:
--------------------------------------------------------------------------------
1 | *.gem
2 | .bundle
3 | Gemfile.lock
4 | pkg/*
5 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "http://rubygems.org"
2 |
3 | gem "rails", "3.1.0.rc4"
4 | gem "capybara", "~> 1.0.0"
5 | gem "sqlite3"
6 |
7 | # Asset template engines
8 | gem 'sprockets', '2.0.0.beta.10'
9 | gem 'sass'
10 | gem 'coffee-script'
11 | gem 'uglifier'
12 | gem 'nokogiri'
13 |
14 | gem 'jquery-rails'
15 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'bundler/gem_tasks'
2 |
3 | # Build the version of video.js and tag us to match
4 |
5 | VIDEO_JS_RAILS_HOME = File.expand_path(File.dirname(__FILE__))
6 | VIDEO_JS_HOME = File.expand_path('../video.js', VIDEO_JS_RAILS_HOME)
7 |
8 | VIDEO_JS_RAKE_USAGE = "Usage: rake videojs:update TAG=v4.12.5"
9 |
10 | namespace :videojs do
11 | task :update => [:build, :commit]
12 |
13 | task :build do
14 | tag = ENV['TAG'] or abort VIDEO_JS_RAKE_USAGE
15 | Dir.chdir(VIDEO_JS_HOME) do
16 | puts "* Building video.js #{tag} using grunt"
17 | unless ENV['NOBUILD']
18 | sh "git checkout -q master"
19 | sh "git pull -q"
20 | sh "git checkout -q #{tag}"
21 | sh "grunt"
22 | end
23 |
24 | # Copy files into our Rails structure
25 | puts
26 | puts "* Copying files to vendor/assets"
27 | sh "cp #{VIDEO_JS_HOME}/dist/video-js/font/* #{VIDEO_JS_RAILS_HOME}/vendor/assets/fonts/"
28 | sh "cp #{VIDEO_JS_HOME}/dist/video-js/video-js.css #{VIDEO_JS_RAILS_HOME}/vendor/assets/stylesheets/"
29 | sh "cp #{VIDEO_JS_HOME}/dist/video-js/video-js.swf #{VIDEO_JS_RAILS_HOME}/vendor/assets/javascripts/"
30 | sh "cp #{VIDEO_JS_HOME}/dist/video-js/video.dev.js #{VIDEO_JS_RAILS_HOME}/vendor/assets/javascripts/"
31 |
32 | # Now, perform some asset_path and other substitutions
33 | puts
34 | puts "* Updating videojs-css.erb for Rails asset pipeline"
35 | css = "#{VIDEO_JS_RAILS_HOME}/vendor/assets/stylesheets/video-js.css"
36 | File.open("#{css}.erb", 'w') do |out|
37 | File.foreach(css) do |line|
38 | # Handle fonts => url('<%= asset_path('vjs.woff') %>') format('woff')
39 | out <<
40 | line.gsub(/url\(('*)font\/(vjs[^\)]+)\)(\s+format[^\)]+\))?/, 'url(<%= asset_path(\1\2) %>)\3')
41 | end
42 | end
43 | sh "rm -f #{css}"
44 |
45 | puts
46 | puts "* Updating video.js.erb for Rails asset pipeline"
47 | jsdev = "#{VIDEO_JS_RAILS_HOME}/vendor/assets/javascripts/video.dev.js"
48 | jserb = "#{VIDEO_JS_RAILS_HOME}/vendor/assets/javascripts/video.js.erb"
49 | File.open(jserb, 'w') do |out|
50 | File.foreach(jsdev) do |line|
51 | # Handle swf => asset_path('video-js.swf')
52 | out <<
53 | line.sub(/(videojs\.options\['flash'\]\['swf'\]\s*=\s*).*/, %q(\1"<%= asset_path('video-js.swf') %>";))
54 | end
55 | end
56 | sh "rm -f #{jsdev}"
57 | sh "rm -f #{VIDEO_JS_RAILS_HOME}/vendor/assets/javascripts/video.js"
58 | end
59 | end
60 |
61 | task :commit do
62 | tag = ENV['TAG'] or abort VIDEO_JS_RAKE_USAGE
63 |
64 | # Update the gem version
65 | version_file = "#{VIDEO_JS_RAILS_HOME}/lib/videojs_rails/version.rb"
66 | lines = File.read(version_file)
67 | File.open(version_file, 'w') do |out|
68 | version_num = tag.sub(/^v/,'') # lose the "v" from the tag
69 | puts "* Setting gem version = #{version_num}"
70 | out << lines.sub(/(VERSION\s*=\s*)\S+/, "\\1'#{version_num}'")
71 | end
72 |
73 | sh "git add ."
74 | sh "git commit -m 'Update to video.js #{tag}'"
75 | puts "* Done. Now run 'rake release' to push to rubygems."
76 | end
77 | end
--------------------------------------------------------------------------------
/lib/videojs_rails.rb:
--------------------------------------------------------------------------------
1 | require 'videojs_rails/tags'
2 | require 'videojs_rails/railtie'
3 | require 'videojs_rails/engine' if defined?(Rails && Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >=1)
4 |
--------------------------------------------------------------------------------
/lib/videojs_rails/engine.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module Rails
3 | class Engine < ::Rails::Engine
4 | end
5 | end
6 | end
7 |
--------------------------------------------------------------------------------
/lib/videojs_rails/railtie.rb:
--------------------------------------------------------------------------------
1 | require 'videojs_rails/view_helpers'
2 |
3 | module VideojsRails
4 | class Railtie < Rails::Railtie
5 | initializer "videojs_rails.view_helpers" do
6 | ActionView::Base.send(:include, ViewHelpers)
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/videojs_rails/tags.rb:
--------------------------------------------------------------------------------
1 | require 'videojs_rails/tags/tag'
2 | require 'videojs_rails/tags/video'
3 | require 'videojs_rails/tags/caption'
4 | require 'videojs_rails/tags/source'
5 |
6 | module VideojsRails
7 | module Tags
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/videojs_rails/tags/caption.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module Tags
3 | class Caption < Tag
4 | def initialize(lang, default_language, options)
5 | @lang = lang.to_sym
6 | @default_language = default_language
7 | parse_options(options)
8 | end
9 |
10 | def to_html
11 | tag :track, attributes
12 | end
13 |
14 | private
15 |
16 | attr_reader :src, :label, :lang, :default_language
17 |
18 | def default?
19 | default_language == lang
20 | end
21 |
22 | def parse_options(options)
23 | case options
24 | when String, Symbol
25 | @src = options
26 | when Hash
27 | @src, @label = options.values_at(:src, :label)
28 | else
29 | raise ArgumentError
30 | end
31 | end
32 |
33 | def attributes
34 | {
35 | kind: :captions,
36 | src: src,
37 | srclang: lang,
38 | label: label,
39 | default: default?
40 | }
41 | end
42 | end
43 |
44 | end
45 | end
46 |
--------------------------------------------------------------------------------
/lib/videojs_rails/tags/source.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module Tags
3 | class Source < Tag
4 | def initialize(video_type, options)
5 | @video_type = video_type
6 | @attributes = parse_options(options)
7 | end
8 |
9 | def to_html
10 | tag :source, @attributes
11 | end
12 |
13 | private
14 |
15 | attr_reader :video_type
16 |
17 | def parse_options(options)
18 | case options
19 | when String, Symbol
20 | {
21 | src: options,
22 | type: type
23 | }
24 | when Hash
25 | options
26 | else
27 | raise ArgumentError
28 | end
29 | end
30 |
31 | def type
32 | "video/#{video_type}"
33 | end
34 | end
35 | end
36 | end
37 |
--------------------------------------------------------------------------------
/lib/videojs_rails/tags/tag.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module Tags
3 | class Tag
4 | include ActionView::Helpers::TagHelper
5 | include ActionView::Context
6 | include ActionView::Helpers::TextHelper
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/videojs_rails/tags/video.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module Tags
3 | class Video < Tag
4 | DEFAULT_LANGUAGE_KEY = :default_caption_language
5 | DEFAULT_OPTIONS = {
6 | controls: true,
7 | preload: :auto
8 | }.freeze
9 |
10 | def initialize(options, &no_js)
11 | @no_js = no_js
12 | prepare_options(options)
13 | end
14 |
15 | def to_html
16 | content_tag(:video, options) do
17 | sources.each {|type, options| concat Source.new(type, options).to_html }
18 | captions.each {|lang, options| concat Caption.new(lang, default_caption_language, options).to_html }
19 | generate_no_js
20 | end
21 | end
22 |
23 | private
24 |
25 | attr_reader :no_js, :sources, :captions, :options, :default_caption_language
26 |
27 | def prepare_options(user_options)
28 | @sources = user_options.delete(:sources) || []
29 | @captions = user_options.delete(:captions) || []
30 | @default_caption_language = user_options.delete(DEFAULT_LANGUAGE_KEY)
31 | @options = DEFAULT_OPTIONS.merge(user_options)
32 | options[:'data-setup'] = options.delete(:setup) if options.key?(:setup)
33 | options[:class] = [:'video-js', :'vjs-default-skin', *options[:class]]
34 | end
35 |
36 | def generate_no_js
37 | return if no_js.nil?
38 | concat content_tag(:p, no_js.call(), class: :'vjs-no-js')
39 | end
40 | end
41 | end
42 | end
43 |
--------------------------------------------------------------------------------
/lib/videojs_rails/version.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | VERSION = '4.12.15'
3 | end
4 |
--------------------------------------------------------------------------------
/lib/videojs_rails/view_helpers.rb:
--------------------------------------------------------------------------------
1 | module VideojsRails
2 | module ViewHelpers
3 | def videojs_rails(user_options, &blk)
4 | VideojsRails::Tags::Video.new(user_options, &blk).to_html
5 | end
6 | end
7 | end
8 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # VideoJS for Rails Asset Pipeline
2 |
3 | Supports Rails 3.x and 4.x
4 |
5 | ## Installation
6 |
7 | Add to your Gemfile
8 |
9 | ```ruby
10 | gem 'videojs_rails'
11 | ```
12 |
13 | And run bundle to install the library.
14 |
15 | ```ruby
16 | bundle
17 | ```
18 |
19 | Add the resources to your application.js file
20 |
21 | ```coffeescript
22 | # app/assets/javascripts/application.js
23 | //= require video
24 | ```
25 |
26 | And that resource to application.css file
27 |
28 | ```sass
29 | /*
30 | *= require_self
31 | *= require video-js
32 | */
33 | ```
34 |
35 | And to production.rb add this line
36 |
37 | ```ruby
38 | config.assets.precompile += %w( video-js.swf vjs.eot vjs.svg vjs.ttf vjs.woff )
39 | ```
40 |
41 | In Rails > 4.1
42 | Add this line to config/initializers/assets.rb
43 |
44 | ```ruby
45 | Rails.application.config.assets.precompile += %w( video-js.swf vjs.eot vjs.svg vjs.ttf vjs.woff )
46 | ```
47 |
48 | ## Usage
49 |
50 | ```erb
51 | <%= videojs_rails sources: { mp4: "http://domain.com/path/to/video.mp4", webm: "http://another.com/path/to/video.webm"}, setup: "{}", controls: false, width:"400" %>
52 | ```
53 |
54 | If you want add a callback if user don't support JavaScript use block with displayed html code:
55 |
56 | ```erb
57 | <%= videojs_rails sources: { mp4: "http://domain.com/path/to/video.mp4", webm: "http://another.com/path/to/video.webm" }, width:"400" do %>
58 | Please enable JavaScript to see this content.
59 | <%- end %>
60 | ```
61 |
62 | ## Captions
63 |
64 | This is currently an experimental function.
65 |
66 | ```erb
67 | <%= videojs_rails sources: { mp4: "http://domain.com/path/to/video.mp4" }, width:"400", captions: { en: { src: "http://domain.com/path/to/captions.vvt", label: "English" }, default_caption_language: :en } %>
68 | ```
69 |
70 | ## Turbolinks
71 |
72 | Some of you might want to use VideoJS with Turbolinks. [andrkrn](https://github.com/andrkrn) provided CoffeeScript that he use:
73 |
74 | ```coffeescript
75 | change = ->
76 | for player in document.getElementsByClassName 'video-js'
77 | video = videojs('example_video')
78 |
79 | before_change = ->
80 | for player in document.getElementsByClassName 'video-js'
81 | video = videojs('example_video')
82 | video.dispose()
83 |
84 | $(document).on('page:before-unload', before_change)
85 | $(document).on('page:change', change)
86 | ```
87 |
88 | ## Resources
89 | http://videojs.com/
90 | http://videojs.com/#getting-started
91 |
92 |
93 | ## Updating this gem to the latest video.js release
94 |
95 | ### Clone this repository
96 |
97 | git clone https://github.com/seanbehan/videojs_rails.git
98 |
99 | ### Clone video.js repository
100 |
101 | git clone https://github.com/videojs/video.js.git
102 |
103 |
104 | ### Run the rake videojs:update task with the tag
105 |
106 | TAG=v4.12.5
107 | rake videojs:update
108 |
109 | Note: The build will fail if you don't have `grunt` installed. To install it:
110 |
111 | cd ../video.js
112 | npm install -g grunt
113 |
114 | ### Make sure everything is added to git
115 |
116 | git add .
117 | git ci -m "Update to $TAG"
118 |
119 | ### Push to rubygems
120 |
121 | * $VIDEO_JS_RAILS_HOME/vendor/assets/javascripts/video.js.erb
122 | * $VIDEO_JS_RAILS_HOME/vendor/assets/stylesheets/video-js.css.erb
123 |
124 | Alternatively, you can set the Flash player SWF file in your web view with the `videojs.options.flash.swf` command:
125 | ```
126 |
129 | ```
130 | As the instructions here suggests: https://github.com/videojs/video.js/blob/stable/docs/guides/setup.md#self-hosted
131 |
--------------------------------------------------------------------------------
/vendor/assets/flash/video-js.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seanbehan/videojs_rails/771a0a51f14dc7a72e0291a56d3d8b7506bf9fe4/vendor/assets/flash/video-js.swf
--------------------------------------------------------------------------------
/vendor/assets/fonts/vjs.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seanbehan/videojs_rails/771a0a51f14dc7a72e0291a56d3d8b7506bf9fe4/vendor/assets/fonts/vjs.eot
--------------------------------------------------------------------------------
/vendor/assets/fonts/vjs.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/vendor/assets/fonts/vjs.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seanbehan/videojs_rails/771a0a51f14dc7a72e0291a56d3d8b7506bf9fe4/vendor/assets/fonts/vjs.ttf
--------------------------------------------------------------------------------
/vendor/assets/fonts/vjs.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seanbehan/videojs_rails/771a0a51f14dc7a72e0291a56d3d8b7506bf9fe4/vendor/assets/fonts/vjs.woff
--------------------------------------------------------------------------------
/vendor/assets/javascripts/video-js.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/seanbehan/videojs_rails/771a0a51f14dc7a72e0291a56d3d8b7506bf9fe4/vendor/assets/javascripts/video-js.swf
--------------------------------------------------------------------------------
/vendor/assets/stylesheets/video-js.css.erb:
--------------------------------------------------------------------------------
1 | /*!
2 | Video.js Default Styles (http://videojs.com)
3 | Version 4.12.15
4 | Create your own skin at http://designer.videojs.com
5 | */
6 | /* SKIN
7 | ================================================================================
8 | The main class name for all skin-specific styles. To make your own skin,
9 | replace all occurrences of 'vjs-default-skin' with a new name. Then add your new
10 | skin name to your video tag instead of the default skin.
11 | e.g.