npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-m3u8-to-mp4

v3.0.0

Published

convert m3u8 to mp4

Downloads

35

Readme

node-m3u8-to-mp4

可以将远程或本地的 m3u8 文件转换成 MP4 或 MP3 等媒体文件

最新版本

v3.0.0

安装

npm install node-m3u8-to-mp4 -S

用法说明

v3.x 将 node-m3u8-to-mp4 的功能解耦成 4 大部分

  • m3u8 解析器 -> mParser
  • 下载器 -> mDownloader
  • 媒体片段合成器 -> mConverter
  • 进度指示器 -> mIndicator

如下代码:

const { mParser, mDownloader, mConverter, mIndicator } = require("node-m3u8-to-mp4");

开发者可以根据项目具体情况灵活组合各个部分

下载 m3u8 媒体文件的一般过程为:解析视频资源列表(mParser)->下载资源片段(mDownloader)->片段合成文件(mConverter)

大致用法如下:

mParser("m3u8的本地或远程路径").then(list=>{
  const medias = list.map((item) => `${item.url}`);
  mDownloader(medias).then(()=>{
    mConverter("临时缓存路径", "媒体路径");
  });
})

详细用法如下(也可以参考 example.js文件):

const {
  mParser,
  mDownloader,
  mConverter,
  mIndicator,
} = require("node-m3u8-to-mp4");

const path = require("path");
const fse = require("fs-extra");

// 设定进度指示器(可不设置)
mIndicator("downloading", (index, total) => {
  console.log("下载进度:" + ((index / total) * 100).toFixed(2) + "%");
});

mIndicator("converting", (index, total) => {
  console.log("转换进度:" + ((index / total) * 100).toFixed(2) + "%");
});

// 过程:解析视频资源列表(mParser)->下载资源片段(mDownloader)->片段合成文件(mConverter)

// 解析资源列表,第二个参数为远程请求文件时的请求头,可留空
mParser(path.join(__dirname, "./index.m3u8"), {
  referer: "https://www.great-elec.com/",
}).then((list) => {
  // [{url:"",isFull:boolean},...] 资源列表,若url不是完整的互联网路径,可根据isFull字段做二次处理
  const medias = list.map((item) => `${item.url}`);

  console.log("解析完成,开始下载");

  // 下载媒体列表,配置项可留空,targetPath为存储片段的临时文件夹路径默认为'.tmp',headers为远程请求头
  mDownloader(medias, {
    targetPath: path.resolve(".target"),
    headers: {
      referer: "https://www.great-elec.com/",
    },
  })
    .then(() => {
      console.log("下载完成,正在转换");

      // 下载完成后,根据临时文件夹路径,合成视频到指定位置的指定格式,最后一个参数表示是否要在程序运行完后清除临时目录,默认为true
      mConverter(path.resolve(".target"), "./video.mp4").then(() => {
        console.log("已生成文件");
      });
    })
    .catch((e) => {
      console.log("下载出错!");
      // 下载出错,可将指定下载的临时目录删除
      fse.removeSync(path.resolve(".target"));
    });
});

作者联系方式

微信: zaowangxiaoye

技术交流,结交好友

ps:接各种前后端私活,欢迎联系