A tool for download hls/m3u8 to mp4
- This package depends on @ffmpeg/ffmpeg, you need install it first
# install ffmpeg
npm install @ffmpeg/ffmpeg
online demo
online demo
install
# npm
npm install hls2mp4
# yarn
yarn add hls2mp4
usage
import Hls2Mp4 from "hls2mp4";
const hls2mp4 = new Hls2Mp4({
/**
* @type {number}
* max retry times while request data failed
*/
maxRetry = 3,
/**
* @type {number}
* the concurrency for download ts
*/
tsDownloadConcurrency = 10,
/**
* the base url of ffmpeg
*/
ffmpegBaseUrl = 'https://unpkg.com/@ffmpeg/core@0.12.2/dist/umd',
/**
* progress callback
*/
onProgress?: (type, progress) => {
// type = 0 => load FFmpeg
// type = 1 => parse m3u8
// type = 2 => downloading ts
// type = 3 => merge ts
};
/**
* error callback
*/
onError?: (error) => {};
});
const buffer = await hls2mp4.download('your m3u8 url')
if (buffer) {
hls2mp4.saveToFile(buffer, 'test.mp4')
}
else {
// download failed
}
as script
<script src="https://github.com/icefee/hls2mp4/raw/1.x/static/js/ffmpeg.min.js"></script>
<script src="https://github.com/icefee/hls2mp4/raw/1.x/dist/hls2mp4.js"></script>
<script>
const hls2mp4 = new Hls2Mp4({...})
</script>