创建新的nodejs服务文件
加载本模块
npm install node-media-server
服务文件代码
const NodeMediaServer = require('node-media-server');
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 60,
ping_timeout: 30
},
http: {
port: 8000,
allow_origin: '*'
}
};
var nms = new NodeMediaServer(config)
nms.run();
运行此文件开启流媒体服务器
ffmpeg推送流
如果您有一个带有H.264视频和AAC音频的视频文件:
ffmpeg -re -i 视频文件地址 -c copy -f flv rtmp:// localhost / live / STREAM_NAME
或者,如果您有以其他音频/视频格式编码的视频文件:
ffmpeg -re -i 视频文件地址 -c:v libx264 -preset superfast -tune zerolatency -c:aac -ar 44100 -f flv rtmp:// localhost / live / STREAM_NAME
(使用cmd命令方式访问,请下载FFmpeg软件版,在解压文件夹使用cmd命令)
实时电脑摄像头采集
ffmpeg -f dshow -i video="摄像头名":audio="麦克风名"
-vcodec libx264 -acodec libvo_aacenc -b 1080k -r 33 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://localhost:1935/live/STREAM_NAME
访问实时流
RTMP
rtmp://localhost/live/STREAM_NAME
HTTP的FLV
http://localhost:8000/live/STREAM_NAME.flv
WebSocket的,FLV
ws://localhost:8000/live/STREAM_NAME.flv
HLS
http://localhost:8000/live/STREAM_NAME/index.m3u8
短跑
http://localhost:8000/live/STREAM_NAME/index.mpd
通过http-flv通过flv.js
<script src="https://cdn.bootcss.com/flv.js/1.4.0/flv.min.js"></script>
<video id="videoElement"></video>
<script>
if (flvjs.isSupported()) {
var videoElement = document.getElementById('videoElement');
var flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'http://localhost:8000/live/STREAM_NAME.flv'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
}
</script>
通过websocket-flv通过flv.js
<script src="https://cdn.bootcss.com/flv.js/1.4.0/flv.min.js"></script>
<video id="videoElement"></video>
<script>
if (flvjs.isSupported()) {
var videoElement = document.getElementById('videoElement');
var flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'ws://localhost:8000/live/STREAM_NAME.flv'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
}
</script>