1

这是我在这里的第一篇文章 :) 我刚从 rails 开始,我有一点 c/c++ 的背景,但没有什么广泛的。

我正在玩 ffmpeg,我用铁轨做了一些小脚手架,或者至少尝试过。所以基本上我想上传两个视频文件并让ffmpeg合并它们并吐出输出。我正在运行 ruby​​ 1.9.2 和 rails 3.2.11。我用brew安装了ffmpeg。在 segment.rb 中,我创建了一个片段名称、source_video 和 the_other_video,并且也这样做了

def append_to_video(the_other_video, output_file)
    system "ffmpeg -i concat: \"#{the_other_video.source_video.path} | #{self.source_video.path}\" -c copy #{output_file}"
end

我可以上传视频并下载相同的视频,但仅此而已。没有 FFMPEG。

我如何让 ffmpeg 在片段中使用回形针?非常感谢您抽出宝贵的时间。

4

3 回答 3

0

首先,您的方法应该进入类内部,然后您可以使用after_save回调(http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)在所有需要的文件完成后立即进行所有 ffmpeg 处理已上传,存储在文件系统中并持久化到数据库中。

class Segment < ActiveRecord::Base
    attr_accessible :name, :source_video, :the_other_video
    has_attached_file :source_video
    has_attached_file :the_other_video

    after_save :append_to_video

    def append_to_video(the_other_video, output_file)
        system "ffmpeg -i concat: \"#{the_other_video.source_video.path} | #{self.source_video.path}\" -c copy #{output_file}"
    end
end

最终,您可能必须对代码进行更多更改,但这应该让您从概念上开始!如果您再次卡住,请更新您的问题!(在那:请始终通过编辑您的原始问题来为您的原始问题添加其他信息,而不是通过给出真正不包含答案但只是更多问题的答案:))

于 2013-08-05T13:21:40.577 回答
0

感谢您的回复 :) 没有错误消息,我在 segment.rb 文件中有 ffmpeg 命令,我什至不确定那是否是正确的地方?在 segment.rb 我有

class Segment < ActiveRecord::Base
  attr_accessible :name, :source_video, :the_other_video
  has_attached_file :source_video
  has_attached_file :the_other_video
end

def append_to_video(the_other_video, output_file)
    system "ffmpeg -i concat: \"#{the_other_video.source_video.path} | #{self.source_video.path}\" -c copy #{output_file}"
 end

对于 _form.html.erb

<%= form_for(@segment) do |f| %>
  <% if @segment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@segment.errors.count, "error") %> prohibited this segment from being saved:</h2>

  <ul>
  <% @segment.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
 <% end %>

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</div>

 <div class="field">
  <%= f.label :source_video %><br />
  <%= f.file_field :source_video %>
</div>

 <div class="field">
  <%= f.label :the_other_video %><br />
  <%= f.file_field :the_other_video %>
 </div>

 <div class="actions">
  <%= f.submit %>
  </div>
 <% end %>

对于 show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
    <%= @segment.name %>
</p>

<p>
   <b>Source Video:</b>
  <%= link_to 'Download Source Video', @segment.source_video.url %>
</p>

<p>
<b>Output:</b>
  <%= link_to 'Download Output', @segment.the_other_video.url %>
</p>

<%= link_to 'Edit', edit_segment_path(@segment) %> |
<%= link_to 'Back', segments_path %>

这是我的 schema.rb

ActiveRecord::Schema.define(:version => 20130801040708) do

create_table "segments", :force => true do |t|
  t.string   "name"
  t.datetime "created_at",                   :null => false
  t.datetime "updated_at",                   :null => false
  t.string   "source_video_file_name"
  t.string   "source_video_content_type"
  t.integer  "source_video_file_size"
  t.datetime "source_video_updated_at"
  t.string   "the_other_video_file_name"
  t.string   "the_other_video_content_type"
  t.integer  "the_other_video_file_size"
  t.datetime "the_other_video_updated_at"
end

end

所以我上传了视频,当我按下创建片段时,我希望它将两个剪辑组合在一起,我不确定我会在哪里运行 ffmpeg 命令?再次感谢!

于 2013-08-01T19:11:33.357 回答
0

你可以试试paperclip-ffmpeg宝石。它允许您以编程方式将参数传递给 FFMPEG,使用Paperclip.

免责声明:我是paperclip-ffmpeggem 的作者。

网址:https ://github.com/owahab/paperclip-ffmpeg

于 2015-04-22T04:33:14.963 回答