如何使用回形针加载和验证视频-Rails
how to load and validate the video using paperclip-Rails
请帮忙解决问题
我用回形针整理上传图片。它有效。
现在我想组织一个视频上传。我改变了这个模型:
型号:
class Video < ActiveRecord::Base
validates :title, presence: true
validates :video, presence: true
belongs_to :user
has_attached_file :video,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/
validates_attachment_file_name :video, :matches => [/3gp\Z/, /mp4\Z/, /flv\Z/]
validate :file_size_validation, :if => "video?"
def file_size_validation
errors[:video] << "should be less than 2MB" if video.size.to_i > 30.megabytes
end
end
视频控制器:
def create
@video = Video.new(video_params)
if @video.save
@video.update_attributes(user: current_user)
flash[:success] = :video_created
redirect_to @video
else
flash.now[:error] = :user_not_created
render 'new'
end
end
形式:
<%= form_for(@video) do |f| %>
<%= f.text_field :title %>
<%= f.file_field :video %>
<%= f.submit %>
<% end %>
尝试上传视频后,我在控制台收到以下错误消息:
Started POST "/videos" for 127.0.0.1 at 2015-07-23 14:17:32 +0300
Processing by VideosController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"RF3w8PzZ9LZ4pdqRPqRvHMQ+nYDL4om0YHAMlzTm8tf3iFmVmKxXSYKap3C0ogEnOkifm9i01oXs/rTX9zmtPA==", "video"=>{"title"=>"tty", "video"=>#<ActionDispatch::Http::UploadedFile:0x007f6624f10770 @tempfile=#<Tempfile:/tmp/RackMultipart20150723-13428-u7s8i8.flv>, @original_filename="mmm.flv", @content_type="video/x-flv", @headers="Content-Disposition: form-data; name=\"video[video]\"; filename=\"mmm.flv\"\r\nContent-Type: video/x-flv\r\n">}, "commit"=>"Create Video"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 8]]
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-6gn39i.flv'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
(0.2ms) BEGIN
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-khstwa.flv'
(0.2ms) ROLLBACK
屏幕上显示以下错误消息:
Video Paperclip::Errors::NotIdentifiedByImageMagickError
其中imageMagik安装在我的系统中:
sudo apt-get install imagemagick
db:
create_table "videos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.integer "user_id"
t.string "video_file_size"
t.string "video_updated_at"
t.string "video_file_name"
t.string "video_content_type"
end
paperclip
的处理器是为图像设计的,所以它不能处理视频。您收到错误消息,指出 ImageMagick 未将此文件识别为图像。很公平,这不是图像!
不过,您可以自己编写一个 post 处理器,以及 paperclip
's github page offers an example 调用自定义处理所需的语法 class:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]
这定义了名为 text
的附件变体,它是通过使用(假设的)Paperclip::Ocr
class 和选项散列 { :quality => :better }
[ 处理上传的文件获得的=17=]
具体转换工具的选择由您决定,但您确实希望确保处理是异步完成的,而不是在通常的请求-响应周期内完成,您可能需要使用像 Sidekiq、Que 这样的后台队列或 Rails' (4.2+) ActiveJob。
使用 image
与 paperclip
和 video
是 different.Though 共同点是它会帮助您将上传的数据发送到服务器,但您需要用一些 processor.Just 来处理它,就像你对 images
使用 ImageMagick
一样,你应该使用 FFMPEG
到 encode/decode video
。
我将分享我通常用于视频的代码..但在此之前,您必须设置 ffmpeg
来处理所有格式的视频,就像您为 Imagemagick
所做的那样。
here 是为 ffmpeg 设置的,不要忘记使用 which ffmpeg
在环境文件中提供路径
您还需要 ffmpeg-paperclip 以及 coding/decoding 个视频。
video.rb
##using s3
##convert the video into mp4 and also get screenshot of video at 5 sec
##add your own formats that you want
has_attached_file :video,
:styles => {
:mp4video => { :geometry => '520x390', :format => 'mp4',
:convert_options => { :output => { :vcodec => 'libx264',
:vpre => 'ipod640', :b => '250k', :bt => '50k',
:acodec => 'libfaac', :ab => '56k', :ac => 2 } } },
:preview => { :geometry => '300x300>', :format => 'jpg', :time => 5 }
},
processors: [:ffmpeg],
:storage => :s3,
:size => { :in => 0..25.megabytes },
:s3_permissions => :public_read,
:s3_credentials => S3_CREDENTIALS
validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/
validates_presence_of :video
保存视频后,您需要使用一些插件在 show
中显示视频播放器 action.You 可以使用 Mediaelement.js (我最喜欢的)。下载它,将它相应地转储到js/css文件中,将它包含在application.js和application.css中。
in your show.html.erb
##add video tag to show video
<video autobuffer="autobuffer" preload="auto" style="width:100%;height:100%;" controls="controls" width="100%" height="100%" poster="<%= @video.video.url(:preview)%>" >
<source src="<%= @video.video.url%>" />
<%if @video.video.expiring_url(:mp4video).present? %>
<source type="video/mp4" src="<%= @video.video.url(:mp4video)%>" />
<%end%>
</video>
##javascript to handle video player
$('video').mediaelementplayer();
请帮忙解决问题
我用回形针整理上传图片。它有效。
现在我想组织一个视频上传。我改变了这个模型: 型号:
class Video < ActiveRecord::Base
validates :title, presence: true
validates :video, presence: true
belongs_to :user
has_attached_file :video,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/
validates_attachment_file_name :video, :matches => [/3gp\Z/, /mp4\Z/, /flv\Z/]
validate :file_size_validation, :if => "video?"
def file_size_validation
errors[:video] << "should be less than 2MB" if video.size.to_i > 30.megabytes
end
end
视频控制器:
def create
@video = Video.new(video_params)
if @video.save
@video.update_attributes(user: current_user)
flash[:success] = :video_created
redirect_to @video
else
flash.now[:error] = :user_not_created
render 'new'
end
end
形式:
<%= form_for(@video) do |f| %>
<%= f.text_field :title %>
<%= f.file_field :video %>
<%= f.submit %>
<% end %>
尝试上传视频后,我在控制台收到以下错误消息:
Started POST "/videos" for 127.0.0.1 at 2015-07-23 14:17:32 +0300
Processing by VideosController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"RF3w8PzZ9LZ4pdqRPqRvHMQ+nYDL4om0YHAMlzTm8tf3iFmVmKxXSYKap3C0ogEnOkifm9i01oXs/rTX9zmtPA==", "video"=>{"title"=>"tty", "video"=>#<ActionDispatch::Http::UploadedFile:0x007f6624f10770 @tempfile=#<Tempfile:/tmp/RackMultipart20150723-13428-u7s8i8.flv>, @original_filename="mmm.flv", @content_type="video/x-flv", @headers="Content-Disposition: form-data; name=\"video[video]\"; filename=\"mmm.flv\"\r\nContent-Type: video/x-flv\r\n">}, "commit"=>"Create Video"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 8]]
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-6gn39i.flv'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-1eailfd.flv[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
(0.2ms) BEGIN
Command :: file -b --mime '/tmp/c4efd5020cb49b9d3257ffa0fbccc0ae20150723-13428-khstwa.flv'
(0.2ms) ROLLBACK
屏幕上显示以下错误消息:
Video Paperclip::Errors::NotIdentifiedByImageMagickError
其中imageMagik安装在我的系统中:
sudo apt-get install imagemagick
db:
create_table "videos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.integer "user_id"
t.string "video_file_size"
t.string "video_updated_at"
t.string "video_file_name"
t.string "video_content_type"
end
paperclip
的处理器是为图像设计的,所以它不能处理视频。您收到错误消息,指出 ImageMagick 未将此文件识别为图像。很公平,这不是图像!
不过,您可以自己编写一个 post 处理器,以及 paperclip
's github page offers an example 调用自定义处理所需的语法 class:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]
这定义了名为 text
的附件变体,它是通过使用(假设的)Paperclip::Ocr
class 和选项散列 { :quality => :better }
[ 处理上传的文件获得的=17=]
具体转换工具的选择由您决定,但您确实希望确保处理是异步完成的,而不是在通常的请求-响应周期内完成,您可能需要使用像 Sidekiq、Que 这样的后台队列或 Rails' (4.2+) ActiveJob。
使用 image
与 paperclip
和 video
是 different.Though 共同点是它会帮助您将上传的数据发送到服务器,但您需要用一些 processor.Just 来处理它,就像你对 images
使用 ImageMagick
一样,你应该使用 FFMPEG
到 encode/decode video
。
我将分享我通常用于视频的代码..但在此之前,您必须设置 ffmpeg
来处理所有格式的视频,就像您为 Imagemagick
所做的那样。
here 是为 ffmpeg 设置的,不要忘记使用 which ffmpeg
您还需要 ffmpeg-paperclip 以及 coding/decoding 个视频。
video.rb
##using s3
##convert the video into mp4 and also get screenshot of video at 5 sec
##add your own formats that you want
has_attached_file :video,
:styles => {
:mp4video => { :geometry => '520x390', :format => 'mp4',
:convert_options => { :output => { :vcodec => 'libx264',
:vpre => 'ipod640', :b => '250k', :bt => '50k',
:acodec => 'libfaac', :ab => '56k', :ac => 2 } } },
:preview => { :geometry => '300x300>', :format => 'jpg', :time => 5 }
},
processors: [:ffmpeg],
:storage => :s3,
:size => { :in => 0..25.megabytes },
:s3_permissions => :public_read,
:s3_credentials => S3_CREDENTIALS
validates_attachment_content_type :video, :content_type => /\Avideo\/.*\Z/
validates_presence_of :video
保存视频后,您需要使用一些插件在 show
中显示视频播放器 action.You 可以使用 Mediaelement.js (我最喜欢的)。下载它,将它相应地转储到js/css文件中,将它包含在application.js和application.css中。
in your show.html.erb
##add video tag to show video
<video autobuffer="autobuffer" preload="auto" style="width:100%;height:100%;" controls="controls" width="100%" height="100%" poster="<%= @video.video.url(:preview)%>" >
<source src="<%= @video.video.url%>" />
<%if @video.video.expiring_url(:mp4video).present? %>
<source type="video/mp4" src="<%= @video.video.url(:mp4video)%>" />
<%end%>
</video>
##javascript to handle video player
$('video').mediaelementplayer();