Suppose I have the following files to group and merge, according to
their filenames. How to do it on Ruby?
P.S In BASH, you can merge two mp3 files perfectly with the cat command.
cat file1.mp3 file2.mp3 > file.mp3
01-01 Faithful Subjects.mp3
01-02 Faithful Subjects.mp3
01-03 Faithful Subjects.mp3
01-04 Faithful Subjects.mp3
01-05 Faithful Subjects.mp3
01-06 Faithful Subjects.mp3
01-07 Colonial Constitutions and Their Inspiration.mp3
01-08 Colonial Constitutions and Their Inspiration.mp3
01-09 Colonial Constitutions and Their Inspiration.mp3
01-10 Colonial Constitutions and Their Inspiration.mp3
01-11 Colonial Constitutions and Their Inspiration.mp3
01-12 Colonial Constitutions and Their Inspiration.mp3
02-01 blah blah blah.mp3
02-02 blah blah blah.mp3
etc…
Open first file for write and binary, then put the cursor at the end of
the file…
Then open the second file, for read and binary, and then add it to the
end, and so on?
Dir.foreach(dir) do |file|
unless file =~ /^./
num, name, ext = file.scan(/([0-9]+-[0-9]+)\ ([A-Za-z0-9
]+)(.mp3)/).flatten
if store[name]
store[name][:numbers] << num
else
store[name] = { :extension => ext, :numbers => [num] }
end
end
end
store.each do |name, properties|
filename = name + properties[:extension]
complete_file = File.open(filename, “w”)
store[name][:numbers].each do |num|
part_filename = num + " " + name + properties[:extension]
File.open(dir+"/"+part_filename, “r”) do |f|
complete_file.write(f.read)
end
end
complete_file.close
end
I have ever tried to merge files in Ruby, but I failed. Now, I am using an easy-to-use tool called Joyoshare Video Joiner to combine audio/video files for me. It is recommended by my friend. Such a tool offers a lossless mode to merge files, which satisfies me greatly.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.