TheWaterbug
Posts: 73
Joined: Fri Feb 05, 2016 12:35 am
Location: Los Angeles, CA, USA, Earth

Any way to add alsa support to ffmpeg without recompiling?

Sun Feb 11, 2018 6:56 pm

Hi! I have ffmpeg compiled and running based on these instructions. My USB mini mic is known-working, because:

arecord -D plughw:1,0 -d 3.0 test.wav && aplay test.wav

results in sound coming out of my earphones, as expected. But when I attempt to feed in sound from a USB mini mic via:

sudo ffmpeg -f alsa -i hw:1 -t 30 out.wav

I get the error

Unknown input format: 'alsa'

Based on this StackExchange answer, it looks like I may not have had alsa support configured when I compiled ffmpeg.

On my machine, grep alsa configure returns:

Code: Select all

  --disable-alsa           disable ALSA support [autodetect]
    alsa
alsa_indev_deps="alsa"
alsa_outdev_deps="alsa"
enabled alsa && check_pkg_config alsa alsa "alsa/asoundlib.h" snd_pcm_htimestamp ||
    check_lib alsa alsa/asoundlib.h snd_pcm_htimestamp -lasound
I tried sudo apt-get install libasound2-dev after the fact, but that doesn't fix the problem.

Is there a way to "install" support in ffmpeg for alsa without recompiling ffmpeg from scratch?

Thanks!

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Tue Feb 27, 2018 12:35 am

No, unfortunately not. You must recompile ffmpeg to add enable additional libraries. Below is the script I build to compile ffmpeg with alsa, fdk-aac, and libx264 support. It will install ffmpeg in your home folder inside a "ffmpeg" folder, so you'll need to call it specifically from there unless you add it to your path. I recommend uninstalling your current ffmpeg before using my script.

Btw, I am able to stream directly to YouTube now without any issues. I use an external USB sound card and the PiCam v2 and stream a [email protected] video stream with a 192kbps stereo audio stream mixed in. It works great!


#!/bin/bash

#Get some required libraries and header files for x264 and OMX
sudo apt-get install libasound2-dev libvpx. libx264. libomxil-bellagio-dev -y

#Get FFMPEG source code
cd ~
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
mkdir dependencies
cd dependencies/
mkdir output
cd ~

#Compile libx264
git clone http://git.videolan.org/git/x264.git
cd x264/
./configure --enable-static --prefix=/home/pi/ffmpeg/dependencies/output/
make -j4
make install
cd ~

#Compile ALSA
wget ftp://ftp.alsa-project.org/pub/lib/alsa ... .1.tar.bz2
tar xjf alsa-lib-1.1.1.tar.bz2
cd alsa-lib-1.1.1/
./configure --prefix=/home/pi/ffmpeg/dependencies/output
make -j4
make install
cd ~

#Compile FDK-AAC
sudo apt-get install pkg-config autoconf automake libtool -y
git clone https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
./autogen.sh
./configure --enable-shared --enable-static
make -j4
sudo make install
sudo ldconfig
cd ~

#Compile FFMPEG
cd ffmpeg
./configure --prefix=/home/pi/ffmpeg/dependencies/output --enable-gpl --enable-libx264 --enable-nonfree --enable-libfdk_aac --enable-omx --enable-omx-rpi --extra-cflags="-I/home/pi/ffmpeg/dependencies/output/include" --extra-ldflags="-L/home/pi/ffmpeg/dependencies/output/lib" --extra-libs="-lx264 -lpthread -lm -ldl"
make -j4
make install
cd ~
Last edited by tropho on Thu Mar 01, 2018 2:32 pm, edited 1 time in total.

subzerosoundman
Posts: 4
Joined: Wed Feb 28, 2018 4:56 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Feb 28, 2018 10:52 pm

Thanks for the info on how to make ffmpeg.

What do you mean - stream to youtube?
I always watch videos from youtube, not to youtube.
What protocol do you use to stream - TCP, UDP, RTP?
Could you show your ffmpeg command line?

Thanks.

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Thu Mar 01, 2018 2:31 pm

You're welcome, I'm glad I could finally contribute something to the forums!

I meant YouTube Live, their live streaming service where anyone with a YouTube account can stream live video from a device, usually a smartphone but it can be any device with a video camera that can output h264 video and stream it via RTMP. Facebook Live operates the same way.

