15

I try to stream live audio using ffmpeg and external USB microphone. I followed this nearly tutorial

I had to adapt some steps but finally, I achieved to receive the stream my laptop using this command :

ffmpeg -f oss -i /dev/dsp1 -acodec libmp3lame -ab 32k -ac 1 -re -f rtp rtp://192.168.28.116:1234

The CPU is near 100%, i have a bad sound during two seconds and after nothing... I see in Wireshark that the board is sending frames continuously.

Does someone have ideas to lower the CPU usage ?

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
hotips
  • 333
  • 1
  • 5
  • 9
  • You may want to look into overclocking and a different memory split. If you have the choice of codec you may be able to achieve some improvement by switching. You may also want to investigate compression of the stream - though this may be a zero some gain - anything you gain in transfer time may be lost in compression. – Steve Robillard Aug 05 '12 at 22:46
  • Did you try writing to a local file first? I'd try to go step by step: 1) realtime recording to a wave file, 2) realtime recording to an mp3 file 3) streaming stuff over the network – pixelistik Aug 06 '12 at 09:36
  • not at the moment. seems to be complicated to do all thoses steps manually instead of full automatic ffmpeg ;-) – hotips Aug 06 '12 at 09:51
  • What are you recording? – Alex Chamberlain Aug 06 '12 at 11:25
  • 1
    I'm trying to make an ethernet babyphone – hotips Aug 06 '12 at 21:45
  • Did you find a suitable solution? – Piotr Kula Feb 21 '15 at 01:14
  • I bought a Wolfson Audio Card for Raspberry Pi but i didn't had the time to try... The raspberry pi b/b+ was too slow without that. I suppose it will be better with the raspberry pi 2 – hotips Feb 21 '15 at 19:14

4 Answers4

8

To answer your specific question, you can reduce CPU by piping arecord into ffmpeg:

arecord -f cd -D plughw:1,0 | ffmpeg -i - -acodec libmp3lame -ab 32k -ac 1 -re -f rtp rtp://234.5.5.5:1234

You'll need to replace plughw:1,0 with your specific sound card. See arecord -l for info. On my Rasp Pi it went from ~95% CPU to ~35%.

kumar303
  • 181
  • 1
  • 2
7

ALSA Input

One alternative is to go via ALSA. A similar command to above would be

ffmpeg -ac 1 -f alsa -i hw:0,0 -acodec libmp3lame -ab 32k -ac 1 -re -f rtp rtp://localhost:1234

I'm not sure how this will affect the CPU usage.

Alex Chamberlain
  • 15,530
  • 14
  • 67
  • 113
  • I tried : ffmpeg -ac 1 -f alsa -i hw:1,0 -acodec libmp3lame -ab 32k -ac 1 -re -f rtp rtp://192.168.1.116:1234 But ffmpeg crashes after somes seconds : [alsa @ 0x7a1510] ALSA buffer xrun. Any other ideas ? – hotips Aug 10 '12 at 21:48
  • I tested ffmpeg -f oss -i /dev/dsp1 -ab 32k -ac 1 -re -f rtp rtp://192.168.28.116:1234 but the sound is very bad... on the lag is around 6 seconds. Any idea to have a better solution ? – hotips Aug 10 '12 at 22:27
  • 1
    @si2w I think 32k for mp3 is a bit of a joke. Either up that, or try a different codec. Maybe one of the G2xx series designed for telephones – Alex Chamberlain Aug 11 '12 at 07:47
4

This works and reduces cpu usage:

ffmpeg -f alsa -i default:CARD=U0x46d0x819 -acodec mp2 -ac 1 -re -f rtp rtp://234.5.5.5:1234 2> /tmp/mylog.log &

Be sure to replace default:CARD=U0x46d0x819 with your microphone ID, ( obtained from arecord -l ) or you cad specify -i hw:0,0 (or whichever device it is).

I had a similar problem -- mp3 encoding took up 90%+ of cpu power and just could not keep up with the audio -- so I changed it to mp2 encoding. This used about 15-18% of CPU (measured vi top) and streams smoothly to VLC on my LAN. It would make a perfect baby monitor, or whatever. There is just a second or so delay, which is the buffering at the VLC end.

Note: The ip address is a multicast address ([224-239].x.y.z). You don't have to aim it at a particular network device on your LAN, and your broadband router will keep the traffic local (by default).

Scott Veirs
  • 173
  • 4
tim
  • 41
  • 2
2

You can significantly reduce the CPU load by reducing the audio sample rate of the input device (-ar 8000 before -f alsa), and setting the codec audio bit rate to 128k (-b:a 128k). Also ironically reducing the number of channels (-ac 1) seemed to increase the CPU load so I have found this command runs at pretty low CPU:

ffmpeg -ar 8000 -f alsa -i hw:0 -acodec mp2 -b:a 128k -f rtp rtp://other:4444

Although one needs to remember that it also depends on the capabilities of the capture hardware one is trying to use, and the versions of ffmpeg/avconv.

Pierz
  • 844
  • 10
  • 8