4

I'm trying to enable video upload in my post. Can't get it to display the video. The video gets uploaded, I can confirm that while i manage to right click on the video area and download it. The problem is how to view it correctly.

Migration:

class AddAttachmentVideoToPosts < ActiveRecord::Migration
  def self.up
    change_table :posts do |t|
      t.attachment :video
    end
  end

  def self.down
    drop_attached_file :posts, :video
  end
end
 def change
    create_table :videos do |t|
        t.string :video_file_name
        t.string :video_content_type
        t.integer :video_file_size
        t.datetime :video_updated_at

        t.timestamps
    end
end

Post Model

class Post < ActiveRecord::Base
    default_scope :order => 'created_at desc'
    attr_accessible :content, :title, :photo, :photo_delete, :video, :video_delete, :dependent => :destroy
    has_attached_file :photo, :styles => {  :thumb => "600x600#", :medium => "300x300#", :small => "160x160#"}
    has_attached_file :video
    validates_uniqueness_of :title
    validates_presence_of :title, :content
    has_destroyable_file :photo, :video
end

Video part in my post _form

<div class="visible-md visible-lg">
  <%= f.file_field :video, :style => "float: left;" %>
  <%= f.check_box :video_delete, :style => "float: left;" %> &nbsp;Delete video
  </div><br />
<div class="visible-xs">
  <%= f.file_field :video, :style => "center" %>
  <%= f.check_box :video_delete, :style => "center" %> &nbsp;Delete video
</div><br />

Video part in Post Show

<% if @post.video? %>
<h1 class="center">
<%= @post.title %>
</h1><br />
<%= video_path @post.video.url %>       
<% end %>

I have also tried with video_tag which don't work and when I try with:

<iframe width="490" height="275" src="<%= video_path @post.video.url %>" frameborder="0" allowfullscreen autoplay="true">
            </iframe>

I get a player that won't play. Would really appreciate if you look it through and maybe help me to come up with a solution that will work. Thanks!

4

2 回答 2

3

回形针视频上传:

上周我遇到了同样的问题 - 试试这个!

Video model:
    has_attached_file :video, styles: {
        :medium => {
          :geometry => "640x480",
          :format => 'mp4'
        },
        :thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
    }, :processors => [:transcoder]
    validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/

确保您已经捆绑:

gem 'paperclip', '~> 4.3.1'
gem 'aws-sdk', '< 2.0'
gem 'paperclip-av-transcoder'
gem "paperclip-ffmpeg", "~> 1.2.0"

运行回形针迁移:

rails g paperclip model video

请务必在 post_controller.rb 中添加:

private

    def bscenes_params
        params.require(:post).permit(:video)
    end

上传表格:

<%= f.file_field :video %>

显示页面:

<%= video_tag bscene.video.url(:medium), controls: true, style: "max-width: 100%;" %>

此时你应该得到这个错误:

Av::UnableToDetect (Unable to detect any supported library):

转到您的终端并输入:

brew options ffmpeg

然后运行以下命令来安装 ffmpeg:

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

重新启动您的服务器并立即尝试上传视频!希望这会有所帮助 - 编码快乐 :)

于 2015-10-14T20:45:24.360 回答
2

您应该使用video标签,而不是iframe(只有video标签具有自动播放选项)。在这里查看什么浏览器支持什么格式:http: //caniuse.com/#search=video

如果您想要跨浏览器解决方案,请尝试 VideoJS - http://www.videojs.com/ 在这里您可以获得 Rails 插件 - https://github.com/seanbehan/videojs_rails

于 2013-10-10T09:42:20.180 回答