Here are my two command lines, which captures video using raspivid and pipes it into ffmpeg, with ffmpeg capturing audio using the alsa plugin (for which I had to recompile ffmpeg) and muxing the h264 video and AAC audio streams into the FLV format for YouTube and Facebook.

For live streaming to YouTube:
raspivid -o - -t 0 -w 1920 -h 1080 -fps 25 -b 3000000 -rot 270 -g 50 -f | /home/pi/ffmpeg/ffmpeg -thread_queue_size 10240 -f h264 -r 25 -i - -itsoffset 5.3 -f alsa -thread_queue_size 10240 -ac 2 -i hw:1,0 -vcodec copy -acodec aac -ac 2 -ar 44100 -ab 192k -f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY

For live streaming to Facebook (requires quotation marks around the URL and stream key):
raspivid -o - -t 0 -w 1920 -h 1080 -fps 25 -b 3000000 -rot 270 -g 50 -f | /home/pi/ffmpeg/ffmpeg -thread_queue_size 10240 -f h264 -r 25 -i - -itsoffset 5.3 -f alsa -thread_queue_size 10240 -ac 2 -i plughw:1,0 -vcodec copy -acodec aac -ac 2 -ar 44100 -ab 192k -f flv "rtmp://rtmp-api.facebook.com:80/rtmp/YOUR_STREAM_KEY"

(Replace YOUR_STREAM_KEY with your own key provided by YouTube or Facebook)

Some things to note:
1. There is about a 10-15 second delay between what is being recorded and when it shows up on YouTube/Facebook.
2. If ffmpeg is not compiled with alsa support there is no way to stream audio, so it would be a silent video. If you don't want or need audio, there is a way to substitute a "dummy" audio device, since YouTube will reject live stream feeds without sound.
3. The itsoffset value may need to be adjusted for your particular sound device; I did a bunch of testing with a microphone to get the audio/video sync just right, coming up with my value of 5.3. Without it set at all the audio would play 5 seconds BEFORE the video!
4. The thread_queue_size prevents ALSA buffer underruns. I had to use the large value of 10240 to stop buffer underruns, at the expense of adding a time delay to the stream. Nothing less than 10240 completely stopped the ALSA buffer underruns.
5. If you don't want to see any streaming statistics on your screen, add -nostats -loglevel 0 after ffmpeg. I like to see the information so I can see that stream is working or if there are problems.
6. Even though I use a 1920x1080 resolution, I limited the video bitrate to a conservative 3mbps (3000000) because anything more would eventually cause YouTube/Facebook to buffer and pause since it was just too much for my Pi3 (or perhaps my internet upload bandwidth) to keep up with. A 192k audio bitrate is good enough, and anything more just increases the overall bandwidth requirements with very little gain in quality.
7. I use a USB external sound device to capture audio, using the stereo line input. I used alsamixer to set my recording levels and which input I wanted to use. The microphone input can also be used but it's mono.
8. I wrote a couple of short scripts to allow me to use the 4 buttons on my Adafruit PiTFT screen for starting live streams (YouTube or Facebook), capturing video straight to the 128GB flash drive (no live stream), and properly shutting down.
9. My hardware: Raspberry Pi 3 Model B, Raspberry Pi Cam v2, 4GB Sandisk microSD card, Adafruit PiTFT 2.8" LCD screen with buttons (model 2298), USB external sound device (https://goo.gl/ALjiit), 20,000mAh battery pack (provides about 14 hours of recording time)

As you can imagine with #8 and #9, I built a small video camera that can live stream or capture video anywhere since it's battery powered. I need either public WiFi or my smartphone's hotspot to connect to the internet, but besides that it is self-contained. I screwed a tripod mount on the bottom of the case so it can be mounted on a tripod, and used right-angle cables and velcro to keep it all compact and secure. I have attached pictures so you can see what it looks like. It resembles an oversized GoPro video camera.


If you have any questions or wonder how or why I did something feel free to ask. I have read many forums and experimented to figure out how to make this work for me, and I would enjoy giving back to other users looking to solve their own challenges.

Good luck!
Attachments
IMG_20180301_091750376.jpg
IMG_20180301_091750376.jpg (106.05 KiB) Viewed 2939 times
IMG_20180301_090852775.jpg
IMG_20180301_090852775.jpg (100.6 KiB) Viewed 2939 times
IMG_20180301_090841225.jpg
IMG_20180301_090841225.jpg (105.65 KiB) Viewed 2939 times
Last edited by tropho on Thu Mar 01, 2018 5:15 pm, edited 1 time in total.

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Thu Mar 01, 2018 2:46 pm

