Changes between Initial Version and Version 1 of Capture/V4L2_ALSA


Ignore:
Timestamp:
Nov 6, 2019, 9:44:48 PM (9 months ago)
Author:
JefS
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Capture/V4L2_ALSA

    v1 v1  
     1== Introduction ==
     2
     3This page explain how to use a capture card in Linux to capture both Audio and Video
     4
     5What you need to keep in mind is that video and audio use different ways to reach ffmpeg in a Linux system. Audio will use [https://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture ALSA] and Video will use [https://en.wikipedia.org/wiki/Video4Linux V4L2]
     6
     7== Video stream ==
     8
     9Find your capture card
     10
     11{{{
     12v4l2-ctl --list-devices
     13}}}
     14
     15Chances are that you will end up using /dev/video0 is you only have a single capture card in your machine.
     16
     17Check the supported video resolutions and frame rates :
     18
     19{{{
     20ffmpeg  -hide_banner -f v4l2 -list_formats all -i /dev/video0
     21}}}
     22
     23or
     24
     25{{{
     26v4l2-ctl --list-formats-ext
     27}}}
     28
     29Based on the discovered information, the capture section of ffmpeg will look like :
     30
     31{{{
     32ffmpeg \
     33        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0
     34}}}
     35
     36== Audio stream ==
     37
     38Find your capture card
     39
     40{{{
     41arecord -L
     42}}}
     43
     44Note that I'm using a big "L" here to have symbolic names instead of index numbers.
     45
     46You want to select the audio stream that is coming directly from your capture card. Those streams always begin with "hw:"
     47
     48Based on the discovered information, the capture section of ffmpeg will look like :
     49
     50{{{
     51ffmpeg \
     52        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0
     53}}}
     54
     55Note the "-ac 2" to select stereo input.
     56
     57== Compression ==
     58
     59There 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).
     60
     61{{{
     62ffmpeg \
     63        -c:v libx264 -b:v 1600k -preset ultrafast \
     64        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
     65        -c:a aac -b:a 128k \
     66}}}
     67
     68Depending on your needs, you will change the bitrate and the preset:
     69* 1.6Mbps is good for SD/HD-ready streaming on the net
     70* preset ultrafast is not loading your CPU too much
     71* 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
     72
     73== Destinations ==
     74
     75=== File ===
     76
     77Just finish your ffmpeg command with a filename :
     78
     79{{{
     80ffmpeg \
     81        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
     82        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
     83        -c:v libx264 -b:v 1600k -preset ultrafast \
     84        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
     85        -c:a aac -b:a 128k \
     86        file.mp4
     87}}}
     88
     89=== Streaming on your web site ===
     90
     91If you have a web server, you can send your captured video to it using [https://en.wikipedia.org/wiki/HTTP_Live_Streaming HTTP Live Streaming (HLS)] :
     92
     93{{{
     94ffmpeg \
     95        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
     96        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
     97        -c:v libx264 -b:v 1600k -preset ultrafast \
     98        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
     99        -c:a aac -b:a 128k \
     100        -f ssegment -segment_list playlist.m3u8 -segment_list_flags +live \
     101        -segment_time 10 out%03d.ts
     102}}}
     103
     104=== Streaming to your LAN ===
     105
     106You 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.
     107
     108{{{
     109ffmpeg \
     110        -f v4l2 -framerate 25 -video_size 960x540 -i /dev/video0 \
     111        -f alsa -ac 2 -i hw:CARD=HDMI,DEV=0 \
     112        -c:v libx264 -b:v 1600k -preset ultrafast \
     113        -x264opts keyint=50 -g 25 -pix_fmt yuv420p \
     114        -c:a aac -b:a 128k \
     115        -f rtp_mpegts rtp://239.0.0.2:5001?ttl=2
     116}}}
     117
     118With 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 :
     119
     120{{{
     121rtp://239.0.0.2:5001
     122}}}
     123
     124