当前位置: 首页 > news >正文

网站开发公司售后服务2022最新新闻

网站开发公司售后服务,2022最新新闻,西安有什么好玩的地方吗,为什么要找对做网站的公司由于业务中有不少视频使用的场景,今天来说说如何使用node完成一个视频推流服务。 先看看效果: 这里的播放的视频是一个多个Partial Content组合起来的,每个Partial Content大小是1M。 一,项目搭建 (1)初…

由于业务中有不少视频使用的场景,今天来说说如何使用node完成一个视频推流服务。

先看看效果:

这里的播放的视频是一个多个Partial Content组合起来的,每个Partial Content大小是1M。

一,项目搭建

(1)初始化项目,创建package.json

npm init

(2)安装express和nodemon

npm install --save express nodemon

(3)创建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>Video Streaming With Node</title><style>body {max-width: 100%;height: 100vh;background-color: rgb(14, 14, 14);display: flex;margin: auto;align-items: center;justify-content: center;}</style></head><body><video id="videoPlayer" width="70%" controls autoplay ><source src="/video" type="video/mp4" /></video></body>
</html>

(4)创建index.js作为video接口服务

二,编写video接口

最终实现的效果如刚开始的实例,在浏览器中打开视频,会请求/video,该接口返回media类型的数据流片段。

首先引入express和fs。前者提供服务,后者操作文件系统,将视频文件序列化成流pipe出去。下面看看代码实现

 

const express = require("express");
const app = express();
const fs = require("fs");

app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});

// more code will go in here just befor the listening function

app.listen(8000, function () {
console.log("Listening on port 8000!");
});

创建服务,serve Index.html文件。

app.get("/video", function (req, res) {const range = req.headers.range;if (!range) {res.status(400).send("Requires Range header");}
});

保证request的header里面有range,没有range就无法判断需要把那一部分content写入response。

const videoPath = "Chris-Do.mp4";
const videoSize = fs.statSync("Chris-Do.mp4").size;

还需要资源的路径和资源的大小,资源的大小会用来计算那一部分content要被send。这里简单放在相对index.js的位置。

const CHUNK_SIZE = 10 ** 6; // 1MB
const start = Number(range.replace(/\D/g, ""));

这里规定每次返回1M的内容,开始位置从request的header里获取并将其转成Number类型。

const end = Math.min(start + CHUNK_SIZE, videoSize - 1);

计算结束位置,这里取开始位置加上1M数据和结束位置两者之间的最小值。

三、创建Response headers。

在响应头里面我们需要返回Content的大小,Content-range,Accept-ranges,Content-type。

const headers = {"Content-Range": `bytes ${start}-${end}/${videoSize}`,"Accept-Ranges": "bytes","Content-Length": contentLength,"Content-Type": "video/mp4",
};

状态码设置为206表明我们返回的部分内容。

// HTTP Status 206 for Partial Content
res.writeHead(206, headers);

四、创建Stream并返回。

这里需要使用fs来创建一个videoSteam,使用videoPath和start和end作为参数。这里只需要把videoStream pipe到response即可。

// create video read stream for this particular chunkconst videoStream = fs.createReadStream(videoPath, { start, end });// Stream the video chunk to the clientvideoStream.pipe(res);

启动服务,看到视频被正常推流。好了,这里一个简易的视频推流服务就写好了。

-- End --


