Created
October 8, 2025 02:13
-
-
Save htdangkhoa/ff15b5b72e1b83a4462c4cb1b38a6902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { spawn } from 'child_process'; | |
| import puppeteer from 'puppeteer'; | |
| async function startScreencastToRtsp() { | |
| const browser = await puppeteer.launch({ | |
| headless: false, | |
| args: ['--remote-debugging-port=9222', '--no-sandbox'], | |
| }); | |
| const page = await browser.newPage(); | |
| await page.evaluateOnNewDocument((interval: number) => { | |
| setInterval(() => { | |
| console.log('repaint'); | |
| const el = document.createElement('div'); | |
| el.style.cssText = 'position:absolute;width:1px;height:1px;opacity:0;'; | |
| document.body.appendChild(el); | |
| el.remove(); | |
| }, interval); | |
| }, 100); | |
| await page.goto('https://example.com'); | |
| const client = await page.target().createCDPSession(); | |
| const ffmpeg = spawn('ffmpeg', [ | |
| '-f', | |
| 'image2pipe', | |
| '-vcodec', | |
| 'mjpeg', | |
| '-r', | |
| '15', // output fps | |
| '-i', | |
| '-', | |
| '-f', | |
| 'rtsp', | |
| '-rtsp_transport', | |
| 'tcp', | |
| '-vcodec', | |
| 'libx264', | |
| '-pix_fmt', | |
| 'yuv420p', | |
| '-preset', | |
| 'veryfast', | |
| '-tune', | |
| 'zerolatency', | |
| 'rtsp://localhost:8554/live/test', | |
| ]); | |
| ffmpeg.stderr.on('data', (d) => console.log('[ffmpeg]', d.toString())); | |
| let lastFrame = null; | |
| let ffmpegOpen = true; | |
| // Bắt đầu screencast | |
| await client.send('Page.startScreencast', { | |
| format: 'jpeg', | |
| quality: 80, | |
| everyNthFrame: 1, | |
| }); | |
| // Nhận frame từ Chrome | |
| client.on('Page.screencastFrame', async (frame) => { | |
| const { data, sessionId } = frame; | |
| await client.send('Page.screencastFrameAck', { sessionId }); | |
| lastFrame = Buffer.from(data, 'base64'); // lưu frame cuối | |
| }); | |
| // Vòng lặp gửi frame đều 15 fps | |
| const interval = setInterval(() => { | |
| if (!lastFrame || !ffmpegOpen) return; | |
| try { | |
| ffmpeg.stdin.write(lastFrame); | |
| } catch (err) { | |
| ffmpegOpen = false; | |
| console.error('FFmpeg closed:', err.message); | |
| } | |
| }, 1000 / 15); | |
| ffmpeg.on('exit', () => { | |
| clearInterval(interval); | |
| console.log('FFmpeg exited'); | |
| }); | |
| console.log('Streaming continuously...'); | |
| } | |
| startScreencastToRtsp(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| docker run --rm -it \ | |
| -e MTX_RTSPTRANSPORTS=tcp \ | |
| -e MTX_WEBRTCADDITIONALHOSTS=0.0.0.0 \ | |
| -p 8554:8554 \ | |
| -p 1935:1935 \ | |
| -p 8888:8888 \ | |
| -p 8889:8889 \ | |
| -p 8890:8890/udp \ | |
| -p 8189:8189/udp \ | |
| bluenviron/mediamtx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment