Video Streaming in Ruby on Rails

Dream your application like youtube? First of all, this application is only working in UNIX Machine, and has not been tested to Windows. This article is taken from written of johndahl.

First, install the gem:

sudo gem install rvideo
.

Next, install ffmpeg and (possibly) other related libraries. This is documented elsewhere on the web, and can be a headache. If you are on a Mac, the Macports build is reasonably good (though not perfect). Install with:

sudo port install ffmpeg
.

Or, for a better build (recommended), add additional video- and audio-related libraries, like this:

sudo port install ffmpeg +lame +libogg +vorbis +faac +faad +xvid +x264 +a52
.

Most package management systems include a build of ffmpeg, but many include a poor build. So you may need to compile from scratch.

If you want to create Flash Video files, also install flvtool2:

sudo gem install flvtool2
.

Once ffmpeg and RVideo are installed, you’re set.

You should remember the basic commands of RVIDEO and here they are :

file = RVideo::Inspector.new(:file => "#{FILE_PATH}/filename.mp4")
file.video_codec # => mpeg4
file.audio_codec # => aac
file.resolution # => 320x240


command = "ffmpeg -i $input_file -vcodec xvid -s $resolution$ $output_file$"
options = {
:input_file => "#{FILE_PATH}/filename.mp4",
:output_file => "#{FILE_PATH}/processed_file.mp4",
:resolution => "640x480"
}

transcoder = RVideo::Transcoder.new

transcoder.execute(command, options)

transcoder.processed.video_codec # => xvid



------------------------------------------------------
DEMONSTRATION OR EXAMPLE OF USAGE
------------------------------------------------------

  • First Create file by uploading the video to your "#{APP_ROOT}/files/file_name.video_ext", you can use act_as_attachment to do it. Example your file is input.mp4.
  • Then in your controller you can run file inspector object like script below:
file = RVideo::Inspector.new(:file => "#{APP_ROOT}/files/input.mp4")

file = RVideo::Inspector.new(:raw_response => @existing_response)

file = RVideo::Inspector.new(:file => "#{APP_ROOT}/files/input.mp4", :ffmpeg_binary => "#{APP_ROOT}/bin/ffmpeg")

file.fps # => "29.97"
file.duration # => "00:05:23.4"

  • To transcode video, you should initialize a trancoder object :
transcoder = RVideo::Transcoder.new
.
  • Then pass a command and valid options to the execute method.
recipe = "ffmpeg -i $input_file$ -ar 22050 -ab 64 -f flv -r 29.97 -s"
recipe += " $resolution$ -y $output_file$"
recipe += "\nflvtool2 -U $output_file$"
begin
transcoder.execute(recipe, {:input_file => "/path/to/input.mp4",
:output_file => "/path/to/output.flv", :resolution => "640x360"})
rescue TranscoderError => e
puts "Unable to transcode file: #{e.class} - #{e.message}"
end

  • If the job succeeds, you can access the metadata of the input and output files with:
transcoder.original # RVideo::Inspector object
transcoder.processed # RVideo::Inspector object
  • Or you can use another Template for you View:
<% if video.processing_status == 2 %>
<div id="videoplayer<%= video.id %>">
<p>This item requires Macromedia Flash Player 7.</p>
<p>To get the latest version please <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">click here</a>.</p>
</div>
<script type="text/javascript">
var so = new SWFObject("/flash/vidplayer.swf?filePath=<%= url_for_file_column(video, 'file') %>.flv&thumbPath=<%= url_for_file_column(video, "file")%>.jpg", "vidplayer", "336", "326", "7", "");
so.addParam("menu", "false");
so.write("videoplayer<%= video.id %>");
</script>
<strong><%= video.title.titleize %></strong><br />
<%= video.body %><br/>
<%= link_to image_tag("icon_video.gif"), url_for_file_column(video, "file") %>
<%= link_to "download original", url_for_file_column(video, "file") %>
<% else %>
<strong><%= video.title.titleize %></strong><br />
<%= video.body %><br/>
<%= link_to image_tag("icon_video.gif"), url_for_file_column(video, "file") %>
<%= link_to "download original", url_for_file_column(video, "file") %>
<p>This video is still being converted, or else there has been some kind of error.</p>
<% end %>

  • Make it simple and never think as frustration then you will success.

That's all explanation about RVideo. You can use RMovie here.

Need a demo script?

1 Response to "Video Streaming in Ruby on Rails"

html5 music player said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
html5 media player

 
powered by Blogger