javascript 我可以使用网络将 m3u8 文件的所有片段下载为一个 mp4 文件吗(没有 ffmpeg)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27603177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I download all segments of a m3u8 file as one mp4 file using web ( no ffmpeg )
提问by elti musa
Is it possible to download all m3u8
segments in one file using javascript or php
是否可以m3u8
使用 javascript 或 php将所有段下载到一个文件中
I search for this but couldn't find anything;
我搜索了这个,但找不到任何东西;
回答by SudoPlz
TL/DR:I used the following code to download all the mpeg-ts chunk files from the m3u8 link, and then I convert each one of them into an .mp4 programmatically.
TL/DR:我使用以下代码从 m3u8 链接下载所有 mpeg-ts 块文件,然后以编程方式将每个文件转换为 .mp4。
I ended up with many small .mp4 files which I could add into a vlc playlist and play, but I was not able to programmatically concatenate all those mp4 files into one mp4 file using javascript.
我最终得到了许多小的 .mp4 文件,我可以将它们添加到 vlc 播放列表中并播放,但我无法使用 javascript 以编程方式将所有这些 mp4 文件连接成一个 mp4 文件。
I've heard the last part of merging all those ts files into one mp4 file can be done with mux.js, but I haven't done it myself.
我听说将所有这些 ts 文件合并为一个 mp4 文件的最后一部分可以使用mux.js完成,但我自己没有这样做。
Long version:
长版:
What I ended up doing was I used m3u8_to_mpegtsto download each and every MPEG_TS file that the m3u8 file pointed to into a directory.
我最终做的是使用m3u8_to_mpegts将 m3u8 文件指向的每个 MPEG_TS 文件下载到一个目录中。
var TsFetcher = require('m3u8_to_mpegts');
TsFetcher({
uri: "http://api.new.livestream.com/accounts/15210385/events/4353996/videos/113444715.m3u8",
cwd: "destinationDirectory",
preferLowQuality: true,
},
function(){
console.log("Download of chunk files complete");
convertTSFilesToMp4();
}
);
Then I converted those .ts files into .mp4 files using mpegts_to_mp4
然后我使用mpegts_to_mp4将这些 .ts 文件转换为 .mp4 文件
var concat = require('concatenate-files');
// Read all files and run
function getFiles(currentDirPath, callback) {
var fs = require('fs'),
path = require('path');
fs.readdir(currentDirPath, function (err, files) {
if (err) {
throw new Error(err);
}
var fileIt = files.length;
files.forEach(function (name) {
fileIt--;
// console.log(fileIt+" files remaining");
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, (fileIt==0));
}
});
});
}
var mpegts_to_mp4 = require('mpegts_to_mp4');
var toConvertIt=0, doneConvertingIt = 0;
function convertTSFilesToMp4(){
getFiles("destinationDirectory/bandwidth-198000",
function onFileDiscovered(filePath, noMoreFiles){ //onFileDiscovered runs for each file we discover in the destination directory
var filenameParts = filePath.split("/"); // if on Windows execute .split("\");, thanks Chayemor!
var filename = filenameParts[2];
if(filename.split(".")[1]=="ts"){ //if its a ts file
console.log(filename);
mpegts_to_mp4(filePath, toConvertIt+'dest.mp4', function (err) {
// ... handle success/error ...
if(err){
console.log("Error: "+err);
}
doneConvertingIt++
console.log("Finished converting file "+toConvertIt);
if(doneConvertingIt==toConvertIt){
console.log("Done converting vids.");
}
});
toConvertIt++;
}
});
}
Caution: What to change in the given code if you wish to use it:
注意:如果您想使用给定的代码,需要更改什么:
- The uri obviously
- the (cwd) location that you wish to save the ts files (mine was destinationDirectory)
- preferLowQuality set it to false if you prefer the highest quality it will find
- the location that you read your ts files from after you've downloaded them (mine was destinationDirectory/bandwidth-198000)
- uri 很明显
- 您希望保存 ts 文件的 (cwd) 位置(我的是 destinationDirectory)
- 如果您更喜欢它会找到的最高质量,则将其设置为 false
- 下载 ts 文件后从中读取文件的位置(我的是 destinationDirectory/bandwidth-198000)
I hope this code helps someone in the future. Special thanks to Tenacex for helping me out on this.
我希望这段代码可以帮助将来的某个人。特别感谢 Tenacex 在这方面帮助我。