Video streaming with ffmpeg

Ffmpeg is a complete, cross-platform solution to record, convert and stream audio and video.

Installation steps

# sh -c 'echo "deb http://www.deb-multimedia.org jessie main" >> /etc/apt/sources.list'
# apt-get update
# apt-get install deb-multimedia-keyring
# apt-get install ffmpeg
# apt-get install v4l-utils

Configure ffserver

Save this content in /etc/ffserver.conf:

HTTPPort 8080                      # Port to bind the server to
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 10000             # Maximum bandwidth per client
                               # set this high enough to exceed stream bitrate
CustomLog -

<Feed feed.ffm>
     File ./feed.ffm
     FileMaxSize 1g
     ACL allow 127.0.0.1
</Feed>

<Stream feed.webm>
     Format webm
     Feed feed.ffm
     VideoCodec libvpx
     VideoSize 320x240
     VideoFrameRate 15
     VideoBitRate 512
     VideoBufferSize 512
     NoAudio
     AVOptionVideo flags +global_header
     StartSendOnKey
</Stream>

<Stream status.html>            # Server status URL
   Format status
   # Only allow local people to get the status
   ACL allow 192.168.1.0 192.168.1.255
</Stream>

#<Redirect index.html>    # Just an URL redirect for index
#   # Redirect index.html to the appropriate site
#  URL /
#</Redirect>

Then run:

$ ffserver

ffmpeg

$ ffmpeg -f v4l2 -i /dev/video0 http://localhost:8080/feed.ffm

Remote access from aweb browser

http://ip_address:8080/status.html

or

http://ip_address:8080/feed.webm

or

<video width="320" height="240" autoplay>
  <source src="http://192.168.1.3:8080/feed.webm" type="video/webm">
Your browser does not support the video tag.
</video>

Links

Appunti su ffmpeg

Coversione da audio stereo a audio mono

ffmpeg -i audio_stereo.m4a -ac 1 audio_stereo.mp3

Registrazione da stream mjpg

ffmpeg -i http://cam4.local:8001/stream.mjpg -an -vcodec h264 file.mp4

Watermark

ffmpeg -i http://cam4.local:8001/stream.mjpg -i watermark.png -filter_complex "overlay=10:10" -an -vcodec h264 file1.mp4    

Data e ora

ffmpeg -i http://cam4.local:8001/stream.mjpg -vf "drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf: text='%{localtime}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1: fontsize=20" -an -vcodec h264 file1.mp4

Conversione serie di jpg in gif

ffmpeg -f image2 -framerate 6 -i img%3d.jpg -vf scale=800x600 out.gif

Scalare una immagine

ffmpeg -i photo1.jpg -vf scale=w=128:h=-1 photos_128/photo1.jpg 

Convertire un gruppo di immagini:

for f in *.jpg; do
  ffmpeg -i $f -vf scale=w=1920:h=-1 scalate/$f 
done

Applicare un Watermark ad una serie di foto

for f in *.jpg; do 
    ffmpeg -i "$f" -i watermark.png -filter_complex "overlay=10:10" "${f/%.jpg/-v.jpg}"; 
done

Convertire una intera directory di video da un formato all'altro

for i in *.wmv; do ffmpeg -i "$i" "${i%.*}.mp4"; done