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

Source: Internet
Author: User

In the past two days, I have studied how to obtain 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 [Plain]View plaincopy
  1. 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:

[Plain]View plaincopy
  1. [Dshow @ 0388f5e0] DirectShow video devices
  2. [Dshow @ 0388f5e0] "integrated camera"
  3. [Dshow @ 0388f5e0] "Screen-capture-recorder"
  4. [Dshow @ 0388f5e0] DirectShow audio devices
  5. [Dshow @ 0388f5e0] "Why are there too many attempts? (Conexant20672 smartaudi"
  6. [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. Obtain 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.

[Plain]View plaincopy
  1. 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:

[Plain]View plaincopy
  1. 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:

[Plain]View plaincopy
  1. Ffplay-F vfwcap-I 0
2.3. encoding: H.264, UDP released

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

[Plain]View plaincopy
  1. 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 obtain the camera data-> encode it as H.264-> encapsulate it as RTP and send it to the multicast address.

[Plain]View plaincopy
  1. FFmpeg-F dshow-I video = "integrated camera"-vcodec libx264-preset: V ultrafast-tune: V zerolatency-F 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.

[Plain]View plaincopy
  1. 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.

[Plain]View plaincopy
  1. 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.

[Plain]View plaincopy
  1. 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:

[Plain]View plaincopy
  1. 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), obtain 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.

[Plain]View plaincopy
  1. 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

[Plain]View plaincopy
  1. 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

[Plain]View plaincopy
  1. 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 libmp 3lame 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.

[Plain]View plaincopy
  1. 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.

[Plain]View plaincopy
  1. FFmpeg-F dshow-I video = "Screen-capture-recorder"-vcodec libx264-preset: V ultrafast-tune: V zerolatency-F 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:

[Plain]View plaincopy
  1. Ffplay test. SDP
3.4. encoding: H.264, rtmp released

The principle is the same as above.

[Plain]View plaincopy
  1. 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.

[Plain]View plaincopy
  1. 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.