wiki:Capture/V4L2_ALSA

Introduction

This page explain how to use a capture card in Linux to capture both Audio and Video

What you need to keep in mind is that video and audio use different ways to reach ffmpeg in a Linux system. Audio will use ALSA and Video will use V4L2

Video stream

Find your capture card

v4l2-ctl --list-devices

Chances are that you will end up using /dev/video0 is you only have a single capture card in your machine.

Check the supported video resolutions and frame rates :

ffmpeg  -hide_banner -f v4l2 -list_formats all -i /dev/video0

or

v4l2-ctl --list-formats-ext

Based on the discovered information, the capture section of ffmpeg will look like :

ffmpeg \
        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0

Audio stream

Find your capture card

arecord -L

Note that I'm using a big "L" here to have symbolic names instead of index numbers.

You want to select the audio stream that is coming directly from your capture card. Those streams always begin with "hw:"

Based on the discovered information, the capture section of ffmpeg will look like :

ffmpeg \
        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0

Note the "-ac 2" to select stereo input.

Compression

There is a lot to be said about compression that you can find elsewhere on this wiki. If you are in a hurry, try with those settings (PAL/25fps region, adapt if you are in NTSC/30fps region).

ffmpeg \
        -c:v libx264 -b:v 1600k -preset ultrafast \
        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
        -c:a aac -b:a 128k \

Depending on your needs, you will change the bitrate and the preset:

  • 1.6Mbps is good for SD/HD-ready streaming on the net
  • preset ultrafast is not loading your CPU too much
  • if you want to record the stream, raise the bandwidth as much as you can at the time of recording, then compress using slower preset after the capture

Destinations

File

Just finish your ffmpeg command with a filename :

ffmpeg \
        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
        -c:v libx264 -b:v 1600k -preset ultrafast \
        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
        -c:a aac -b:a 128k \
        file.mp4

Streaming on your web site

If you have a web server, you can send your captured video to it using HTTP Live Streaming (HLS) :

ffmpeg \
        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
        -c:v libx264 -b:v 1600k -preset ultrafast \
        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
        -c:a aac -b:a 128k \
        -f ssegment -segment_list playlist.m3u8 -segment_list_flags +live \
        -segment_time 10 out%03d.ts

Streaming to your LAN

You can send your captured video to a multicast address, so that it can be viewed on many devices on your LAN. The advantage of multicast is that your encoder will only send the video stream once on the network, whatever the number of viewers you have.

ffmpeg \
        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
        -c:v libx264 -b:v 1600k -preset ultrafast \
        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
        -c:a aac -b:a 128k \
        -f rtp_mpegts rtp://239.0.0.2:5001?ttl=2

With the settings above, you can use any rtp-compatible client to view the stream. For instance, in VLC, go to Media > Open Network Stream and use this address :

rtp://239.0.0.2:5001
Last modified 5 years ago Last modified on Nov 6, 2019, 7:44:48 PM
Note: See TracWiki for help on using the wiki.