Home » Questions » Computers [ Ask a new question ]

Linux command to concatenate audio files and output them to ogg

Linux command to concatenate audio files and output them to ogg

What command-line tools do I need in order to concatenate several audio files and output them as one ogg (and/or mp3)?

Asked by: Guest | Views: 31
Total answers/comments: 5
Guest [Entry]

"Here's my suggestion: Use mplayer and oggenc connected with a named pipe.

Use mplayer to decode the audio.
It can play back a wide variety of
audio (and video) formats, and it also can play
multiple files.
Use oggencto encode the audio to
Ogg Vorbis.
To eliminate the need for a
temporary file, use a named
pipe
to transfer the data between encoder
and decoder.

Putting that into a script:

#!/bin/sh
# Usage: ./combine2ogg destination.ogg source1.wma source2.wma ...
# Requires: mplayer, oggenc
destination=""$1""
shift
readpipe=""$destination.tmpr""
writepipe=""$destination.tmpw""
mkfifo ""$readpipe""
mkfifo ""$writepipe""
cat ""$writepipe"" > ""$readpipe"" &
mplayer -really-quiet -slave -nolirc -vc null -vo null -ao ""pcm:fast:file=$writepipe"" ""$@"" &
oggenc --quiet -o ""$destination"" ""$readpipe""
rm -f ""$readpipe""
rm -f ""$writepipe""

Explained:

Take the destination file name from the first command line parameter.
Remove the first command line parameter, leaving only the source file names.
Create a name for a pipe for oggenc to read from.
Create a name for a pipe for mplayer to write to
Create the pipes.
Use cat to continuously dump the writepipe to the readpipe (this helps avoid issues where mplayer may terminate, but prevents oggenc from thinking it's done when this happens)
Decode the audio from the source files using mplayer. Options -really-quiet -slave -nolirc are there to disable messages and to make it not read the keyboard or the remote. Options -vc null -vo null are there to disable video encoding and output. The -ao option directs it to output the audio in WAV format to the named write pipe.
While the previous command is running, simultaneously encode from the named read pipe into Ogg using oggenc.
Remove the named pipes.

Stuff to left to improve: Terminating the script early if one of the commands fails (use set -e), but still properly cleaning up the fifo (trap the necessary signals)."
Guest [Entry]

"I would use ffmpeg. To convert wma to ogg vorbis try:

ffmpeg -i sample.wma -acodec vorbis -aq 100 sample.ogg

or mp3:

ffmpeg -i input.wma -acodec libmp3lame output.mp3

you'll need lame installed for the mp3 convert. sudo apt-get install lame libmp3lame0

Cat, then convert doen't seem to work well, although you can find lots of references on the web saying you can do something like cat *.wma | ffmpeg -i - -acodec ... - this doesn't work on my machine - only the first file gets processed. There is a 'copy' codec for ffmpeg, but doesn't make much difference.

Doing the convert with ffmpeg first, then cat *.ogg > output.ogg worked for me."
Guest [Entry]

"Mencoder can concatenate and transcode videos without temp files (trivial example here). There's a script floating around somewhere called aconvert that tricks mencoder into doing audio-only (which it normally refuses to do). These two things combined seem like they would satisfy your requirements.

Edit: I found aconvert.sh. Unfortunately, it's only designed to convert one audio file. But here's the code anyway, in case anyone wants to use it to craft a command line that converts multiple audio files.

#!/bin/sh

# Author: Jonas Jermann
# Description: A hack to allow mencoder to encode from an audio only file

if [ ""$1"" = """" ]; then
echo ""Usage: $0 <\""input file\""> <\""output file\""> <\""options\"">""
exit 0
fi

options=${3:-""-oac mp3lame""}

mencoder -demuxer rawvideo -rawvideo w=1:h=1 -ovc copy -of rawaudio -endpos `mplayer -identify $1 -frames 0 2>&1 | grep ID_LENGTH | cut -d ""="" -f 2` -audiofile $1 -o $2 $options $1"
Guest [Entry]

"This might not be exactly what you need, but it should help
http://bitzenbytes.com/index.php?name=PNphpBB2&file=viewtopic&t=6538&postdays=0&postorder=asc&sid=f87b364ddbae3ddd9417f5d10fae870c"
Guest [Entry]

"Thought I'd throw my answer in. This probably isn't the most elegant way, but it did what I wanted.

Step 1: convert to ogg (uses mplayer, pacpl and ruby, all available from the free Debian repos. I)

#! /usr/bin/ruby

ARGV.each do |target|
file_ext = target.scan(/\.\w*$/)[0]
name = target.sub(file_ext, """")

`mplayer -novideo -ao pcm:file=""tmp.wav"" ""#{target}""`
`pacpl -t ogg --outfile ""#{name}"" tmp.wav`
`rm tmp.wav`
end

Step 2: concatenate ogg files

cat *ogg > big-file.ogg"