Skip to main content

Live video streaming with ffmpeg and ffserver

Live video and audio streaming with ffmpeg and ffserver
This is a guide on how to stream video using ffserver and ffmpeg in ubnutu. The ffmpeg ffserver version used is 3.3-1~16.04.york1. You can download ffserver from ffmpeg.org. I'm using linux mint 18. So, I've downloaded ffmpeg from the repository. You can follow the link below to install the latest version of ffmpeg.
Install ffmpeg in linux mint 18 using command line.
First, we need to setup our ffserver.conf to stream the audio and video properly. ffserver.conf file is usually located in /etc/ffserver.conf. You can either edit the ffserver.conf or create your own .conf file. I'll be creating my own .conf file named livestream.conf
In console, type

sudo nano /etc/livestream.conf

Blank .ffm file will be created. Now, enter the required properties for streaming live video using ffserver
#Default port
HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 100000
CustomLog -

#############################################################

<Feed video.ffm>
  File /tmp/video.ffm                    # this creates a temp video.ffm file where streams are read/write
  FileMaxSize 1G
  ACL allow localhost
  ACL allow 127.0.0.1
  ACL allow 192.168.0.0 192.168.255.255
</Feed>

<Stream stream>
# streaming for webm file
# run : ffserver -f /etc/ffserver.conf
# run : ffmpeg -i videoname.mp4 http://localhost:8090/video.ffm
# error : encoder setup failed
  Feed video.ffm
  Format webm

# Audio settings
  AudioCodec vorbis
  AudioBitRate 64                             # Audio bitrate

# Video settings
  VideoCodec libvpx
  VideoSize 720x486                           # Video resolution
  VideoFrameRate 30                           # Video FPS
  AVOptionVideo flags +global_header          # Parameters passed to encoder

  AVOptionVideo cpu-used 0
  AVOptionVideo qmin 10                       # lower the better, min 0
  AVOptionVideo qmax 42                       # higher outputs bad quality, max 63
  AVOptionVideo quality good
  AVOptionAudio flags +global_header
  PreRoll 15
  StartSendOnKey
  VideoBitRate 400                            # Video bitrate
</Stream>

 

###########################################################################

# Audio only
# run ffmpeg -i audio.mp3 http://localhost:8090/audio.ffm
# run http://localhost:8090/audio in vlc or browser

<Feed audio.ffm>
  File /tmp/audio.ffm
  FileMaxSize 1G
  ACL allow localhost
  ACL allow 127.0.0.1
  ACL allow 192.168.0.0 192.168.255.255
</Feed>

<Stream audio>
  Feed audio.ffm
  Format mp2                                     #audio format
  AudioCodec libmp3lame                          #audio codec
  AudioBitRate 64                                #audio bitrate
  AudioChannels 1                                #audio channel, 1 for mono and 2 for stereo
  AudioSampleRate 44100
  NoVideo                                        #discard video
</Stream>

####################################################################
#view status of ffserver
<Stream stat.html>
  Format status
  ACL allow localhost
  ACL allow 192.168.0.0 192.168.255.255
</Stream>

 

# Redirect index.html to the appropriate site

<Redirect index.html>
  URL http://www.ffmpeg.org/
</Redirect>

We have to be careful while chosing the properties for streaming, you can play around with them and use which is appropriate for you. But, video bitrate, size, frame rate, audio bitrate, codec used must be accurate.
After all the properties are set we've to start ffserver first. If you will start the ffmpeg without starting the ffserver, it will show the error like tcp connection refused like this:
[tcp @ 0x56347fda1360] Connection to tcp://localhost:8090 failed: Connection refused
http://localhost:8090/video.ffm: Connection refused
start the ffserver by typing
ffserver -f /etc/livestream.conf
FFserver will fetch the file located at /etc and start the ffserver
Output in console:
Fri May 12 20:53:02 2017 FFserver started.
Now start ffmpeg so that we can pass the input video and stream it to video.ffm we described inside livestream.conf file
ffmpeg -i Big\ Buck\ Bunny.mp4 http://localhost:8090/video.ffm
We can define all other properties in the above command line like video size, framerate, audio and video codec as well. For more, please visit the ffmpeg documentation.
If everything went well ffmpeg will start writing data to video.ffm located in /tmp directory.
Now, we can read the data from our browser or vlc media player.
In the browser, enter the following link
http://localhost:8090/stream
In vlc, go to media>stream>network and enter the streaming url and press play button.

We can now stream live video using ffmpeg and ffserver.

Comments

Popular posts from this blog

Handling unexpected runtime exception inside Thread. thread.setUncaughtExceptionHandler

Create and set UncaughtExceptionHandler before starting new thread to catch unexpected runtime exception in Multithreading application. package demo; public class ExceptionHandler {     public static void main(String[] args) {         Thread thread = new Thread(new Runnable() {             @Override             public void run() {                 throw new RuntimeException("Critical Error");             }         });         System.out.println("Starting new thread " + Thread.currentThread().getName());         //Throw this exception if any exception has occurred inside running thread but was not caught anywhere else         //This should be set before starting the new thread         thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {             @Override             public void uncaughtException(Thread thread, Throwable throwable) {                 System.out.println("Error occurred on thread: " + thread.getName() + ". Reason :

How to run Spring Boot application inside docker container

Step by Step procedure in creating and running a simple Spring Boot application inside docker container This tutorial will show you on how to run simple Spring Boot application inside docker container. If you have not installed docker already, please install it first. I'm using Linux Mint 18.2. You can follow these steps on How To install Docker in Linux Mint 18.2 After you've successfully installed docker in your machine, let's create a docker image that will run inside the container. Create a simple Spring Boot project from here . Select Web as a dependency. Import the project into you IDE, I'm using Spring Tool Suite. Create a controller and name it HelloController. You can name your controller anything you like. Create a method that will map the request and return a response. Create Dockerfile at the root of the project. The name is important here, only the letter D is capital Make the following entries inside the Dockerfile #Enter all the requi