nodejs)
play.router.js
const express = require('express');
const router = express.router();
const playcontroller = require('../controller/play.controller');
router.get('/video/:filename', playcontroller.playvideo);
module.exports = router;
play.controller.js
const path = require('path');
const fs = require('fs');
async function playVideo(req, res) {
const filename = req.params.filename;
const videoPath = path.join(__dirname, '..', 'uploads', filename);
const stat = fs.statSync(videoPath);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = (end - start) + 1;
const file = fs.createReadStream(videoPath, { start, end });
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
res.writeHead(200, head);
fs.createReadStream(videoPath).pipe(res);
}
}
module.exports = {
playVideo,
};
reactjs)
VideoPlayer.js
import React from 'react';
import { API_BASE_URL } from '../../config';
const VideoPlayer = ( { fileName } ) => {
return (
<div>
<video width="750" height="500" controls>
<source src={`${API_BASE_URL}/play/video/${fileName}`} type="video/mp4" />
Your browser does not support the video tag.
</video>
</div>
);
};
export default VideoPlayer;
'웹' 카테고리의 다른 글
사전 참여 아이디어 - online to offline - 무형을 유형으로 증명 (0) | 2024.06.04 |
---|---|
nodejs, reactjs) react to static page - gatsby node api - 개발 기록 4 (0) | 2024.06.03 |
nodejs) puppeteer 사용해서 특정 사이트에서 file download - 개발 기록 2 (0) | 2024.05.29 |
데이터 만들기 (0) | 2024.05.26 |
nodejs) getRandomItem - 임의로 1개 선택해서 반환하기 - 개발 기록 1 (0) | 2024.05.26 |