[英]NextJS not working with jsmpeg/node-rtsp-stream. Trying to display RTSP stream
Following: https://github.com/kyriesent/node-rtsp-stream and How to display IP camera feed from an RTSP url onto reactjs app page? 我試圖從閉路電視顯示 RTSP stream 但它給了我一個錯誤。 ReferenceError: document is not defined
在scripts\jsmpeg.min.js (1:701) @ eval
中定義
我還沒有在 NextJS 中找到這個模塊的單個實現,所以我可能做錯了什么,但我不知道是什么。 而且我沒有為 NextJS 找到更好的解決方案。
沒有什么可以讓我進入: https://github.com/phoboslab/jsmpeg但我可能在這里用錯了。
兔子洞由此開始: How can I display an RTSP video stream in a web page? 但事情要么已經過時,要么不適用,要么我無法弄清楚。
如何解決我得到的錯誤? NextJS 中是否有替代方法? 我不在乎我需要的只是 stream 來自閉路電視的 RTSP 提要。
components
-layout
-Stream.js
pages
-api
-stream
-[streamId].js
-app.js
-index.js
scripts
-jsmpeg.min.js
Stream.js
是 stream/app.js 中的一個組件, stream/app.js
stream/app.js
用於stream/[streamId].js
Stream.js
import JSMpeg from "../../scripts/jsmpeg.min.js";
const Stream = (props) => {
const player = new JSMpeg.Player("ws://localhost:9999", {
canvas: document.getElementById("video-canvas"), // Canvas should be a canvas DOM element
});
return (
<Fragment>
<canvas
id="video-canvas"
className={classes.canvas}
onMouseDown={onMouseDownHandler}
></canvas>
</Fragment>
);
};
[streamId.js]
export async function getStaticProps(context) {
const StreamCCTV = require("node-rtsp-stream");
const streamCCTV = new StreamCCTV({
ffmpegPath: "C:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe", //! remove on Ubuntu
name: "name",
streamUrl: "rtsp://someuser:somepassword@1.1.1.1",
wsPort: 9999,
ffmpegOptions: {
// options ffmpeg flags
"-stats": "", // an option with no neccessary value uses a blank string
"-r": 30, // options with required values specify the value after the key
},
});
我也嘗試過使用https://www.npmjs.com/package/jsmpeg我將Stream.js
更改為:
import jsmpeg from 'jsmpeg';
const Stream = (props) => {
const client = new WebSocket("ws://localhost:9999")
const player = new jsmpeg(client, {
canvas: document.getElementById("video-canvas"), // Canvas should be a canvas DOM element
});
return (
<Fragment>
<canvas
id="video-canvas"
className={classes.canvas}
onMouseDown={onMouseDownHandler}
></canvas>
</Fragment>
);
};
現在的錯誤是: ReferenceError: window is not defined
jsmpeg 只能播放 mpeg1。 請使用 mpegts 格式的 ffmpeg stream
我設法使用兩種解決方案來完成這項工作:
從此處下載jsmpeg.min.js
並將其添加到與您的組件相同的目錄(或任何其他所需目錄)。
將文件的第一部分從var JSMpeg...
更改為export const JSMpeg...
添加具有以下內容的組件:
import { useRef } from 'react'
const StreamPlayer = () => {
const streamRef = useRef(null)
useEffect(() => {
const { JSMpeg } = require('./jsmpeg.min.js')
const player = new JSMpeg.Player('ws://localhost:9999', {
canvas: streamRef.current
})
}, [])
return <canvas ref={streamRef} id="stream-canvas"></canvas>
}
我不確定在useEffect
中使用require
是否可以,但由於JSMpeg
使用的是document
和window
對象,因此它們必須在加載后導入。\
從這里下載jsmpeg.min.js
並將其添加到 Nextjs 項目的/public
目錄中。
添加具有以下內容的組件:
player = new JSMpeg.Player('ws://localhost:9999', {
canvas: document.getElementById('stream-canvas')
})
jsmpeg.min.js
的末尾我添加了: player = new JSMpeg.Player('ws://localhost:9999', { canvas: document.getElementById('stream-canvas') })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.