Here are more pictures since the forum post limited me to three attachments.
Attachments
IMG_20180301_090902351.jpg
IMG_20180301_090902351.jpg (98.71 KiB) Viewed 2931 times
IMG_20180301_090822787.jpg
IMG_20180301_090822787.jpg (102.75 KiB) Viewed 2931 times

subzerosoundman
Posts: 4
Joined: Wed Feb 28, 2018 4:56 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Fri Mar 02, 2018 12:22 am

Wow, that is impressive. Streaming live audio/video, for not much money.
That USB sound box is super cheap.

Thanks for the command lines. Those are really long.
I am glad to know how to add audio to ffmpeg video now.
Who would have guessed it would be that complicated?

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Fri Mar 02, 2018 1:22 am

Thanks, it was a fun project for me. The best part was I had everything but the right angle cables already, so it was very little extra cost. I could've used my regular straight cables but it looked sloppy and I wanted it to look a bit less like a science project when I use it around other people.

It was difficult to work out the ALSA compilation and command line options since most people seemed to stream video-only (no audio) to YouTube, and no one had an answer how to add audio since many people have no idea how to compile from source, or that it's even necessary. I finally found a guy's personal blog with his ffmpeg compilation method, which needed to be changed a little to work for me. Hopefully people can now find this message thread and it helps them out since it's all in one place.

schaema
Posts: 2
Joined: Sat Apr 21, 2018 10:48 am

Re: Any way to add alsa support to ffmpeg without recompiling?

Sat Apr 21, 2018 10:52 am

Hello,
great Manual, it Works perfect with Video & Audio. :)
First Time i use Docker Image, but it was without Audio. This Manual is great.
Can you show me the Scripts to Start the Stream with the 4 Button?

schamea

TheWaterbug
Posts: 73
Joined: Fri Feb 05, 2016 12:35 am
Location: Los Angeles, CA, USA, Earth

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Apr 25, 2018 1:34 am

tropho wrote:
Tue Feb 27, 2018 12:35 am
No, unfortunately not. You must recompile ffmpeg to add enable additional libraries. Below is the script I build to compile ffmpeg with alsa, fdk-aac, and libx264 support.
Thanks! I don't know why I didn't see this earlier. I'll try this when I get back from my trip.

TheWaterbug
Posts: 73
Joined: Fri Feb 05, 2016 12:35 am
Location: Los Angeles, CA, USA, Earth

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Apr 25, 2018 1:39 am

tropho wrote:
Thu Mar 01, 2018 2:31 pm
2. If ffmpeg is not compiled with alsa support there is no way to stream audio, so it would be a silent video. If you don't want or need audio, there is a way to substitute a "dummy" audio device, since YouTube will reject live stream feeds without sound.
I had this same problem. Another solution is to download some royalty-free MP3 files provided by YouTube. Use scp to copy them to your pi, then make a text file containing their filenames, and use ffmpeg's "concat" filter to play them in series, e.g.:

Code: Select all

./ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i "rtsp://anonymous:[email protected]:554" -f concat -safe 0 -i playlist.txt -vcodec copy -acodec copy -t 01:47:02 -f flv "your-youtube-streaming-key"
With "-acodec copy" the CPU usage is nearly 0% for the audio stream. The "-t 01:47:02" is just the length of my playlist (which I got by copying and pasting the titles and lengths from iTunes into Excel).

In my case I'm running ffmpeg on a different Pi than the Pi that my camera is built around, but I intend to change that once I get ffmpeg compiled on the camera Pi.

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Apr 25, 2018 12:52 pm

schaema wrote:
Sat Apr 21, 2018 10:52 am
Hello,
great Manual, it Works perfect with Video & Audio. :)
First Time i use Docker Image, but it was without Audio. This Manual is great.
Can you show me the Scripts to Start the Stream with the 4 Button?

schamea
Sure thing, I've pasted my Python script below. I named it pitft_buttons.py but of course you can name it what you like.

