HOWTO: Recording audio from the command line

Recording any sound that goes through your computer is a feature that you don’t have to miss out. Mostly if this sound is music. There are lots of internet streaming mp3 sources that stream audio that you can listen with your computer and why not, you can record to mp3 and have an enormous mp3 library. In this howto I’m going to give you powerful tools to record sound directly to an mp3 file or ogg file from the command line. So, you will be able to record for hours and hours your favourite music without having to worry about your hard disk space.

First of all you have to set up the recording channels by doing

alsamixer

Once there, select the capture view by typing the tab key. You’ll get the next screen:

alsamixer.png

With the arrow keys select the column Capture and set it to the CAPTUR mode with the space key as in the screenshot. Adjust the recording volume with the arrow keys. You can also set it up with the gnome volume control panel going to the capture tab.

Recording sound to an mp3 file

You’ll need the lame mp3 encoder. Install it by doing

sudo apt-get install lame

Type the following command

arecord -f cd -t raw | lame -x -r – out.mp3

Arecord captures the audio that goes through your computer and pipes it to the lame encoder, so you encode the audio directly to an mp3 file. You can specify more options to the lame encoder such as the bitrate with lame -x -b bitrate. Without specifying the bitrate it encodes to 128kbps constant bit rate cbr. If you want to record for an specific amount of time then:

arecord -f cd -d numberofseconds -t raw | lame -x -r – out.mp3

Recording sound to an ogg file

You’ll need the oggenc (the ogg encoder). Install it by doing

sudo apt-get install vorbis-tools

Type the following command

arecord -f cd -t raw | oggenc – -r -o out.ogg

And you’ll get your sound recorded to an ogg file. Take into account that we record directly to a compressed file, so there’s nothing in between, so you can record for hours saving an incredible amount of hard disk space.

Ripping shoutcast audio streaming

Streamripper allows us to rip audio streaming servers. Install it by typing

sudo apt-get install streamripper

You can connect to any shoutcast radio station with xmms. Once playing get the info and write down the url. Then type:

streamripper url

and you’ll get each song in a separate mp3 file.

Enjoy recording!!! 🙂
Interesting audio applications.

Audacity-Editing mp3/ogg files

Streamripper-ripping audio streaming servers

Streamtuner-Stream directory browser

About jordilin

I'm a Senior Software Engineer with an MSc in Telecommunications Engineering. I've got a range of interests including Linux, a very powerful and limitless operating system, travelling, gastronomy and technology.
This entry was posted in HOWTOs. Bookmark the permalink.

