TheWaterbug
Posts: 77
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: 58
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: 58
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 11625 times
IMG_20180301_090852775.jpg
IMG_20180301_090852775.jpg (100.6 KiB) Viewed 11625 times
IMG_20180301_090841225.jpg
IMG_20180301_090841225.jpg (105.65 KiB) Viewed 11625 times
Last edited by tropho on Thu Mar 01, 2018 5:15 pm, edited 1 time in total.

tropho
Posts: 58
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 11617 times
IMG_20180301_090822787.jpg
IMG_20180301_090822787.jpg (102.75 KiB) Viewed 11617 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: 58
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: 77
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: 77
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: 58
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: 58
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: 58
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?

davethemoneyman
Posts: 8
Joined: Wed Mar 20, 2019 2:04 am

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

Thu Mar 21, 2019 9:16 pm

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. 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 ~
I've rebuilt this setup and THANK YOU for posting this. I'm going to try live streaming an event soon with 3 of these. before I order all the pieces I wanted to get 1 working first. my issue is that I've set it all up and followed the instructions and code... streams to Youtube but no sound. not sure what I did wrong. I know there could be a million things but is there anything special you did with the microphone and sound card? I bought the same sound card plugged it in via USB mic and I get nothing. Any assistance you can provide would be GREATLY appreciated. I'm thinking that the sound card needs to be setup or installed on the RPi. Using RPI 3B+, Camera v2, same sound unit and a small boom mic plugged into the 'line in'
Thanks

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

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

Fri Mar 22, 2019 6:06 pm

davethemoneyman wrote:
Thu Mar 21, 2019 9:16 pm

I've rebuilt this setup and THANK YOU for posting this. I'm going to try live streaming an event soon with 3 of these. before I order all the pieces I wanted to get 1 working first. my issue is that I've set it all up and followed the instructions and code... streams to Youtube but no sound. not sure what I did wrong. I know there could be a million things but is there anything special you did with the microphone and sound card? I bought the same sound card plugged it in via USB mic and I get nothing. Any assistance you can provide would be GREATLY appreciated. I'm thinking that the sound card needs to be setup or installed on the RPi. Using RPI 3B+, Camera v2, same sound unit and a small boom mic plugged into the 'line in'
Thanks
Assuming you use my suggested command line for streaming to YouTube (below), are you certain you're specifying the correct audio recording device? If you haven't disabled the onboard audio device you should have at least one recording device and two playback devices, all with different hardware IDs. In my case, I had to use -i hw:1,0, but you could try -i hw:0,0 or -i hw:1,1 or -i hw:0,1. You can run arecord -l to list your recording devices and aplay -l to list your playback devices.

If you want to disable the onboard audio device (it's playback only and won't affect recording), use sudo nano /etc/modules to edit the modules file, put a hash sign before snd-bcm2835 to make it #snd-bcm2835, then reboot. aplay -l will then report only one playback device, the USB audio adapter. If you do this all audio input and output will use the USB device, which will be the default.

My command line to stream to YouTube:
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

You should also know that I stopped using my device because it became unreliable during real (not tests at home) use. In one of my previous posts I detailed how the "speed=" indicator would gradually decrease over time while streaming, to the point where it was no longer streaming fast enough to prevent the dreaded buffering problem in YouTube. It happened every time, no matter what settings I adjusted. I was able to prolong the time until failure by reducing the bitrate but even when I reduced it to a terrible, high-compression setting it still eventually failed. I had better luck on a higher bandwidth connection but I was never able to resolve this; hopefully you can. If you do please share your solution with us!

davethemoneyman
Posts: 8
Joined: Wed Mar 20, 2019 2:04 am

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

Fri Mar 22, 2019 9:57 pm

tropho wrote:
Fri Mar 22, 2019 6:06 pm
davethemoneyman wrote:
Thu Mar 21, 2019 9:16 pm


Assuming you use my suggested command line for streaming to YouTube (below), are you certain you're specifying the correct audio recording device? If you haven't disabled the onboard audio device you should have at least one recording device and two playback devices, all with different hardware IDs. In my case, I had to use -i hw:1,0, but you could try -i hw:0,0 or -i hw:1,1 or -i hw:0,1. You can run arecord -l to list your recording devices and aplay -l to list your playback devices.

If you want to disable the onboard audio device (it's playback only and won't affect recording), use sudo nano /etc/modules to edit the modules file, put a hash sign before snd-bcm2835 to make it #snd-bcm2835, then reboot. aplay -l will then report only one playback device, the USB audio adapter. If you do this all audio input and output will use the USB device, which will be the default.

