David Soler
David Soler

Reputation: 229

Concatenate video files to get a single video in Rails project

I would like to know which is the best or the most efficient library to concatenate video files. I'm working in a Rails project and I need to merge different video files to get one video in mp4. I've been searching in Internet but I don't see a clear and easy solution. So if you could give a good advice I would really appreciate it.

Upvotes: 1

Views: 914

Answers (2)

fguillen
fguillen

Reputation: 38832

This is what worked for me:

# Usage: 
#   output_path = ConcatVideoFilesService.new.perform(video_paths)
class ConcatVideoFilesService
  def perform(video_paths, output_path = nil)
    output_path ||= Dir::Tmpname.create(["concat_video_files_out_put", ".webm"]) {}
    video_paths_path = Dir::Tmpname.create(["concat_video_files_list", ".txt"]) {}

    File.open(video_paths_path, "w") do |f|
      video_paths.each do |video_path|
        f.write("file '#{video_path}' \n")
      end
    end

    begin
      system "ffmpeg -avoid_negative_ts 1 -f concat -safe 0 -i #{video_paths_path} -c copy #{output_path}"
      return output_path
    ensure
      File.delete(video_paths_path) if File.exists?(video_paths_path)
    end
  end
end

Upvotes: 0

Kiloreux
Kiloreux

Reputation: 2256

Your best choice might be FFMPEG

You will be able to manipulate video input and audio as well as many other things, and for the concatenation you could use the following

system "ffmpeg -i concat: \"#{video source(path)} | #{other video source (path)}\" -c copy #{name_of_output_file}"

Upvotes: 4

Related Questions