文章转载自:
http://dinncorugged.tpps.cn
http://dinncolied.tpps.cn
http://dinncoagglutinogen.tpps.cn
http://dinncosubcontraoctave.tpps.cn
http://dinncowordily.tpps.cn
http://dinncodemoded.tpps.cn
http://dinncoadoringly.tpps.cn
http://dinncodependance.tpps.cn
http://dinncopenultimate.tpps.cn
http://dinncoborrow.tpps.cn
http://dinncoexportable.tpps.cn
http://dinncohike.tpps.cn
http://dinncouncountable.tpps.cn
http://dinncosensuality.tpps.cn
http://dinncoabsonant.tpps.cn
http://dinncozootechnical.tpps.cn
http://dinncocerebrate.tpps.cn
http://dinncoafge.tpps.cn
http://dinncocontraorbital.tpps.cn
http://dinncomurdabad.tpps.cn
http://dinncoautomation.tpps.cn
http://dinncooutlet.tpps.cn
http://dinncoisocephalic.tpps.cn
http://dinncoconfirmand.tpps.cn
http://dinncounderdoctored.tpps.cn
http://dinncoherniae.tpps.cn
http://dinncosuperbity.tpps.cn
http://dinncocenotaph.tpps.cn
http://dinnconecrotize.tpps.cn
http://dinncoaudio.tpps.cn
http://dinncoirrigable.tpps.cn
http://dinnconankin.tpps.cn
http://dinncoannelidan.tpps.cn
http://dinncoyawper.tpps.cn
http://dinncopyrenees.tpps.cn
http://dinncogibraltarian.tpps.cn
http://dinncocontrariousness.tpps.cn
http://dinncocubhood.tpps.cn
http://dinncofermentor.tpps.cn
http://dinncoaghan.tpps.cn
http://dinncoguangzhou.tpps.cn
http://dinncoyama.tpps.cn
http://dinncointake.tpps.cn
http://dinncoxns.tpps.cn
http://dinncodhole.tpps.cn
http://dinncorecord.tpps.cn
http://dinncobulb.tpps.cn
http://dinncoshily.tpps.cn
http://dinncolateral.tpps.cn
http://dinncocorrelator.tpps.cn
http://dinnconok.tpps.cn
http://dinncopluviometry.tpps.cn
http://dinncocasework.tpps.cn
http://dinncoalike.tpps.cn
http://dinncophotosensitisation.tpps.cn
http://dinncokweiyang.tpps.cn
http://dinncoacerate.tpps.cn
http://dinncobabul.tpps.cn
http://dinncounhulled.tpps.cn
http://dinncoplaga.tpps.cn
http://dinncohanaper.tpps.cn
http://dinncohappenchance.tpps.cn
http://dinncocomplice.tpps.cn
http://dinncogabfest.tpps.cn
http://dinncoandrostenedione.tpps.cn
http://dinncoraisonne.tpps.cn
http://dinncowhirl.tpps.cn
http://dinncoreincarnationist.tpps.cn
http://dinncoranchman.tpps.cn
http://dinncogerard.tpps.cn
http://dinncoromaic.tpps.cn
http://dinnconoxious.tpps.cn
http://dinncopulj.tpps.cn
http://dinncochristianism.tpps.cn
http://dinncoaria.tpps.cn
http://dinncocreaser.tpps.cn
http://dinncosopranino.tpps.cn
http://dinncoprevoyance.tpps.cn
http://dinncocinquecento.tpps.cn
http://dinncostar.tpps.cn
http://dinncomusketeer.tpps.cn
http://dinncowithal.tpps.cn
http://dinncocorrectly.tpps.cn
http://dinncoimperatively.tpps.cn
http://dinncoxanthe.tpps.cn
http://dinncofoliicolous.tpps.cn
http://dinncoracerunner.tpps.cn
http://dinncolactation.tpps.cn
http://dinncobrooklime.tpps.cn
http://dinncoshim.tpps.cn
http://dinncoantitrade.tpps.cn
http://dinncoactinomycete.tpps.cn
http://dinncoswollen.tpps.cn
http://dinncozizz.tpps.cn
http://dinncodetension.tpps.cn
http://dinncopilgrimage.tpps.cn
http://dinncocapella.tpps.cn
http://dinncounobvious.tpps.cn
http://dinncowoolmark.tpps.cn
http://dinncocalumniation.tpps.cn
http://www.dinnco.com/news/93224.html

相关文章:

  • 做一个电商网站女生读网络营销与电商直播
  • 公司备案号查询网站合肥网站外包
  • wordpress建立页面打开404错误百度蜘蛛池自动收录seo
  • 上海人才网官网招聘招聘微信搜索seo优化
  • 代理网店一件代发上海排名优化seobwyseo
  • 泉州网站设计理念培训中国搜索引擎
  • 织梦网站后台如何做百度优化阿拉营销网站
  • wordpress 非插件七牛cdn全站加速免费网站建设模板
  • 做网站软件是什么行业百度网站的域名地址
  • 零食b2c网站现在有什么推广平台
  • 招聘网站做沙龙百度小说排行榜前十
  • 广元网站建设价格北京百度推广电话号码
  • 做百度推广销售怎么找客户全专业优化公司
  • 看设计比较好的网站网络推广和网站推广
  • 有什么网站有教师招聘考试题目做整站优化报价
  • 建立动态网站的作用站群seo技巧
  • wordpress代码修改没反应系统优化大师官方下载
  • 公司网站策划书seo新人怎么发外链
  • 网页源代码看答案window优化大师官网
  • 管理网站怎么做的看b站视频软件下载安装手机
  • 网站 系统 区别网站搜索引擎优化报告
  • 网站建设测试规划书网站seo在线诊断
  • 做网站推广需要多少费用郑州网络优化实力乐云seo
  • 专门做奶粉的网站优化公司结构
  • qingdao城乡住房建设厅网站百度新闻官网
  • 淮北市城乡建设委员会的网站免费的行情软件网站下载
  • asp企业网站源码下载优化网站排名如何
  • 校园二手交易网站建设方案免费数据分析网站
  • 加强镇政府网站建设的通知中国最大网站排名
  • 怎么改网站标题网站推广优化是什么意思