You should also know that I stopped using my device because it became unreliable during real (not tests at home) use. In one of my previous posts I detailed how the "speed=" indicator would gradually decrease over time while streaming, to the point where it was no longer streaming fast enough to prevent the dreaded buffering problem in YouTube. It happened every time, no matter what settings I adjusted. I was able to prolong the time until failure by reducing the bitrate but even when I reduced it to a terrible, high-compression setting it still eventually failed. I had better luck on a higher bandwidth connection but I was never able to resolve this; hopefully you can. If you do please share your solution with us!
I'll look into the audio device settings again. I did figure out that sound is going through but the volume level is barely audible ( I only figured this out because I sneezed almost right into the mic while I was testing) so I know the signal is going through, just the level is now the issue. - I should pre-face that I just got started with programming so I'm trying to get up to speed as quick as I can and I do appreciate your help.

I did use your code to start the stream and that works great. I haven't tried long term stream yet but I found the frame rate over about 10 minutes seemed to drop to 1.01x. I'll see if that changes on longer streams. IF I can get to even an hour without issues, I can reset for my purposes every hour. it would be a pain but still would work.

now with that all said, have you found a better way to live stream to Youtube/facebook with Video and Audio?
I'm thinking I may have to look at streaming to my laptop and then use OBS to send it out to Youtube from there.
thanks again for your input.

davethemoneyman
Posts: 8
Joined: Wed Mar 20, 2019 2:04 am

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

Sat Mar 23, 2019 4:03 am

So with realizing the fact that the sound is there but very quiet, I played around with ALSAMIXER and increased the line levels to top end of the white before it goes red. that helped to some degree. you can now hear the audio but still too low. need to boost it to about double to be acceptable for what I need. I'm wanting to live stream a Karate Tournament next week... ya, tall order, I know. I need to set up 3 of these to be running.

I did find one suggestion on Stackexchanger:
"You could try increasing the thread_queue_size to something like 4096 or even higher. I've seen it as high as 12000. I had a similar problem with my stream lagging and this seemed to fix it."
Here's the thread where I found that information:
https://raspberrypi.stackexchange.com/q ... -over-time

That whole sentence means nothing to me but hoping it may make sense to you.

I've been streaming now for an hour and the speed is holding steady at 1x. I'll keep it running and see how long it will go without dropping any more. I'll post an update in the morning.

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

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

Sat Mar 23, 2019 4:03 pm

davethemoneyman wrote:
Sat Mar 23, 2019 4:03 am
So with realizing the fact that the sound is there but very quiet, I played around with ALSAMIXER and increased the line levels to top end of the white before it goes red. that helped to some degree. you can now hear the audio but still too low. need to boost it to about double to be acceptable for what I need. I'm wanting to live stream a Karate Tournament next week... ya, tall order, I know. I need to set up 3 of these to be running.

I did find one suggestion on Stackexchanger:
"You could try increasing the thread_queue_size to something like 4096 or even higher. I've seen it as high as 12000. I had a similar problem with my stream lagging and this seemed to fix it."
Here's the thread where I found that information:
https://raspberrypi.stackexchange.com/q ... -over-time

That whole sentence means nothing to me but hoping it may make sense to you.

I've been streaming now for an hour and the speed is holding steady at 1x. I'll keep it running and see how long it will go without dropping any more. I'll post an update in the morning.
Oh yes, I remember fiddling with the thread_queue_size setting a lot, eventually settling on 10240. The higher this setting is, the longer it takes to start the stream since it seems to prebuffer a bit. I was up to 30 seconds of lag with that setting but it did run for a while before buffer underrun issues began. Regarding alsamixer, look for a mic boost setting; you may need to enable that if it's present.

Also, if you're looking for rock-solid streaming I highly recommend OBS but I really wanted to create an all-in-one live streaming device based on a Raspberry Pi 3B. I was able to run it on a Windows 10 laptop with no issues whatsoever....it ran exactly like I wanted it to but that would be much more expensive if you need to have three cameras running at once.

davethemoneyman
Posts: 8
Joined: Wed Mar 20, 2019 2:04 am

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

Tue Mar 26, 2019 1:48 pm

Also, if you're looking for rock-solid streaming I highly recommend OBS but I really wanted to create an all-in-one live streaming device based on a Raspberry Pi 3B. I was able to run it on a Windows 10 laptop with no issues whatsoever....it ran exactly like I wanted it to but that would be much more expensive if you need to have three cameras running at once.
So I've been playing with the code and figured out that Youtube will continue the stream even if the stream from the RPi stops and starts again. If you restart within about 10 seconds then the stream will continue in the same stream.