70 Responses to HOWTO: Recording audio from the command line

  1. Maurice says:

    I’ve been trying to do this for ages. You may have concluded thet I’m not too computer savvy so thanks for the help – it works.

  2. Sven says:

    Great page! I’ve tried so many wird things on the console to achive this with little success. Now I know the easy solution 🙂

  3. Matt says:

    Thanks for this howto. Now I can record my favourite radio programs and copy them to my portable mp3 player. Next step is to set up some cron jobs.

  4. Asif Ali Rizwaan says:

    thank you friend, i really appreciate the small tutorial for recording sound in linux.. and encoding into mp3 and encode into .ogg format.

    thanks. i like it..

  5. Phil Sammons says:

    Thankyou sooo much.

    Been hacking away trying to get arecord to work for ages, so i’m glad that now works. But streamripper works soo much bettter anyway, so now I use that.

    Thanks

    Phil

  6. Asif Ali Rizwaan says:

    arecord -f cd -t raw | lame -x – out.mp3

    should be

    arecord -f cd -t raw | lame -x – out.mp3

    the ‘-‘ is converted to special character ‘–’

  7. Dan Farrell says:

    ‘LINE IN’ CAPTURE

    The capture screen in alsa mixer often allows for the selection of one of a multiple set of inputs. on my system, the default was Mic, but pressing ‘spacebar’ when ‘Line In’ was selected make that the capture device.

  8. mrdevis says:

    Great, and nearly just what I’m looking for. One small but, as I need to record VERY long files, is there: a) a file size limit in linux for mp3 coded files? And if so: b) a way to make seamless chunks of mp3 files at max size?

    Thanks, Jeroen

  9. Erik Christiansen says:

    Many thanks for this method. Next step is to find out how to use the -D option to obviate the need for manual mucking with alsamixer. Then the whole deal could be scripted. The arecord manpage makes selection of front microphone a complete mystery. (arecord -L doesn’t help much either, sadly.)

  10. vpv says:

    “With the arrow keys select the column Capture and set it to the CAPTUR mode with the space key as in the screenshot.”

    God bless you, man! You saved my life!!

  11. max says:

    This is the best HOWTO ever. Finally I got my desk- and laptop to record after a lot of tries.
    Thank you

  12. Donneker says:

    Hi, just wanna say, you can use radio_rip also for ripping (radio-) streams. Its good to use with streamtuner. Just look at my project page under http://www.donneker.de/projects/radio-rip/
    You dont have to type the URLs any more, click and hear & record with just easy streamtuner usage. It also splits the songs after the stream titles. enjoy!

  13. GByte says:

    Hi!

    How about arecord restriction on size of output file? – it is set to 2GB…

  14. Pingback: Odaiba Net » Blog Archive » Odaiba Net 38

  15. Charles says:

    Tried all above and everything seemed to go ok.
    The out.mp3 file is generated but it is silent when played back. the file size is not zero.

    Any suggestions ??

  16. Alok says:

    I have an in-built mic and an external mic jack (though no mic is connected to it). When I tried to record sound, all I got was an empty (44byte) wave file. I am not sure what is going wrong…. In fact I have two capture columns in my mixer (which I daresay correspond to the two mic inputs) I have checked both of them to ensure both are used to capture sound. Does anyone know what is going wrong here ?

  17. Pingback: Enfim o capitulo « btux

  18. Bill says:

    This is great. What command should one use to record directly into a flac file?

  19. Bill says:

    To record in flac it is essential to use arecord to create wav not raw output. So command is:

    arecord -f cd -t wav | flac – -o /home/Bill/Music/out.flac

  20. yo yo says:

    If you get empty, silent files using this method, you might need to also enable the “Mix” capture channel in alsamixer. That did the trick for me.

    Thank you for the excellent tutorial!

  21. Ben says:

    pardon my ignorance…

    LAME 32bits version 3.98.2
    arecord: version 1.0.16

    arecord -f cd -t raw | lame -x – out.mp3
    Recording raw data ‘stdin’ : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
    Warning: unsupported audio format

  22. jordilin says:

    Well, actually it should be:

    arecord -f cd -t raw | lame -x -r – out.mp3

  23. MalBoakly says:

    eh.. luv it

  24. MalBoakly says:

    any updates coming ?

  25. meho_r says:

    Hi. I managed to record a sound, but it contains terrible noise (I can here a song I’ve been recording through it). Any ideas? And, BTW, that “–” doesn’t work for me. I had to put “-” before “out.mp3”

  26. Raffles10 says:

    Doesn’t work:

    arecord -f cd -t raw | lame -x -r – out.mp3

    results in:

    Could not find “–”.
    Recording raw data ‘stdin’ : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

    Nothing is recorded.

  27. Raffles10 says:

    Replaced “–” with “-”, recorded silent file ‘out.mp3’

  28. tensigh says:

    Doesn’t work for me — I can’t seem to find a ‘capture’ mode.

  29. Jo says:

    Ditto. I can’t get ‘capture’ to show up.

  30. tensigh says:

    Jo,

    I think it has to do with how Ubuntu uses a sound server like thing. I managed to do what I needed but not by using this method.

    There’s a way to set your default recording input to use ‘monitor’ which is basically your sound card. And if you think about it, it’s the same as a band using a monitor on a stage for hearing how they’re playing. This seems to work and I can record now any sound that comes out of my sound card.

  31. Hey, you have a great blog here! I’m definitely going to bookmark you! Thank you for your info.And this is streaming radio site/blog. It pretty much covers streaming radio related stuff.

  32. Andrei Thorp says:

    Hello!

    Good article, a very nice introduction to the commands. Everything seems to work, but there are some things I can’t figure out based on man pages:
    – How do I pipe a file into sound input. Say I want to mix in some silly sounds to my recording live, rather than editing them in afterwards.
    – How do I choose to record speakers only, or mic only, or both together? I see it has a -D for PCM choosing, but my only options are “default”, “front”, and a bunch of surround variants that probably don’t do much since I’m on a laptop.

  33. Sarat Babu says:

    Hi, It is very good article…
    I want to broadcast my mic output from my system to all systems in my network without delay. I am very thankful to you. Please help me.

  34. John Good says:

    Thanks soooo much. I’ve going crazy trying to record line-in.

  35. Gabriel says:

    Very good! This help me a lot but my mic was impossible to be convinced to work….
    So I dig more and I find this blog:
    http://www.33dots.com/index.php/linux/enabling-mic-to-work-on-cent-os.html
    Now It WORKS!!!!!!!
    Thanks!

  36. jordilin says:

    For those who have lots of background noise while recording to mp3, try to remove the “-x” option (byte swapping). You can always type “man lame” to see the multiple options that lame provides.

  37. miki987 says:

    I would have a question regardig the extend of the command. I am typing this and get a non working file.mp3 of 0 kb, although my mic works without lame.

    -D seems to cause issues.
    arecord -Dplughw:1,0 -f cd -t raw | lame -x -r – file.mp3

    Is that right tgat’ -D and lame cannot working altoghert, by piping ?

  38. Henrik Storner says:

    Selecting the right device appears to be a problem. I figured out how I could do it – the “-D” option for arecord needs a device name, which can be found from “arecord -L” output. Mine gives (I’ve left out all those devices that are described as “output”, “surround” or “speaker” devices):

    $ arecord -L
    null
    Discard all samples (playback) or generate zero samples (capture)
    default:CARD=Intel
    HDA Intel, ALC1200 Analog
    Default Audio Device
    default:CARD=iVinyl
    PhonoPreAmp iVinyl, USB Audio
    Default Audio Device
    So you can see, there are only three possible devices: A null device, my motherboard audio port, and the iVinyl USB thing that I have connected to my old analog record player. This is the one I want to use, so the -D option needs this name: “default:CARD=iVinyl”. Hence this command is what I ended up using:

    arecord -D “default:CARD=iVinyl” -f cd myrecord.wav

    Hope this helps.

  39. Rob says:

    Thanks for the information. I fumbled off and on for almost a week trying to get rec (sox) to record a radio program from the line-in jack. The first minute always started out fine and then the sound got choppy for the rest of the recording. It was unbearable to listen to and many searches resulted in no answers.

    I just listened to an eleven minute recording using arecord piped to oggenc and it is flawless. I’m good to go.

    Thank you.

  40. redkirk says:

    Great article. I do have a question that hasn’t been covered –how do you end the recording when you’re done? I assume you’d want to cleanly terminate the session and close the file you’ve been recording to.

    • jordilin says:

      You can specify the duration of the recording by means of -d (–duration) numberofseconds. If not, it will record forever till you kill it by doing Ctrl-c; that will stop recording and save the mp3 file you’ve been recording till then. Killing the app will not flush your mp3 file or delete it if you meant that.

  41. Thanks, just started researching this today, as theres a bunch of radio stations with great talk radio shows on them now i’d like to record..

  42. Pingback: Timed audio recording howto

  43. Great article! Thanks!
    I was just wondering if it would be possible to retrieve info out of an ical ics file, to schedule the recording, to save the recording to a set name in the ics file, and stuff like that…
    Anyone ever used that stuff?
    Maybe some software already does this kind of stuff by itself?
    Thanks for letting me know!

  44. stuart says:

    I have tired to record a Spanish station but so far I have not been successful.
    I listen to the station in firefox 5 and in a terminal window I tried to save this sound, as you suggested,
    arecord -f cd -t raw | oggenc – -r -o myfile.ogg
    It looks like something is happening. Then when I replay ogg123 I hear nothing. I can hear the station in Firefox with out trouble but I cant seemed to replay the file using ogg123.the file I have saved. I am a developer but not in this field. I am trying to record long periods of “talking” mainly as it would be useful to replay the Spanish, which I am learning, when there is something I didn’t understand. The compression of the files appeals to me as I could listen to the radio all day. It doesn’t have to me mp3. Any Ideas what I can try next? I have looked at the Alsamixer and it looks ok but I am not sure of the significance of everything. Thanks for any help.

    • jordilin says:

      Make sure that the recording channel is the right one and with enough volume by means of Alsamixer. Just type “alsamixer” and then “tab” to see the “capture” channel. Increase volume up to the top with the arrow keys. Just did it myself with an Ubuntu Natty 11.04 and works fine. Hope that helps.

  45. 787d says:

    Fantastic! Thank you!

  46. Pingback: How to divert sound from laptop speakers to a sound file without using a microphone ?

  47. Linda says:

    I get:

    hello@prompt:~/etc/install$ arecord -f cd -t raw | lame -x – test.mp3
    Recording raw data ‘stdin’ : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
    Warning: unsupported audio format

  48. Pushkaraj Thorat says:

    I do not have “capture” device in the list of alsamixer. I have checked in F4:Capture view as well as in F5:All view. Any clue?

  49. Pushkaraj Thorat says:

    This solved my problem.. http://www.ubuntugeek.com/how-to-recording-internal-audio-in-ubuntu.html

    This is GUI stuff, hmm not really powerful as arecord or lame, but it works.

    Do not miss the comment section.

  50. Pingback: Recording audio output | Q&A System

  51. bonz says:

    love the script works great except for mplayer lag where vlc works fine.
    i know it is meant to be quick and dirty and have not tried this myself yet but thought is this.
    make the time a parameter where no time means record indefinitely and then place a sleep right after the mplayer line before running pkill mplayer on following line.

  52. WOW just what I was looking for. Came here by searching for
    auditing

  53. I think that what you said made a ton of sense. However, think on
    this, what if you composed a catchier post title?
    I mean, I don’t want to tell you how to run your website, but suppose you added a title to possibly get people’s
    attention? I mean HOWTO: Recording audio from the command line | A Linux Geek wandering around is a little vanilla.
    You might look at Yahoo’s front page and watch how they create article titles to grab viewers to click. You might add a video or a related pic or two to grab people interested about what you’ve
    got to say. In my opinion, it might bring your posts a little livelier.

  54. Mentaleak says:

    I’m having trouble getting many of the functions to work.
    Can you tell me what packages I need to install
    to get

    alsamixer
    arecord

    installed?

    I am currently running Raspbian

  55. Steven says:

    Thanks for this how to, but there is a thing that is not clear to me.
    I want to record in 320 vbr, how do I do this. When I type this arecord -f cd -t raw | lame -x -r – out.mp3 , I get this ‘stdin’ : Signed 16 bit Little Endian, Frequentie 44100 Hz, Stereo, and for the rest nothing happens. It seems like he is waiting for something.
    So I want to be able to record 320kbps VBR, and know where he saves the file.
    Can someone help me
    Thanks

    • Dan Houlihan says:

      For the Lame commands type: man lame for the lame manual. The file will be saved in your current directory I would think. Hope this helps.

  56. Dan Houlihan says:

    This is a great tidbit to know!!! I’M have an old, 15 year old Think pad, And the blot of todays gui’s make it drag. It still works great on the console! It would be a little to hassle to get the level set but not that much. I use several terminal programs I use including Mutt for email. The commands to use this should work great with a script file with a short name. Thanks for the Idea it should be very helpful to record my friends record collection to cd!! I do have two I5 machines but its fun to make the old machine pay it’s way. It does quite well for a $20.00 dollar computer!

  57. def says:

    Another application for recording audio is fmedia (http://fmedia.firmdev.com):

    fmedia –record –out=rec.ogg

    Like arecord, it uses ALSA capture device, but fmedia also supports more complex output formats such as FLAC, OGG and MP3, so there’s no need for you to install additional packages (lame or oggenc).

    Also, while recording, fmedia shows how loud the signal is, for example:

    $ fmedia –record –out=myrec.flac
    fmedia v0.10
    0:19 [========..] -7.89dB / -1.31dB

  58. Ocie Bormann says:

    ceramic floor tiles are the best, i used to have linoleum tiles but they do not last very long~

  59. mrbriskers says:

    Why not make things simple? To capture a Shoutcast or an IceCast stream at the command line:
    curl http://%5Bwebdomain name]:8000 -o filename.mp3
    It will continuously push the stream into a filename.mp3 file until you hit “Command+C” to stop filling the filename.mp3 bucket or until you kill the process ID. Now you have an MP3 of all audio material broadcast for that radio program. No special programs except the standard UNIX/Linux command line utility “curl” and if you don’t have it then do [ sudo apt-get install curl ] to get it.

  60. Judy says:

    It is not my first time to visit this website, i am visiting this web site dailly and get nice information from
    here every day.

  61. Angelia says:

    An impressive share! I have just forwarded this onto a friend who was doing a little research on this.
    And he in fact bought me breakfast due to the fact that I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!

    But yeah, thanx for spending some time to discuss this matter here on your website.

  62. Burton says:

    After I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and now each
    time a comment is added I receive four emails with the exact same comment.
    Is there a means you are able to remove me from that service?
    Cheers!

  63. Karly Caves says:

    Having read this I believed it was really enlightening. I appreciate you spending some time and energy to put this content together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worthwhile!

  64. Pingback: record soundcard with arecord (not microphone) - Boot Panic

Leave a reply to Raffles10 Cancel reply