使用 Node Media Server 和 FFmpeg 创建直播流,推送本地视频

Node.js安装:

参考这个
Nodejs安装教程

安装完成之后,记得修改一下npm源:

javascript 复制代码
npm config set registry https://registry.npmmirror.com

下一步,安装node-media-server

javascript 复制代码
npm install node-media-server

参考这个:安装ffmpeg

下一步:创建js脚本 app.js,内容如下:

javascript 复制代码
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
const async = require('async');
const NodeMediaServer = require('node-media-server');

// 创建node-media-server实例
const nmsConfig = {
    rtmp: {
        port: 9999,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: 8000,
        mediaroot: './media',
        allow_origin: '*'
    }
};
const nms = new NodeMediaServer(nmsConfig);
nms.run();

// 根据指定的目录和文件类型创建要推流的文件列表
const videoDir = 'E:\Build';
const allowedExtensions = ['.mp4', '.avi', '.mov'];

const fileList = [];
fs.readdirSync(videoDir).forEach(file => {
    const fileExtension = path.extname(file);
    if (allowedExtensions.includes(fileExtension)) {
        fileList.push(path.join(videoDir, file));
    }
});

if (!fileList.length) {
    console.log('No video files found!');
    process.exit(1);
}

// 配置FFmpeg推流命令行参数
const ffmpegCommand = `ffmpeg -re -stream_loop -1 -i "$input$"  -c copy -f flv "$output$"`;
const ffmpegEscapeMap = { '$input$': '', '$output$': '' };

let index = 0;
const maxIndex = fileList.length - 1;

async.each(fileList, (videoPath, callback) => {
    // 构建FFmpeg命令行参数
    ffmpegEscapeMap['$input$'] = videoPath;
    // node-media-server IP即可,无需填写端口默认端口1935 服务端获取的RTMP流也是这个地址
    ffmpegEscapeMap['$output$'] = `rtmp://88.22.10.113:9999/live/test`;
    const output = `rtmp:88.22.10.113:9999/live/test`;
    const command = ffmpegCommand.replace(/\$\w+\$/g, m => ffmpegEscapeMap[m]);
    console.log(`Starting streaming for file: ${videoPath}...`);

    // 调用FFmpeg推流
    const ffProcess = spawn("ffmpeg"
        ,['-re','-stream_loop','-1','-i',videoPath,'-c','copy','-f','flv',output]
        , { detached: true });

    // 捕捉事件
    ffProcess.on('exit', () => {
        console.log(`Stop streaming for file: ${videoPath}.`);
        if (++index > maxIndex) {
            index = 0; // 循环推流
        }
    });
    ffProcess.on('close', code => {
        console.log(`Process ${path.parse(videoPath).name} exited with code ${code}`);
        if (++index > maxIndex) {
            index = 0; // 循环推流
        }
    });

    ffProcess.stderr.on('data', (data) => {
        console.log(`${videoPath}: ${data.toString()}`);
    });

    callback();
})
console.log(`Started pushing ${fileList.length} stream(s) to node-media-server on rtmp://88.22.10.113:9999/live/.`);

,记得修改里面的videoDir以及ip地址。

最后node app.js,至此已经推流成功。

测试:安装vlc播放器,然后播放网络流,地址:rtmp://88.22.10.113:9999/live/test,记得修改IP

javascript 复制代码
// 推送视频:xm0525$ ffmpeg -re -i 视频名称 -c copy -f flv rtmp://ip:1935/live/STREAM_NAME
// 推送摄像头:ffmpeg -f avfoundation -video_size 640x480 -framerate 30 -i 0:0 -vcodec libx264 -preset veryfast -f flv rtmp://ip:1935/live/STREAM_NAME
// 推送屏幕:ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://ip:1935/live/STREAM_NAME
相关推荐
淡淡的香烟2 小时前
Android Camera开发详解
android·音视频
≮傷£≯√7 小时前
图片合并成视频 img2video ffmpeg
ffmpeg·音视频
A13345559 小时前
视频特效字幕怎么翻译?保姆级AI字幕与外挂字幕教程
人工智能·音视频
ViiTor_AI21 小时前
从去字幕到 AI 配音:完整视频本地化流程
人工智能·音视频
明德扬1 天前
多路MIPI摄像头如何同步聚合?一文看懂FPGA视频汇聚方案
fpga开发·音视频
HY小宝F1 天前
树莓派 FFmpeg 实战笔记(二):命令行、源码编译与硬件编码的真相
学习·职场和发展·ffmpeg
折枝吖1 天前
音视频开发-PCM 原理与 AI 模块实战
音视频·pcm
HySpark1 天前
熙瑾·会悟 ASR 实战:Qwen-ASR 漏字导致转写不完整,如何从音频分段到二次校验解决?
音视频·熙瑾会悟
qq_316837751 天前
uniapp + Vite + ffmpeg-wasm 0.12.x 集成示例
ffmpeg·uni-app·wasm
qingmiaozhuan1 天前
电脑录屏黑屏?只有声音没画面?从原理到排查一篇搞定
ffmpeg·电脑·音视频开发·录屏技术·电脑录屏