运行
localhost/1.html
server.js

// ✅ 正确导入方式
const NodeMediaServer = require('node-media-server');

// RTMP 服务配置
const config = {
  rtmp: {
    port: 7003,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 7001,
    allow_origin: '*'
  }
  // 注释掉 https 部分,除非你有证书文件
  /*
  https: {
    port: 7002,
    key: './privatekey.pem',
    cert: './certificate.pem'
  }
  */
};

// 启动服务器
const nms = new NodeMediaServer(config);
nms.run();

// 监听事件
nms.on('preConnect', (id, args) => {
  console.log('[NodeEvent] 收到连接请求:', new Date());
});

nms.on('postPublish', (id, StreamPath, args) => {
  // 做个安全检查,防止 StreamPath 为 undefined
  if (!StreamPath) {
    console.log(`[直播启动] 未知流地址 (ID: ${id})`);
    return;
  }
  const streamName = StreamPath.split('/').pop(); // 获取最后的流名,如 movie
  console.log(`[直播启动] 流名: ${streamName} @ ${new Date().toLocaleString()}`);
});

nms.on('donePublish', (id, StreamPath, args) => {
  if (!StreamPath) {
    console.log(`[直播结束] ID: ${id}`);
    return;
  }
  const streamName = StreamPath.split('/').pop();
  console.log(`[直播结束] 流名: ${streamName} 已断开`);
});



console.log('✅ FLV 直播服务器已启动!');
console.log('📍 RTMP 推流地址: rtmp://localhost:7003/live/movie');
console.log('📍 HTTP-FLV 播放地址: http://localhost:7001/live/movie.flv');
console.log('📍 管理接口: http://localhost:7001/api/streams');
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <title>FLV.js 全功能播放器</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f4f4f4;
      text-align: center;
      padding: 20px;
    }
    h1 {
      color: #333;
    }
    .container {
      max-width: 900px;
      margin: 0 auto;
      padding: 20px;
      background: white;
      border-radius: 10px;
      box-shadow: 0 0 15px rgba(0,0,0,0.1);
    }
    video {
      width: 100%;
      max-width: 800px;
      height: auto;
      border-radius: 8px;
      border: 1px solid #ddd;
    }
    .status {
      margin-top: 10px;
      padding: 10px;
      font-size: 14px;
      color: #555;
      background: #f0f0f0;
      border-radius: 6px;
    }
    .info {
      margin-top: 20px;
      font-size: 14px;
      color: #777;
      line-height: 1.6;
    }
  </style>
  <!-- 加载 flv.js -->
  <script src="https://cdn.jsdelivr.net/npm/flv.js@latest/dist/flv.min.js"></script>
</head>
<body>

<div class="container">
  <h1>🎥 FLV.js 直播播放器</h1>
  <p>正在连接直播流:<code>http://localhost:7001/live/movie.flv</code></p>

  <video id="videoElement" controls autoplay muted></video>
  <div class="status" id="status">状态:初始化...</div>

  <div class="info">
    <p><strong>推流地址:</strong> rtmp://localhost:7003/live/movie</p>
    <p>使用 OBS 或 FFmpeg 推流即可观看。</p>
  </div>
</div>

<script>
  const videoElement = document.getElementById('videoElement');
  const statusDisplay = document.getElementById('status');

  function updateStatus(msg) {
    statusDisplay.textContent = msg;
    console.log('[Player] ' + msg);
  }

  if (flvjs.isSupported()) {
    const flvPlayer = flvjs.createPlayer({
      type: 'flv',
      url: 'http://localhost:7001/live/movie.flv'
    });

    flvPlayer.attachMediaElement(videoElement);

    // 事件监听
    flvPlayer.on(flvjs.Events.ERROR, (e, data) => {
      updateStatus(`❌ 播放错误: ${data}`);
    });

    flvPlayer.on(flvjs.Events.LOADING_COMPLETE, () => {
      updateStatus('⏹ 播放结束');
    });

    flvPlayer.on(flvjs.Events.METADATA_ARRIVED, () => {
      updateStatus('✅ 元数据已到达,播放中...');
    });

    try {
      flvPlayer.load();
      updateStatus('⏳ 加载中...');

      // 自动播放(若被阻止,则提示用户点击)
      flvPlayer.play().catch(err => {
        updateStatus('⚠️ 自动播放被阻止,请手动点击播放按钮');
        videoElement.muted = true; // 静音后通常可自动播放
        videoElement.play().catch(e => updateStatus('❌ 播放失败: ' + e.message));
      });
    } catch (err) {
      updateStatus('❌ 播放异常: ' + err.message);
    }

    // 页面卸载前销毁播放器
    window.addEventListener('beforeunload', () => flvPlayer.destroy(), false);

  } else {
    updateStatus('❌ 当前浏览器不支持 flv.js(需要 MSE)');
  }
</script>

</body>
</html>

安装node

npm init -y
npm install node-media-server
D:\phpstudy_pro\WWW\flv>node  server.js
[2025/11/8 16:17:06] [INFO] Node-Media-Server v4.1.0
[2025/11/8 16:17:06] [INFO] Homepage: https://github.com/illuspas/Node-Media-Server
[2025/11/8 16:17:06] [INFO] License: Apache-2.0
[2025/11/8 16:17:06] [INFO] Author: Chen Mingliang
✅ FLV 直播服务器已启动!
📍 RTMP 推流地址: rtmp://localhost:7003/live/movie
📍 HTTP-FLV 播放地址: http://localhost:7001/live/movie.flv
📍 管理接口: http://localhost:7001/api/streams
[2025/11/8 16:17:06] [INFO] WebSocket server listening on port undefined:7001
[2025/11/8 16:17:06] [INFO] HTTP server listening on port undefined:7001
[2025/11/8 16:17:06] [INFO] Rtmp Server listening on port undefined:7003
[直播启动] 未知流地址 (ID: [object Object])
[2025/11/8 16:17:17] [INFO] RTMP session mhq0harugcldhc03 ::1:53005 start push /live/movie
[2025/11/8 16:20:13] [INFO] RTMP session mhq0harugcldhc03 close
[直播结束] ID: [object Object]
[直播启动] 未知流地址 (ID: [object Object])
[2025/11/8 16:20:16] [INFO] RTMP session mhq0l4yq6hdfz8eh ::1:53116 start push /live/movie
ffmpeg -re -i test.mp4 -c copy -f flv http://localhost:7001/live/movie.flv
Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