Create mp3 previews from wav and aiff files

I would like to create a program that makes mp3 files the first 30 seconds of aiff or wav file. I would also like to be able to choose a location and length, for example, a sound between 2:12 and 2:42. Are there any tools that let me do this?

Shutting down the shell is fine. The application will run on a Linux server, so it should be a tool that runs on Linux.

I do not mind doing this in two steps - i.e. a tool that first creates the aiff / wav cutout and then transfers it to the mp3 encoder.

+4
source share
5 answers

I wanted to use something as low 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 
+2
source

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

+3
source

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

+1
source

Run this single-line Bash in the 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 

The first 60 seconds. 3 seconds of attenuation and 3 seconds of extinction. The wav source files remain untouched. Preview files come prefixed with "preview-". You can choose the location and length by changing the β€œtrim 0 60” to fit your needs. Requires: sox, lame

If you have a directory with mp3 files and you need to create a preview, 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

+1
source

I wrote pydub which makes this trivial, although it uses ffmpeg for conversions 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") 
+1
source

Source: https://habr.com/ru/post/1305614/


All Articles