문제

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.

도움이 되었습니까?

해결책 2

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

다른 팁

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.

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.

Run this Bash one-liner in a directory with *.wav files.

for wavfile in *.wav; do \
  sox "${wavfile}" "preview-${wavfile}" trim 0 60 fade 3 57 3; \
  lame --preset standard "preview-${wavfile}" \
    "preview-`basename ${wavfile} .wav`".mp3; \
  rm "preview-${wavfile}"; \
done

First 60 seconds. 3 seconds fade-in and 3 seconds fade-out. Original wav files stay untouched. Preview files come with a "preview-" prefix. You'll be able to choose location and length by changing "trim 0 60" to fit Your needs. Requires: sox, lame

If You have a directory with mp3 files and need to create previews, run this:

for mp3file in *.mp3; do \
  mpg123 -w "${mp3file}.wav" "${mp3file}"; \
  sox "${mp3file}.wav" "preview-${mp3file}.wav" trim 0 60 fade 3 57 3; \
  rm "${mp3file}.wav"; \
  lame --preset standard "preview-${mp3file}.wav" "preview-${mp3file}"; \
  rm -v "preview-${mp3file}.wav"; \
done

Requires: mpg123, sox, lame

I wrote a python library, pydub, that makes this trivial, though it uses ffmpeg in to do the conversions in order to support more formats…

from pydub import AudioSegment

sound = AudioSegment.from_file("/input/file.aiff", format="aif")

# 2 min and 12 sec, them convert to milliseconds
start = (2*60 + 12) * 1000
end = start +  (30 * 1000)
snip = sound[start:end]

# add 3 second fade in and fade out
snip = snip.fadeIn(3000).fadeOut(3000)

# save as mp3
snip.export("/output/file.mp3", format="mp3")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top