audiomp3wavaiff

Create mp3 previews from wav and aiff files


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.


Solution

  • I wanted to use something as low level as possible, so I ended up using RubyAudio, 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