So my work around is to generate a script to start the stream code for a period of say 15 min, have it stop and then restart the stream. The stream would have a 5-10 second pause every 15 min but when live-streaming an event over 8 hours, it'd be better than having the sound out of sync.

my issue is, I'm still new to this and have no idea how to write the code to make The RPi do that. I know it's not an ideal solution but for what I need (live-stream an amateur tournament) it would work fine. Any suggestions on how to write that into the code to constantly loop the code to start, run for 15 min, stop and then start again?
Any help you could provide would be greatly appreciated.
Thanks.

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

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

Tue Mar 26, 2019 8:53 pm

davethemoneyman wrote:
Tue Mar 26, 2019 1:48 pm
Also, if you're looking for rock-solid streaming I highly recommend OBS but I really wanted to create an all-in-one live streaming device based on a Raspberry Pi 3B. I was able to run it on a Windows 10 laptop with no issues whatsoever....it ran exactly like I wanted it to but that would be much more expensive if you need to have three cameras running at once.
So I've been playing with the code and figured out that Youtube will continue the stream even if the stream from the RPi stops and starts again. If you restart within about 10 seconds then the stream will continue in the same stream.

So my work around is to generate a script to start the stream code for a period of say 15 min, have it stop and then restart the stream. The stream would have a 5-10 second pause every 15 min but when live-streaming an event over 8 hours, it'd be better than having the sound out of sync.

my issue is, I'm still new to this and have no idea how to write the code to make The RPi do that. I know it's not an ideal solution but for what I need (live-stream an amateur tournament) it would work fine. Any suggestions on how to write that into the code to constantly loop the code to start, run for 15 min, stop and then start again?
Any help you could provide would be greatly appreciated.
Thanks.
That is a great idea! If I hadn't had cellular bandwidth issues I could've implemented it as a workaround.

I recommend you configure your streaming command line to run for 15 minutes, and call it from within another bash script that will restart your YouTube streaming script whenever it finishes. This should continue indefinitely until you CTRL-C or power off the Raspberry Pi. You will need to replace -t 0 with -t 900000 in your command line to stop streaming after 15 minutes (900,000ms=15 minutes). My previous command line is in the example script below; replace it with whatever you have tailored to your needs.

Here's my suggestion:

Create the bash script you will invoke when you want to stream to YouTube:
nano 15min_stream_loop.sh

Within this script enter:
#!/bin/bash
while true
do
raspivid -o - -t 900000 -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
sleep 1
done


Make the bash script executable:
chmod +x 15min_stream_loop.sh

Run it:
./15min_stream_loop.sh

Now it should stream to YouTube for 15 minutes, wait 1 second, then begin streaming again for 15 minutes....again and again until you kill it. You can test it by reducing the streaming time to 2 minutes (-t 120000) to ensure it is restarting the stream as intended.

davethemoneyman
Posts: 8
Joined: Wed Mar 20, 2019 2:04 am

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

Wed Mar 27, 2019 3:59 am

tropho wrote: Now it should stream to YouTube for 15 minutes, wait 1 second, then begin streaming again for 15 minutes....again and again until you kill it. You can test it by reducing the streaming time to 2 minutes (-t 120000) to ensure it is restarting the stream as intended.
Unfortunately it seems the Sleep command seems to be crashing the feed. As I'm watching the terminal lines get to the 2 min mark nothing seems to happen. Then the Youtube feed goes into buffering mode but the Terminal screen just keeps going. The fps starts to drop quickly from what was becoming a stable 31 down to 24 in the next 15 seconds. Youtube just hangs in buffering mode.

When I enter the original single line code in Terminal on it's own it runs fine hovering at about 30 fps for at least 2hrs, I always ended it by that time. what I found I can do is cancel the stream in Terminal. Close the Terminal, open another Terminal window and run the code again with only a delay in Youtube of the time it took to do that. Youtube takes about 20-30 seconds to determine if the feed is done and then will close the stream. so as long as I can do that in the 20-30 seconds, the YT Stream buffers for the time that I re-ran the code and then continues.

This code doesn't seem to re-set the Rpi feed. Is there a way to code a program to open Terminal, run the line for the feed and then close the terminal in 20 min and then do it all over again? I would think that would be possible but it's beyond my ability to code that. I appreciate your help on this. I'd buy you a beer it's a little tough to send via internet. :)

Return to “Graphics, sound and multimedia”