Linux record audio from console / terminal with rec, arecord and ffmpeg

 

Recording sound input from microphone linux penguin holding microphone

Recording from microphone input on Linux is possible, through multiple programs.

1. Recording microphone input using SoX's rec

 The classical old-school way is through a little proggie called sox. Back in the day I remember we recorded with sox with a friend from the school years necroleak – mirror of his website is on kenamick.pc-freak.net, trying to make save vox for one of his Tracked Songs. The experiments was not very succesful as both the PC microphone was low quality one, as well as the the state of recording microphone sound streams on Linux was terrible, but at least I learned about sox.

sox is not so popular and mainstream as it used to be back in the day but for anyone willing to investigate into the roots of GNU / Linux sound capturing make sure you have installed sox, alsa-utils and lame package. The package is available across virtually all main stream Linux distributions, depending on the distro to INSTALL sox do:

 

apt-get install --yes sox alsa-utils lame 
....

 (On Debian, Ubuntu, Arch, Xubuntu … )

yum -y install sox  alsa-utils lame
....

 (on Fedora, CentOS, RHEL …)

 slapt-get install sox ; swaret install sox alsa-utils lame
.....

(on Slackware and derivatives)

Before continuing it is a good idea to check, the microphone is not muted in alsamixer, amixer or aumix

The SoX package provides 4 binaries;

dpkg -L sox|grep -i /usr/bin/
/usr/bin/sox
/usr/bin/rec
/usr/bin/play
/usr/bin/soxi

sox -is tool to apply effects to recorded sound streams

rec – is historically among the first sound recorder tool to make records from microphone (even form the days of OSS – Open Sound System)

play – play is tiny .WAV and some other native classical sounds formats with a beautiful ASCII art (text) equalizer

soxi – gives information on recorded sound stream header (info)

rec -r 8000 -c 1 record_microphone_input.wav

rec is unfortunately made to use the old and now obsolete /dev/dsp sound interface, so on many Linux distributions, recording sound with it might pose problems.

Another problem of rec is it usually records with a lot of noise, thus reducing the noise later with sox cmd is almost necessery, to mitigate the noise you will have to experiment with its options. For some better quality of recording use arg -r 22050.

A little shell script with plenty of example use cases of rec and post sox effect applied as synchronization record_and_normalize_from_mic_with_rec_and_sox_on_linux.sh is here

Generally I mentioned rec for historical reasons, nowadays it is quite obsolete so you probably better stick to the newer alsa native arecord.

2. Recording sound from microphone using alsa-utils arecord

alsa-utils package has bunch of tools to record, play and tune sound;

dpkg -L alsa-utils |grep -i /usr/bin/
/usr/bin/aplaymidi
/usr/bin/aplay
/usr/bin/aconnect
/usr/bin/amixer
/usr/bin/alsamixer
/usr/bin/aseqdump
/usr/bin/arecordmidi
/usr/bin/speaker-test
/usr/bin/iecset
/usr/bin/amidi
/usr/bin/aseqnet
/usr/bin/arecord

One of tools included arecord is able to capture sound from microphone. arecord, can record into .WAV, but as .WAVs are not compressed and most people prefer to save the input to some more wide recognized format as .MP3 it should be invoked in conjunction with lame;

arecord -D plughw:0,0 -f S16_LE -c1 -r22050 -t raw | lame -r -s 22.05 -m m -b 64 - mic-input.mp3


Writting this long and hard to remember command line and arguments is tough, so I created a tiny shell script wrapper which accepts as 1-st argument a file name and saves .WAV and converts it to .MP3. The script linux_record_from_microphone.sh is here

3. Recording from microphone input using ffmpeg

I've earlier blogged on how to use ffmpeg to capture Microphone sound here.

For those lazy to read my previous post the skele syntax is;

ffmpeg -f alsa -ac 2 -i pulse -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y myVOICE.wav 

To later convert WAV to MP3 use lame;

 lame -r -s 22.05 -m m -b 64 myVOICE.wav  mic-input.mp3

Share this on:

More helpful Articles

Download PDFDownload PDF

Tags: , , , , , , , , , , , , , ,

5 Responses to “Linux record audio from console / terminal with rec, arecord and ffmpeg”

  1. Gepeto says:
    Firefox 18.0 Firefox 18.0 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0

    Thanks for this share ! I’ve managed to record audio with a Python script (witch is a nice way to do that too). I was missing the lame command line to convert to mp3. Great ! God bless !

    View CommentView Comment
    • admin says:
      Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
      Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

      Glad it helped. What kind of python script you used to record audio put a link here to check it out 🙂 What is witch I hear it first time?

      View CommentView Comment
  2. Gepeto says:
    Firefox 18.0 Firefox 18.0 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0

    Well… I can share you two links witch inspired me a lot for one of my purpose :
    http://stackoverflow.com/a/6743593
    http://stackoverflow.com/a/4160733
    My first purpose was to detect silences in a record stream, so these helped (the is_silent function). My real final prupose is to record readings (Bible) for, let say, a whole book, then to cut a big file according to 5 seconds silences (managed by the reader). I just wanna checked if this is easily possible for the moment. I didn't get to a final script. I just get to the point : "if needed, I know how to do that !"
    Now, just because that's the first time I write to your blog, let me say you something : Your blog is real interesting, from the technical side, as from the spiritual one. Thanx from France 🙂

    View CommentView Comment
    • admin says:
      Firefox 3.6.3 Firefox 3.6.3 Windows 7 Windows 7
      Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

      Heya Gepeto,

      Thanks for the kind words, you have very good intention to record Bible. Pray God bless you to complete your good goal. Also it is rare people say a good word for my blog content and it is always so nice to hear helpful.

      Just few minutes before I saw some video in youtube about some ancient Churches from 5th century in France. In Bulgaria we have few from 5th centuries too. Never was in france but will be glad to see it one day. Heard many good things of France from my cousin who went few times already.

      Greetings

      View CommentView Comment
  3. Saif Ali says:
    Google Chrome 74.0.3729.131 Google Chrome 74.0.3729.131 Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36

    I Love This Post, Thank You Share With Us.

    View CommentView Comment

Leave a Reply

CommentLuv badge