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

Spring Security with Spring Boot and MySQL

Spring Security with Spring Boot and MySQL In this tutorial I'll show how to use Spring Security with Spring Boot and MySQL. We'll implement a UserDetailsService provided by Spring Security. It is easy to configure Spring Security with Spring Boot and MySQL. You can also check how to run Spring Boot application inside docker container Steps: 1. Create a Spring Boot project from start.spring.io with following dependencies in your pom.xml file. You can choose gradle also for project dependencies. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- dev tools for hot reloading --> <dependency> <groupId>org.springframework.b...

How To install Docker in Linux Mint 18.2

How To install docker in Linux Mint 18.2 Codename: sonya Docker is a Container as a service (CaaS) platform which helps to eliminate "works in my computer" problems when working together with other members in the team. More here Docker for Linux Mint is availabe as a free Community Edition (CE). Installation is easy and straigh forward. We will install a stable release of docker. OS Requirements Linux Mint 18.2 (LTS) Architecture: x86_64 If you've the older version installed already, uninstall it $ sudo apt-get remove docker docker-engine docker.io Add repositories $ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - Since, Linux Mint 18.2 (sonya) is based on Ubuntu 16.04 (xenial), we've to use the release codename of Ubuntu 16.04 i.e. xenial. Also, we'll use the latest stable version $ sudo add-apt-repository "deb [arch=a...