TL/DR:
我使用以下代码从 m3u8 链接下载所有 mpeg-ts 块文件,然后以编程方式将它们中的每一个转换为 .mp4。
我最终得到了许多可以添加到 vlc 播放列表并播放的小 .mp4 文件,但我无法使用 javascript 以编程方式将所有这些 mp4 文件连接到一个 mp4 文件中。
我听说将所有这些 ts 文件合并为一个 mp4 文件的最后一部分可以使用 mux.js 完成,但我自己没有这样做。
加长版:
我最终做的是使用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();
}
);
然后我使用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++;
}
});
}
注意:如果你想使用给定的代码,需要改变什么:
- uri 明显
- 您希望保存 ts 文件的 (cwd) 位置(我的是destinationDirectory)
- preferLowQuality 如果您更喜欢它能找到的最高质量,请将其设置为 false
- 下载后读取 ts 文件的位置(我的是destinationDirectory/bandwidth-198000)
我希望这段代码将来能对某人有所帮助。
特别感谢 Tenacex 在这方面的帮助。