linux下使用ffmpeg指令从摄像头中录取视频

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 (), we need to use 2 different drivers for video and audio:

    video capture - v4l2 () audio capture - alsa ()
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 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

    go to menu Media->Open Network Stream... in URL field type udp://@0.0.0.0:5000 and press Play
经验分享 程序员 微信小程序 职场和发展