Use ffmpeg to realize rtmp streaming

1. First download ffmpeg and ffplay


http://ffmpeg.org/

The official download link is: http://ffmpeg.org/

2. cmd enters the directory where ffmpeg is located


cmd enters the directory where ffmpeg.exe is located

3. ffmpeg view computer equipment


Enter the following statement to list the devices of the computer

ffmpeg -list_devices true -f dshow -i dummy

ffmpeg -list_devices true -f dshow -i dummy

You can see that there is a USB2.0 PC CAMERA camera and a garbled microphone in my computer

If the device name is in Chinese, there will be garbled characters. If you want to see the original name of the device, you can check it in the device manager or use a third-party tool to check it. The latter is recommended.

For example, using graphedit, after opening the program, go to Chart -> Insert Filter, and you can see the corresponding device name

It can be found that the available devices are USB2.0 PC CAMERA and microphone (2- USB2.0 MIC)

It can be found that the available devices are USB2.0 PC CAMERA and microphone (2- USB2.0 MIC)

4. Test whether the camera is available


Enter the following statement in cmd and press Enter (USB2.0 PC CAMERA is the name of the camera)

ffplay -f dshow -i video="USB2.0 PC CAMERA"或者ffplay -f vfwcap -i 0

Pop-up monitoring screen

If the playback window pops up successfully, it means that the device is available, otherwise it may be that the device is unavailable or occupied

5. View camera and microphone information


Enter the following statement in cmd to query the camera information

ffmpeg -list_options true -f dshow -i video="USB2.0 PC CAMERA"

ffmpeg -list_options true -f dshow -i video="USB2.0 PC CAMERA"

USB2.0 PC CAMERA camera information

Enter the following statement in cmd to query the microphone information

ffmpeg -list_options true -f dshow -i audio="麦克风 (2- USB2.0 MIC)"

麦克风(2- USB2.0 MIC)信息

6、本地视频的推流


先进行简单的本地视频推流模拟,我们在ffmpeg的目录下放置一个视频,然后cmd进入该目录,把视频推流至rtmp://127.0.0.1:1935/live/123(127.0.0.1:1935为rtmp服务器地址、live为nginx配置节点、123当做密钥,推流拉流地址一样即可播放),语句如下

ffmpeg.exe -re -i demo.wmv -f flv rtmp://127.0.0.1:1935/live/123

推流中...

此时ffmpeg源源不断的把视频推流至服务器,如果地址没错,可以利用vlc或其他手段实现拉流,这里就先不解释如何拉流

7、摄像头推流


接下来正式把对摄像头进行推流,从前面我们知道摄像头名称为USB2.0 PC CAMERA,而且推流服务器ip为127.0.0.1:1935,关键字为live,所以cmd中输入以下语句:

ffmpeg -f dshow -i video="USB2.0 PC CAMERA" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://127.0.0.1:1935/live/123

摄像头推流中

和本地视频推流一样,摄像头拍到的画面会实时推流出去(当然会有延迟而且现在是没有声音的),当地址正确时,可以实现拉流

8、麦克风推流


前面介绍了摄像头画面推流,可是没有声音,这次我们把麦克风声音推流出去,cmd中输入下面语句

ffmpeg -f dshow -i audio="麦克风 (2- USB2.0 MIC)" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flvrtmp://127.0.0.1:1935/live/123

推送声音

和前面差不多,声音被推流出去了,通过vlc拉流可以听到录制的声音,但很明显不会有画面

9、摄像头&麦克风推流


终于到最激动人心的时刻了,我们这次要实现同时推流摄像头画面与声音,此时我们的语句应该如下

ffmpeg -f dshow -i video="USB2.0 PC CAMERA" -f dshow -i audio="麦克风 (2- USB2.0 MIC)" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flvrtmp://127.0.0.1:1935/live/123或者ffmpeg -f dshow -i video="USB2.0 PC CAMERA":audio="麦克风 (2- USB2.0 MIC)" -vcodec libx264 -r 25 -preset:v ultrafast -tune:v zerolatency -f flvrtmp://127.0.0.1:1935/live/123

监控画面与声音同步推流

很nice,和前面一样,画面与声音源源不断的被推流到服务器,接下来我们就应该正式的开发拉流了。

原文https://zhuanlan.zhihu.com/p/426063448

★文末名片可以免费领取音视频开发学习资料,内容包括(FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)以及音视频学习路线图等等。

见下方!↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/129385369