使用 ffmpeg 时没有获取部分视频内容

编程入门 行业动态 更新时间:2024-10-05 13:28:06

使用 ffmpeg 时没有获取部分视频<a href=https://www.elefans.com/category/jswz/34/1771374.html style=内容"/>

使用 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 时没有获取部分视频内容

本文发布于:2024-05-30 15:36:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   视频   ffmpeg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!