tags:

views:

78

answers:

3

I would like to create a program that makes mp3s of the first 30 seconds of an aiff or wav file. I would also like to be able to choose location and length, such as the audio between 2:12 and 2:42. Are there any tools that lets me do this?

Shelling out is OK. The application will run on a linux server, so it would have to be a tool that works on linux.

I don't mind doing it in two steps - i.e. a tool that first creates the cutout of the aiff/wav, then pass it to a mp3 encoder.

+3  A: 

SoX with the trim predicate can do this. If your sox is not built with MP3 support then you'll have to pipe the output to lame after, or find one that is.

Ignacio Vazquez-Abrams
+1 Sox... ooooo shiny
Byron Whitlock
+1  A: 

Use LAME for the mp3 encoding part. Use shntplit to split the file. You will need to put your split points in a cue file, but that is easy.

Byron Whitlock
A: 

I wanted to use something as low level as possible, so I ended up using Rubyudio, a wrapper for libsndfile.

require "rubygems"
require "ruby-audio"

EXTRACT_BEGIN = 11.2
EXTRACT_LENGTH = 3.5

RubyAudio::Sound.open("/home/augustl/sandbox/test.aif") do |snd|
  info = snd.info
  ["channels", "format", "frames", "samplerate", "sections", "seekable"].each do |key|
    puts "#{key}: #{info.send(key)}"
  end

  # TODO: should we use a 1000 byte buffer? Does it matter? See RubyAudio::Sound rdocs.
  bytes_to_read = (info.samplerate * EXTRACT_LENGTH).to_i
  buffer = RubyAudio::Buffer.new("float", bytes_to_read, info.channels)

  snd.seek(info.samplerate * EXTRACT_BEGIN)
  snd.read(buffer, bytes_to_read)

  out = RubyAudio::Sound.open("/home/augustl/sandbox/out.aif", "w", info.clone)
  out.write(buffer)
end
August Lilleaas