>我最近開始了一個項目,將實時數碼單反相關的錄像流傳輸到網站或應用程序。 我的目標很簡單:從我的數碼單反相機的實時圖像流。但是,實現無縫的流媒體被證明比預期的更具挑戰性。
>這就是我解決的方式,利用digicamcontrol和express.js。
我的目標是將此圖像流轉換為標準的MJPEG feed,與網絡和桌面應用程序兼容。
解決方案
通過實驗,我發現了一種使用Express.js將這些靜態圖像轉換為光滑的MJPEG流的方法。 這個過程令人驚訝地簡單。
解決常見錯誤
const express = require('express'); const axios = require('axios'); const app = express(); const PORT = 3000; // MJPEG proxy port const DIGICAM_URL = 'http://127.0.0.1:5513/liveview.jpg'; const FPS = 20; // Frames per second app.get('/liveview.mjpeg', (req, res) => { res.writeHead(200, { 'Content-Type': 'multipart/x-mixed-replace; boundary=frame', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', }); const interval = setInterval(async () => { try { const response = await axios.get(DIGICAM_URL, { responseType: 'arraybuffer' }); const imageData = response.data; res.write(`--frame\r\n`); res.write(`Content-Type: image/jpeg\r\n\r\n`); res.write(imageData); res.write(`\r\n`); } catch (error) { console.error('Error fetching image:', error.message); clearInterval(interval); res.end(); } }, 1000 / FPS); req.on('close', () => { clearInterval(interval); }); }); app.listen(PORT, () => { console.log(`MJPEG server running on http://localhost:${PORT}/liveview.mjpeg`); });
>。
結果
在運行服務器的情況下,我成功地將DSLR的LiveView Feed流到了我的Web應用程序中,以光滑的20 fps。儘管這種MJPEG方法對於大多數應用程序都是有效的,但不像RTSP那樣複雜。
Error fetching liveview image: connect ECONNREFUSED ::1:5513
如果您的目標是將實時數碼單反相機集成到項目中,則此方法提供了一種直接有效的解決方案。 DigicamControl處理攝像頭處理,並表達簡化圖像流。 結果令人滿意! localhost
http://localhost:5513/liveview.jpg
http://127.0.0.1:5513/liveview.jpg
以上是將靜態圖像變成無縫的MJPEG序列的詳細內容。更多資訊請關注PHP中文網其他相關文章!