连接视频文件以获得 Rails 项目中的单个视频

Concatenate video files to get a single video in Rails project

我想知道哪个是连接视频文件的最佳或最有效的库。我在一个 Rails 项目中工作,我需要合并不同的视频文件以获得一个 mp4 格式的视频。我一直在 Internet 上搜索,但没有看到一个清晰简单的解决方案。因此,如果您能提供一个好的建议,我将不胜感激。

您的最佳选择可能是 FFMPEG

您将能够操纵视频输入和音频以及许多其他东西,对于连接,您可以使用以下

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

这对我有用:

# 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