ruby-on-rails - 回形针视频上传

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

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

迁移:

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>

我得到一个不会玩的播放器。 如果您仔细阅读并帮助我想出一个可行的解决方案,我将不胜感激。谢谢!

最佳答案

回形针视频上传:

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

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

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

关于ruby-on-rails - 回形针视频上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222492/

相关文章:

ruby-on-rails - 将 html 类传递给 options_for_select 中的选项不起作用 Rails 2.3.9

ruby - 检查是否调用了类方法

ruby-on-rails - rails : Render a status code without rendering a template

javascript - 同时使用 AJAX $_FILES 和 $_POST

java - 无法上传大文件

ruby-on-rails - 使用 Ruby on Rails 保持项目列表有序的最佳方法是什么?

javascript - 如何在 rails 的文件( xyz.js.erb )中调用 java 脚本函数?

ruby-on-rails - 时间.current.tap{|t| t.change小时: 10 } is not working in Rails 4. 2

ruby-on-rails - 如何重定向到 Rails 中的 404?

c# - ASP.net C# 多个 web 应用程序 - 一个文件上传目录