我可以使用网络将m3u8文件的所有段下载为一个mp4文件吗( 没有ffmpeg )

2022-08-30 23:38:07

是否可以使用javascript或php下载一个文件中的所有片段m3u8

我搜索了这个,但找不到任何东西;


答案 1

断续器:我使用以下代码从 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();
   }
);

然后,我使用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)位置(我的是目标目录)
  • preferLowQuality将其设置为false,如果您更喜欢它会发现的最高质量
  • 下载ts文件后,您从中读取它们的位置(我的位置是目标目录/带宽-198000)

我希望这段代码将来能帮助别人。特别感谢Tenacex帮助我解决这个问题。


答案 2