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 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
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

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

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

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

Then run:

$ ffserver


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

Remote access from aweb browser





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


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


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 

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}"; 

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

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