使用Rails上传和转换视频

前提是需要安装FFMPEG,实现视频转换和上传我们需要一些插件


[size=large]Paperclip[/size]

没什么好多说,历史的选择,上传还是这个
[url=git://github.com/thoughtbot/paperclip.git]Github[/url]

[size=large]Acts As State Machine
[/size]

状态机支持,也经常用到
关于状态机,[url=http://www.iteye.com/topic/412610]这个讨论[/url]很有味道,建议了解

ruby script/plugin install


创建Model
First off, let’s create our video model. We’ll call it video. Inventive, eh?


ruby script/generate model video

可以看到代码生成如下:

class Video < ActiveRecord::Base
# Paperclip
# http://www.thoughtbot.com/projects/paperclip
has_attached_file :source

# Paperclip Validations
validates_attachment_presence :source
validates_attachment_content_type :source, :content_type => 'video/quicktime'
end


使用过Paperclip,会习惯用source保存上传的大文件
[size=large]添加状态[/size]


  # Acts as State Machine
# http://elitists.textdriven.com/svn/plugins/acts_as_state_machine
acts_as_state_machine :initial => :pending
state :pending
state :converting
state :converted, :enter => :set_new_filename
state :error

event :convert do
transitions :from => :pending, :to => :converting
end

event :converted do
transitions :from => :converting, :to => :converted
end

event :failed do
transitions :from => :converting, :to => :error
end



[size=large]运行 Migrations[/size]
migration文件如下:

class CreateVideos < ActiveRecord::Migration
def self.up
create_table :videos do |t|
t.string :source_content_type
t.string :source_file_name
t.integer :source_file_size
t.string :state
t.timestamps
end
end

def self.down
drop_table :videos
end
end


其中source_ 是用来给Paperclip上传文件, state字段是给acts_as_state_machine

运行命令如下:

rake db:migrate

[size=large]
添加转换方法[/size]


在 video.rb文件中添加如下方法用来处理转换

# This method is called from the controller and takes care of the converting
def convert
self.convert!
success = system(convert_command)
if success && $?.exitstatus == 0
self.converted!
else
self.failure!
end
end

protected

# This method creates the ffmpeg command that we'll be using
def convert_command
flv = File.join(File.dirname(source.path), "#{id}.flv")
File.open(flv, 'w')

command = <<-end_command
ffmpeg -i #{ source.path } -ar 22050 -ab 32 -acodec mp3
-s 480x360 -vcodec flv -r 25 -qscale 8 -f flv -y #{ flv }
end_command
command.gsub!(/\s+/, " ")
end

# This update the stored filename with the new flash video file
def set_new_filename
update_attribute(:source_file_name, "#{id}.flv")
end


[size=large]控制器中[/size]

控制器将如下,上传的view比较容易,唯一注意的是应该给form注明:multipart => true

class VideosController < ApplicationController
def index
@videos = Video.find :all
end

def new
@video = Video.new
end

def create
@video = Video.new(params[:video])
if @video.save
@video.convert
flash[:notice] = 'Video has been uploaded'
redirect_to :action => 'index'
else
render :action => 'new'
end
end

def show
@video = Video.find(params[:id])
end
end


[quote]
After the video is saved in the create method, the convert method is called. This should convert the video to a flash video file, update the database entry and set the state of the model. The state will be set to converted if everything went to plan, or error if everything went to shit.
[/quote]
完整的video.rb文件可以看[url=http://dl.iteye.com/topics/download/9d2c0480-98fa-338c-89b1-bbd4e5d9562c]这里[/url]

[size=large]在页面显示视频[/size]


显示视频的方案当前有两个

[url=http://blog.deconcept.com/swfobject/]SWFObject[/url]

[url=http://www.jeroenwijering.com/?item=JW_FLV_Media_Player]JW FLV Media Player[/url]

前者是javascript的嵌入Adobe Flash,优势是搜索支持,后者支持多种转换格式FLV, but also MP3, H264, SWF, JPG, PNG and GIF)。


介绍后一个要加载 swfobject.js文件

<%= javascript_include_tag 'swfobject' %>


将mediaplayer.swf放到public/flash/mediaplayer.swf

<div id="player">
You need to have <%= link_to 'Flash Player', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' %> installed to display this video
</div>

<script type="text/javascript">
var so = new SWFObject('/flash/mediaplayer.swf', 'mpl', '480', '360', '8');
so.addParam('allowscriptaccess', 'always');
so.addParam('allowfullscreen', 'true');
so.addVariable('height', '360');
so.addVariable('width', '480');
so.addVariable('file', '<%= @video.source.url %>');
so.write('player');
</script>



[quote]The contents of the div will be replaced with the flash player, if flash isn't installed the user is shown a message with a link to download flash player. The <%= @video.source.url %> line will output the path to the flv video file.[/quote]

[url=http://www.jeroenwijering.com/?page=wizard]标准的JW FLV Player安装介绍[/url]


最后,

[url=http://hlee.iteye.com/admin/blogs/747086]使用rails转换视频之 安装FFMPEG[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值