Use FFMPEG to get data from the DirectShow device (camera, screen recording)

Source: Internet
Author: User

In the past two days, I have studied how to get data from the DirectShow device by using FFMPEG. Here I will record it for memo. The methods described in this article mainly correspond to Windows platforms.

1. Column Devices


ffmpeg -list_devices true -f dshow -i dummy


After the command is executed, the output result is as follows (Note: Chinese devices may encounter garbled characters ). The name of the device displayed in the list is very important. You can enter "-F dshow-I video =" {Device name.


The following devices are listed on my machine:


[Dshow @ override] DirectShow video devices [dshow @ 0388f5e0] "integrated camera" [dshow @ 0388f5e0] "Screen-capture-recorder" [dshow @ override] DirectShow audio devices [dshow @ 0388f5e0? (Conexant20672 smartaudi "[dshow @ 0388f5e0]" virtual-audio-Capturer"


In the following test, two video inputs are used: "integrated camera" and "Screen-capture-recorder ".

Note: Garbled audio devices have not been resolved yet.

2. get the camera data (save as a local file or send a real-time stream)2.1. encode it as H.264 and save it as a local file

In the following command, we can read data from the camera and save it as H.264 and save it as mycamera.mkv.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 mycamera.mkv
2.2. Directly Play the camera data

Use ffplay to directly play the camera data. The command is as follows:


ffplay -f dshow -i video="Integrated Camera"


If the device name is correct, the camera on the local device is directly turned on ,.


Note: In addition to using DirectShow as the input, you can also use VFW to read the data of the camera. For example, the following command can play the data of the camera:


ffplay -f vfwcap -i 0


2.3. encoding: H.264, UDP released

The following command is used to get the camera data-> encode it as H.264-> encapsulate it as UDP and send it to the multicast address.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f h264 udp://233.233.233.223:6666


Note 1: To increase the encoding speed of libx264, the-preset: V ultrafast and-tune: V zerolatency options are added.

NOTE 2: when the resolution is high, UDP may cause packet loss. To avoid this, you can add the-S parameter (for example,-s 320x240) to reduce the resolution.

2.4. encoding: H.264, released RTP

The following command is used to get the camera data-> encode it as H.264-> encapsulate it as RTP and send it to the multicast address.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f rtp rtp://233.233.233.223:6666>test.sdp


Note 1: To increase the encoding speed of libx264, the-preset: V ultrafast and-tune: V zerolatency options are added.

Note 2: "> test. SDP" can be added at the end to generate the SDP file at the same time as the release. This file can be used to play the video stream.

2.5. encoding: H.264, rtmp released

The following command is used to get the camera data-> encode it as H.264-> and send it to the rtmp server.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://localhost/oflaDemo/livestream


2.6. Code MPEG2 and release UDP

Similar to encoding H.264, specify-vcodec.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec mpeg2video -f mpeg2video udp://233.233.233.223:6666


The UDP stream for playing MPEG2 is as follows. Specify-vcodec is mpeg2video.


ffplay -vcodec mpeg2video udp://233.233.233.223:6666


 

3. screen recording (save as a local file or send a real-time stream on Windows)

In Linux, it is relatively convenient to use FFMPEG for screen recording. You can use x11grab and use the following command:


ffmpeg -f x11grab -s 1600x900 -r 50 -vcodec libx264 –preset:v ultrafast –tune:v zerolatency -crf 18 -f mpegts udp://localhost:1234


For more information, see topstreaming with FFMPEG for lower latency.

Linux screen recording will not be described here. Screen recordings on Windows platforms are a little more complex. On Windows, replace x11grab with-dshow. One statement: register the screen recording dshow filter (for example, screen-capture-recorder), get the screen recording image through dshow, and then encode it.

Therefore, you must install the dshow filter before using FFMPEG screen recording. We recommend a software: screen capture recorder. After installing the software, you can use FFMPEG screen recording.

 

Screen capture recorder project homepage:

Http://sourceforge.net/projects/screencapturer/

:

Http://sourceforge.net/projects/screencapturer/files

After the download is complete, the installation can be completed through "Next. Note: The Java Runtime Environment (Java Runtime Environment) is required. If not, download one.

Screen capture recorder itself can be used for screen recording, but here we use FFMPEG for screen recording.


3.1. encode it as H.264 and save it as a local file

The following command can encode the screen recording as H.264 and save it as a local file.


ffmpeg -f dshow -i video="screen-capture-recorder" -r 5 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency MyDesktop.mkv


Note: "-R 5" means to set the frame rate to 5.

The final result is as follows.


In addition, sound recording can also be used. Sound input can be divided into two types: one is the voice of real people, input through the microphone; the other is the virtual voice, that is, the sound in the headset of the computer during screen recording. The following two commands can respectively record the microphone sound and the sound in the computer headset.

Screen recording with microphone input sound


FFmpeg-F dshow-I video = "Screen-capture-recorder"-F dshow-I audio = "Why are there too many audio streams? (Conexant 20672 smartaudi "-R 5-vcodec libx264-preset: V ultrafast-tune: V zerolatency-acodec libmp 3lame mydesktop.mkv


The above command has a problem: audio there are garbled, The garbled ansi to the UTF-8 after (garbled into "built-in Mike") still not good, this problem first keep, and then have time to solve.

PS: I think this command is suitable for lectures and so on.

 

Screen recording, with the sound of headset Input


ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="virtual-audio-capturer" -r 5 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -acodec libmp3lame MyDesktop.mkv


PS: When testing this command, this is listening to the song, so the audio in the recorded video is the song.

 

3.2. encoding: H.264, UDP released

The following command can encode the screen recording as H.264 and encapsulate it into UDP and send it to the multicast address.


ffmpeg -f dshow -i video="screen-capture-recorder" -r 5 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f h264 udp://233.233.233.223:6666


Note 1: To increase the encoding speed of libx264, the-preset: V ultrafast and-tune: V zerolatency options are added.

NOTE 2: when the resolution is high, UDP may cause packet loss. To avoid this, you can add the-S parameter (for example,-s 320x240) to reduce the resolution.

3.3. encoding: H.264, released RTP

The following command can encode the screen recording as H.264 and encapsulate it as RTP and send it to the multicast address.


ffmpeg -f dshow -i video="screen-capture-recorder" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f rtp rtp://233.233.233.223:6666>test.sdp


Note 1: To increase the encoding speed of libx264, the-preset: V ultrafast and-tune: V zerolatency options are added.

Note 2: "> test. SDP" can be added at the end to generate the SDP file at the same time as the release. This file can be used to play the video stream. Run the following command to play the video:


ffplay test.sdp


3.4. encoding: H.264, rtmp released

The principle is the same as above.


ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://localhost/oflaDemo/livestream


Note: When playing rtmp, the-max_delay parameter will significantly affect the delay. setting this parameter value smaller will help reduce the delay.


ffplay -max_delay 100000 "rtmp://localhost/oflaDemo/livestream live=1"



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.