More notes:
-1. I use the AdaFruit PiTFT 2.8" LCD, so if a higher resolution screen is used some of the scripts might need to be adjusted for spacing and font size.
-2. To have the Python button script start when the Pi boots I added /home/pi/pitft_buttons.py & to the /etc/rc.local file.
-3. You can also add sudo sh -c "TERM=linux setterm -blank 0 >/dev/tty0" to stop the cursor from blinking on the terminal screen, to give it a slightly more professional look and be less distracting. You will still be able to type, there will just be no visible blinking cursor on either the LCD screen or if you ssh from another computer.
-4. The shell scripts called in the python script below are just like the ones mentioned in my earlier post, but I've included them once again below that script if you need them.
--4a. Each script also changes the font type and size to make everything fit better on the 2.8" PiTFT screen.
--4b. The stream_local_1080.sh script records video to a 128GB USB flash drive connected to one of the Pi3's USB ports, so you would have to setup something similar (mount a USB drive, change your mount point, etc.) to make it work for you. It also uniquely names files with date/time stamps. If you don't need this feel free to use that button for something else.
--4c. The "black" button (GPIO 27, black button for me) shuts down the Pi cleanly to prevent microSD card corruption. I wait about 10 seconds before I disconnect power.
-5. Don't forget to replace the "INSERT-YOUR-STREAMING-KEY-HERE" in the scripts with your unique streaming keys for YouTube and Facebook.

Here's my Python script to control the AdaFruit PiTFT buttons:
#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import os
import subprocess

#Set GPIO mode
GPIO.setmode(GPIO.BCM)

#Setup GPIO
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Give the system a quick break
time.sleep(0.5)

#var for the 'while' statement to keep it running
var = 1

#Main program
while var == 1:

if (GPIO.input(17) == False):
subprocess.call("/home/pi/stream_yt_1080.sh")
time.sleep(0.5)

if (GPIO.input(22) == False):
subprocess.call("/home/pi/stream_fb_1080.sh")
time.sleep(0.5)

if (GPIO.input(23) == False):
subprocess.call("/home/pi/stream_local_1080.sh")
time.sleep(0.5)

if (GPIO.input(27) == False):
subprocess.call("clear")
os.system("sudo shutdown -h now")
time.sleep(0.5)

time.sleep(1)

GPIO.cleanup()


YouTube streaming script stream_yt_1080.sh:
#!/bin/bash
clear
setfont Uni2-Terminus12x6
raspivid -o - -t 0 -w 1920 -h 1080 -fps 25 -b 3000000 -rot 270 -g 50 -f | /home/pi/ffmpeg/ffmpeg -hide_banner -thread_queue_size 10240 -f h264 -r 25 -i - -itsoffset 5.3 -f alsa -thread_queue_size 10240 -ac 2 -i hw:1,0 -vcodec copy -acodec aac -ac 2 -ar 44100 -ab 192k -f flv rtmp://a.rtmp.youtube.com/live2/INSERT-YOUR-STREAMING-KEY-HERE
setfont Uni2-Fixed13

Facebook streaming script stream_fb_1080.sh:
#!/bin/bash
clear
setfont Uni2-Terminus12x6
raspivid -o - -t 0 -w 1280 -h 720 -fps 25 -b 3000000 -rot 270 -g 50 -f | /home/pi/ffmpeg/ffmpeg -hide_banner -thread_queue_size 10240 -f h264 -r 25 -i - -itsoffset 5.3 -f alsa -thread_queue_size 10240 -ac 2 -i plughw:1,0 -vcodec copy -acodec aac -ac 2 -ar 44100 -ab 192k -f flv "rtmp://rtmp-api.facebook.com:80/rtmp/INSERT-YOUR-STREAMING-KEY-HERE"
setfont Uni2-Fixed13

Local video recording script stream_local_1080.sh:
#!/bin/bash
clear
setfont Uni2-Terminus12x6
NOW=$(date +%Y-%m-%d-%H-%M-%S)
raspivid -o - -t 0 -w 1920 -h 1080 -fps 25 -b 6000000 -rot 270 -g 50 -f | /home/pi/ffmpeg/ffmpeg -hide_banner -nostats -thread_queue_size 10240 -f h264 -r 25 -i pipe: -itsoffset 5.3 -f alsa -thread_queue_size 10240 -ac 2 -i plughw:1,0 -vcodec copy -acodec aac -ar 44100 -ab 256k -f flv /mnt/128GB/$NOW.mkv
setfont Uni2-Fixed13

