Merging mp3 files in Ruby

Q

Qu3ry Qu3ry

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...
 
A

Aldric Giacomoni

Qu3ry said:
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?
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 04. Nov 2009, 17:57:24 +0900 schrieb Qu3ry Qu3ry:
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

ruby -pe '' file1.mp3 file2.mp3 >file.mp3

Bertram
 
Q

Qu3ry Qu3ry

Bertram said:
Hi,

Am Mittwoch, 04. Nov 2009, 17:57:24 +0900 schrieb Qu3ry Qu3ry:

ruby -pe '' file1.mp3 file2.mp3 >file.mp3

Bertram

How to iterate the mp3 files according to their names, so that files
with the same names are merged together?
 
S

Sven Schott

[Note: parts of this message were removed to make it a legal post.]

How about a hash or something like that?

dir = ARGV[0]
store = Hash.new

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top