Assuming the basic stuff in our hands, we can now move on to the development of the flash video playback
The basic functionality of the video app has been defined with the following inputs and outputs:
The user uploads the video using the user interface
The video is then converted into the flv format using the mencoder command line converter
The converted video is then played in the flv player in the browser output.
So, lets start off with the database stuff,the following is the basic structure of the database which I have been using for the demo
Just Posting the migration file.
class CreateVideos < ActiveRecord::Migration
def self.up
create_table :videos do |t|
t.column :video_data, :string
end
end
def self.down
drop_table :videos
end
end
This is just a basic database which saves the video_name and the id of the video.You can customize this file to save more info related to the video.
Now,First of all, I generate a controller named Upload Controller which handles the basic actions for video conversion
FileName:upload_controller.rb
class UploadController < ApplicationController
def convert
render :file => ‘app/views/upload/convert.rhtml’
end
The convert action render the input file for the video i.e the webpage from the video is supposed to be uploaded.
def show_video
if request.get?
@video = Video.new
else
@video = Video.new(params[:video])
if @video.save
upload = Video.convert_to_flv(@video)
@file_data = Video.find(:first, :select=>’DISTINCT id,video_data’,:conditions =>[’video_data=?’,@video[”video_data”]],:order => “id DESC”)
@video_data= “/video/video_data/”+ “#{@file_data[”id”].to_s }/”+ “#{@file_data[”video_data”]}.flv”
flash[:notice]=”Hey,Your video has been uploaded successfully”
render :file => ‘app/views/upload/show_video.rhtml’
else
flash[:notice]=”Hey,There was an error processing your video”
end
end
end
end
The show video is the action which handles the conversion of the input video and also the display of the video to the output page.
The action receives the parameters named video from the input page and before saving the video converts the video to the flv format
The convert_to_flv takes the @video as the parameter and converts it into the flv format which we will see later.
The @file data is nothing but a result set which holds the data of the query which gets the information related to the latest uploaded video by the user.
The @video_data passes the path to the video to the output flv player
FileName: convert.rb
<%= form_tag({:action=>’show_video’},{:multipart=>true}) %>
<%=file_column_field(”video”,”video_data”,:size=>”15″)%>
</p>
<p>
<input name=”convert_submit” type=”submit” value=”Convert_submit” id=”convert”>
</p>
<%=end_form_tag%>
Here I am using the file column plugin to upload the video and the form action pointing to the action show_video
FileName: show_video.rb
<%= javascript_include_tag ‘ufo’ %>
<h> Show Video </h></br>
<%=flash[:notice]%>
<% p “video data from the view”%>
<% p @video_data%>
<%=flv_player :file =>@video_data %>
The show video represents the output file of the flv player.The Javascript include tag includes the ufo.js which comes bundled with the flash player helper plugin and needs to be explicitly included in your application.
NOTE: The ufo.js can be included to application.rb when it comes to a development of the big app.
The flv_player tag adds an flv player to the page with the input.For more customization with respect to the player You can visit the farooq ali’s blog which I have mentioned in my previous post.
Now the last and the most important, The model file
FileName:Video.rb
class Video < ActiveRecord::Base
file_column :video_data
def self.convert_to_flv(upload)
full_name = upload.video_data
name_array = full_name.split(”/”)
name = name_array[name_array.length-1]
directory = “public/video/video_data/#{upload.id}”
# create the file path
path = File.join(directory, name)
if !name.nil?
convert_video_to_flv(path,name, directory)
end
end
The convert_to_flv takes the input video and strips out the filename from the parameter and creates a directory under the directory path given above with the id and saves the uploaded file there.
The uploaded file is then passed to the convert_video_yo_flv method which we will see below.
def self.convert_video_to_flv(path,name,video_id)
memdir =RAILS_ROOT + ‘/public/mplayer/’
opdir = RAILS_ROOT + “/#{video_id}/”+name+”.flv”
filepath = RAILS_ROOT + “/#{video_id}/”+name
system “#{memdir}mencoder #{filepath} -o #{opdir} -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames”
File.delete(”#{filepath}”)
end
end
The convert_video_to_flv takes the path, name and the video_id as the parameter and then uses the mencoder command line utility to convert the video to flv format.
And then deletes the original file after conversion ,which the user has uploaded ,mainly to save the server space.
I am also posting the following links with respect to the mencoder command which will help u take more control over the commanline
HOWTO Mencoder Introduction Guide
http://gentoo-wiki.com/HOWTO_Mencoder_Introduction_Guide
Basic usage of Mencoder
http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html
Encoding with the libavcodec codec family
http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-enc-libavcodec.html
Now,Before I hang up this post of mine, I would like mention a few checkpoints with respect to the conversion and the code also.
- The code if mine can be still refactored by keeping the model loose and placing the methods from to the upload_helper in your app and then calling those method conveniently before save.
- During the conversion of 3gp file to flv format, it may happen that the video may not play the sound.This is mainly because of the AMR sound codec,I have been working on this issue and will soon post the resolution as the AMR codec is been integrated with the mplayer.
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.