【问题标题】:Paperclip Video Upload回形针视频上传
【发布时间】:2013-10-13 21:18:37
【问题描述】:

我正在尝试在我的帖子中启用视频上传。 无法让它显示视频。视频已上传,我可以确认,虽然我设法右键单击视频区域并下载它。问题是如何正确查看。

迁移:

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

后模型

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

我的帖子_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 />

展后视频部分

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

我也尝试过 video_tag 不起作用,当我尝试使用时:

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

我得到一个不会玩的玩家。 如果您仔细查看并可能帮助我想出一个可行的解决方案,我将不胜感激。谢谢!

【问题讨论】:

  • 取决于你想要什么。请记住,托管您自己的用户上传的视频存在一些问题。就像您确保获得正确的编码以实现跨浏览器兼容性一样。 jplayer.org 是很棒的开源播放器,可以处理您的视图层。如果你想要高跨浏览器兼容性,你需要为 jplayer 提供多种编码的路径。您可以使用 zen coder 之类的服务。这确实很昂贵,但他们都有一定程度的免费开发帐户来学习和试验他们的系统。
  • 谢谢,试试jplayer :)
  • @TyrelRichey 我已经安装了 jplayer,加载了皮肤,现在有了一个视频播放器外壳。但我现在不知道如何调用视频以便播放器工作。我有这个 jquery:
  • 让示例视频播放,这意味着它会加载所有已安装的文件。虽然当我调用 m4v 时它不会播放我的视频:“”

标签: ruby-on-rails ruby file-upload paperclip ruby-on-rails-4


【解决方案1】:

回形针视频上传:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 谢谢,是的,使用了 videojs。虽然不是宝石:P ffmpeg、回形针和 flashplayer 是让它在每个 Webb 浏览器中顺利运行的最佳解决方案!
    • 我使用 github.com/devthenet/voyeur 和 Sidekiq 在后台转换视频。
    猜你喜欢
    • 2013-10-16
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多