I added the below lines to my .bashrc file, which displays a nice menu to show the user what each button does. It also resizes and changes the terminal font so everything fits nicely on the PiTFT, as well as changes the cursor prompt text color to black so it is not visible:
setfont Uni2-Fixed13
echo -e '\033[?17;0;0c' > /dev/tty1
clear
echo
echo
echo
echo -e " \e[31mRED \e[0mbutton begins YouTube live stream"
echo
echo
echo
echo
echo -e " \e[34m\e[1mBLUE \e[0mbutton begins Facebook live stream"
echo
echo
echo
echo -e " \e[33m\e[1mYELLOW \e[0mbutton records video to camera"
echo -e " (live stream will NOT be broadcasted)"
echo
echo
echo -e " \e[1mBLACK \e[0mbutton shuts down camera"
Last edited by tropho on Wed Apr 25, 2018 1:00 pm, edited 1 time in total.

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Apr 25, 2018 12:53 pm

TheWaterbug wrote:
Wed Apr 25, 2018 1:39 am
tropho wrote:
Thu Mar 01, 2018 2:31 pm
2. If ffmpeg is not compiled with alsa support there is no way to stream audio, so it would be a silent video. If you don't want or need audio, there is a way to substitute a "dummy" audio device, since YouTube will reject live stream feeds without sound.
I had this same problem. Another solution is to download some royalty-free MP3 files provided by YouTube. Use scp to copy them to your pi, then make a text file containing their filenames, and use ffmpeg's "concat" filter to play them in series, e.g.:

Code: Select all

./ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i "rtsp://anonymous:[email protected]:554" -f concat -safe 0 -i playlist.txt -vcodec copy -acodec copy -t 01:47:02 -f flv "your-youtube-streaming-key"
With "-acodec copy" the CPU usage is nearly 0% for the audio stream. The "-t 01:47:02" is just the length of my playlist (which I got by copying and pasting the titles and lengths from iTunes into Excel).

In my case I'm running ffmpeg on a different Pi than the Pi that my camera is built around, but I intend to change that once I get ffmpeg compiled on the camera Pi.

Great idea, and thanks for the tip!

Digivolution
Posts: 16
Joined: Wed Mar 07, 2018 9:56 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Mon Jun 18, 2018 8:49 pm

I followed tropho's in the 2nd reply/tutorial and Frame rate drop pretty steady over time.

Any ideas?
D.

tropho
Posts: 37
Joined: Mon Feb 26, 2018 11:36 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Wed Jun 20, 2018 6:24 pm

Digivolution wrote:
Mon Jun 18, 2018 8:49 pm
I followed tropho's in the 2nd reply/tutorial and Frame rate drop pretty steady over time.

Any ideas?
D.
Are you referring to the framerate or the transcoding speed? I too have noticed a gradual slowdown, but for the transcoding speed rather than framerate (the "speed=" field during ouput). It usually begins rather high around 3x-4x, then slowly decreases as it settles to 1.02x....but then it slowly slips below 1.0x and eventually the stream falls behind and starts buffering or drops out. I have not been able to resolve this, and have used a Windows laptop with OBS, which works flawlessly via my phone' WiFi hotspot, the same connection I used for the RPI live stream camera.

Unfortunately, it has not been very reliably for me during real-world tests. It worked great at home, even using the WiFi hotspot but I think it's very sensitive to bandwidth changes when I am at locations with less than ideal cellular data coverage and speeds. Since it worked reliably at my house I don't think it's due a slow CPU problem. What a shame, it was a fun project to get up and running.

Digivolution
Posts: 16
Joined: Wed Mar 07, 2018 9:56 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Sat Jun 23, 2018 4:01 am

I was guessing fps. That's what it was labeled in the ssh window. I dropped to around 25 pretty quick then slowly even lower.

schaema
Posts: 2
Joined: Sat Apr 21, 2018 10:48 am

Re: Any way to add alsa support to ffmpeg without recompiling?

Sun Jun 24, 2018 3:18 pm

Hello,
thank your for the Python scripts for the buttons. Do you have the image from die Pi for download?
I;m not be able to edit button red. Al other button working fine. I use the 2,2 inch TFT Display with 6 Buttons on the print. GPI 23-22-24-5 on the left side from the Display, but GPI 23 allways logout the PI for shutdown.

schaema

VonJulio
Posts: 2
Joined: Sun Aug 12, 2018 8:14 pm

Re: Any way to add alsa support to ffmpeg without recompiling?

Fri Aug 31, 2018 1:44 am

Hello all,
I've been trying your manual with a new raspberry pi 3. Everything except compiling ALSA and compiling FFMPEG worked out the way it was supposed to. what am i doing wrong?

Return to “Graphics, sound and multimedia”