是否可以使用javascript或php将所有m3u8
段下载到一个文件中
我搜索这个,但找不到任何东西;
TL/DR:我使用以下代码从 m3u8 链接下载所有 mpeg-ts 块文件,然后以编程方式将它们中的每一个转换为.mp4。
我最终得到了许多小.mp4文件,我可以将其添加到 vlc 播放列表中并播放,但我无法使用 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();
}
);
然后我使用这些 .ts 文件转换为.mp4文件mpegts_to_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++;
}
});
}
注意:如果您希望使用它,请在给定代码中更改什么:
- 显然是乌里
- 您希望保存 TS 文件的 (CWD( 位置(我的是目的地目录(
- 首选低质量 如果您更喜欢它会找到的最高质量,请将其设置为 false
- 下载 TS 文件后读取文件的位置(我的是 destinationDirectory/bandwidth-198000(
我希望这段代码将来对某人有所帮助。特别感谢Tenacex在这方面帮助我。