/ FFmpeg

Stream camera video and audio with FFmpeg

4youngpadawans.com presents Stream camera video and audio with FFmpeg featuring FFmpeg | Multimedia


FFmpeg is one of leading frameworks for multimedia processing. Among variety of features, FFmpeg can capture video and audio from your computer's camera and stream them over network to some other remote machine.

Use cases

It becomes more and more common that there is at least one desktop computer or lap top at your home with sole purpose to collect dust. Have you ever wondered if you can put it back in action to do something more useful?

Well I have an idea for you: turn it into live surveillance device

  • (for fresh parents) to monitor baby's cradle and listen if baby started to complain
  • (for housewives) to remotely monitor kitchen activities and see if soup cooking transformed into volcano
  • (for businessmen) to watch TV football match while playing video game
  • (for teenagers) to monitor if mommy entered their room to read secret diaries

Install FFmpeg

Windows 7 or later

  • go to FFmpeg builds web page, choose Windows 64-bit architecture, static linking and Download build.
  • create directory c:\\ffmpeg and extract zip content into it,
  • go to Control Panel->System->Edit system environment variables and click on Environment variables
  • in System variables locate and select Path row, click Edit and add value c:\ffmpeg\bin.

Linux Ubuntu

Open Terminal and execute following

$ sudo apt-get install ffmpeg
$ sudo apt-get install v4l-utils

Discover camera and microphone system names

Windows

From command line prompt type and execute

ffmpeg -list_devices true -f dshow -i dummy
Linux

From terminal type and execute

$ v4l2-ctl --list-devices

and you will see all video and audio devices available on your system. Something like this

ffmpeg version N-90264-g80798e3857 Copyright (c) 2000-2018 the FFmpeg ...
[dshow @ 00000128ed43a5c0] DirectShow video devices (some may be both video and audio devices)
[dshow @ 00000128ed43a5c0]  "Integrated Webcam"
[dshow @ 00000128ed43a5c0]     Alternative name "@device_pnp_\\?\usb#vid_0c45&pid_6713&mi_00#6&4851259&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
[dshow @ 00000128ed43a5c0] DirectShow audio devices
[dshow @ 00000128ed43a5c0]  "Microphone (Realtek Audio)"
[dshow @ 00000128ed43a5c0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{7FB8E837-0274-41D1-8DF1-B93D69E57620}"

Find and note video and audio device names. In this example

  • video device name is Integrated Webcam
  • audio device name is Microphone (Realtek Audio)

Discover computer IP address

First discover IP address of computer or smart phone where you want to watch stream from camera:

  • on windows: execute ipconfig from command prompt,
  • on linux: execute ifconfig from terminal
  • on android: go to Settings->WiFi->Advanced settings

Usually IP address will be something like 192.168.1.x. Note it down.

Capture and stream video and audio

Now, on machine with camera, open Terminal/command prompt and run following command to start capturing video and audio and generating live stream to another computer

Windows
ffmpeg -f dshow -i video="Integrated Webcam":audio="Microphone (Realtek Audio)" -profile:v high -pix_fmt yuvj420p -level:v 4.1 -preset ultrafast -tune zerolatency -vcodec libx264 -r 10 -b:v 512k -s 640x360 -acodec aac -ac 2 -ab 32k -ar 44100 -f mpegts -flush_packets 0 udp://192.168.1.4:5000?pkt_size=1316
Linux

On linux, instead of dshow (DirectShow), we need to use 2 different drivers for video and audio:

ffmpeg -f v4l2 -i video="Integrated Webcam" -f alsa -i hw:0 -profile:v high -pix_fmt yuvj420p -level:v 4.1 -preset ultrafast -tune zerolatency -vcodec libx264 -r 10 -b:v 512k -s 640x360 -acodec aac -strict -2 -ac 2 -ab 32k -ar 44100 -f mpegts -flush_packets 0 udp://192.168.1.4:5000?pkt_size=1316

Do not forget to replace video and audio devices names and destination IP address with previously discovered values.

Options demystified

Lets reformat previous command so that we can see used options better

ffmpeg -f dshow \
-i video="Integrated Webcam":audio="Microphone (Realtek Audio)" \
-profile:v high -pix_fmt yuvj420p -level:v 4.1 -preset ultrafast -tune zerolatency \
-vcodec libx264 -r 10 -b:v 512k -s 640x360 \
-acodec aac -ac 2 -ab 32k -ar 44100 \
-f mpegts -flush_packets 0 udp://192.168.1.4:5000?pkt_size=1316
  • -f fshow - windows system drivers for capturing video and audio
  • -f v4l2 - linux system drivers for capturing video
  • -f alsa - linux system drivers for capturing audio
  • -i - ffmpeg option that defines input
  • -vcodec libx264 - raw video from camera will be encoded using H264 video codec
  • -r 10 - video FPS (frames per second)
  • -b:v 512k - video bitrate Kb/s (kilo bits per second)
  • -s 640x360 - video width and height
  • -acodec aac - raw audio from microphone will be encoded using AAC audio codec
  • -ac 2 - 2 audio channels (stereo)
  • -ab 32k - audio bitrate in Kb/s
  • -ar 44100 - audio sampling rate 44.1 KHz
  • -f mpegts - video and audio will be packed into MPEG transport stream (MPEG TS)
  • udp://192.168.1.4:5000 - MPEG transport stream is sent via UDP protocol to computer with IP address 192.168.1.4 on IP port 5000.

Play camera stream

Windows/Linux

On destination computer stream can be watched using VLC player

  • go to menu Media->Open Network Stream...
  • in URL field type udp://@0.0.0.0:5000 and press Play
Android

Install MX Player, from top menu go to Network stream, type udp://127.0.0.1:5000 in URL field and press OK.

Happy spying !!! :)

Stream camera video and audio with FFmpeg
Share this