内容"/>
使用 ffmpeg 时没有获取部分视频内容
我正在尝试使用 nodejs 和
fluent-ffmpeg
部分发送视频。但它无法发送数据。
当我只使用
fs
模块发送视频时,它工作正常。这是代码。
const express = require("express");
const app = express();
const fs = require("fs");
const VIDEO_PATH = 'video.mp4';
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
})
app.get("/video", (req, res) => {
const range = req.headers.range;
if(!range) {
res.status(400).send("Requires range header!");
}
const size = fs.statSync(VIDEO_PATH).size;
const CHUNK_SIZE = 10**6;
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, size - 1);
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${size}`,
"Accept-Ranges": 'bytes',
"Content-Length": contentLength,
"Content-Type": "video/mp4"
}
res.writeHead(206, headers);
const videoStream = fs.createReadStream(VIDEO_PATH, {start, end});
videoStream.pipe(res);
})
app.listen(3000, () => {
console.log("Server is running on port: ", 3000);
})
当我使用
fluent-ffmpeg
模块处理后发送视频时,它不起作用。为了便于理解,我简化了代码。这是我的代码。
const express = require("express");
const app = express();
const fs = require("fs");
const ffmpegStatic = require('ffmpeg-static');
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegStatic);
const VIDEO_PATH = 'video.mp4';
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
})
app.get("/video", (req, res) => {
const range = req.headers.range;
if(!range) {
res.status(400).send("Requires range header!");
}
const size = fs.statSync(VIDEO_PATH).size;
const CHUNK_SIZE = 10**6;
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, size - 1);
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${size}`,
"Accept-Ranges": 'bytes',
"Content-Length": contentLength,
"Content-Type": "video/mp4"
}
res.writeHead(206, headers);
const videoStream = fs.createReadStream(VIDEO_PATH, {start, end});
ffmpeg(videoStream)
.outputOptions('-movflags frag_keyframe+empty_moov')
.toFormat('mp4')
.pipe(res);
})
app.listen(3000, () => {
console.log("Server is running on port: ", 3000);
})
我的
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<video id="video-player" width="50%" controls>
<source src="/video" type="video/mp4">
</video>
</body>
</html>
任何帮助将不胜感激。提前致谢。
回答如下:更多推荐
使用 ffmpeg 时没有获取部分视频内容
发布评论