16

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,336
  • 6
  • 66
  • 105
hotips
  • 343
  • 1
  • 5
  • 10

4 Answers4

9

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
  • 191
  • 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,638
  • 15
  • 69
  • 113
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
  • 852
  • 11
  • 9