简体   繁体   中英

Unable to concatenate video files using FFMPEG, Paperclip, Rails

This is my first post here :) I am just starting with rails, I have a little background in c/c++, but nothing extensive.

I am playing around with ffmpeg and I did some minor scaffolding with rails, or at least tried. So basically I want to upload two video files and have ffmpeg merge them and spit out the output. I am running ruby 1.9.2 and rails 3.2.11. I installed ffmpeg with brew. In segment.rb I created a segment name, source_video, and the_other_video and also did this

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

I am able to upload the video and get a download of the same video, but that is pretty much it. No FFMPEG.

How do I get ffmpeg to work with paperclip in the segment? Thank you so much for taking the time.

thanks for the reply :) There is no error message, I have the ffmpeg command in the segment.rb file, I am not even sure if that is the right place? In segment.rb I have

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

for _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 %>

for 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 %>

This is my 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

So I upload the videos, and when I press Create segment, I want it to combine the two clips together, I am not sure where I would run the ffmpeg command? Thanks again!

First of all, your method should go inside the class, then you can use an after_save callback ( http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html ) to do all the ffmpeg processing as soon as all needed files have been uploaded, stored in the file system and persisted to the database.

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

Ultimately you will probably have to make some more changes to your code, but this should get you started conceptually! Do update your question if you're stuck again! (And at that: Please always add additional information to your original question by editing your original question, not by giving answers that really contain no answer but just more of your question :))

You can try paperclip-ffmpeg gem. It would allow you to programmatically pass arguments to FFMPEG using Paperclip .

Disclaimer: I am the author of paperclip-ffmpeg gem.

URL: https://github.com/owahab/paperclip-ffmpeg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM