简体   繁体   English

我可以使用web下载m3u8文件的所有片段作为一个mp4文件(没有ffmpeg)

[英]Can I download all segments of a m3u8 file as one mp4 file using web ( no ffmpeg )

Is it possible to download all m3u8 segments in one file using javascript or php 是否可以使用javascript或php在一个文件中下载所有m3u8

I search for this but couldn't find anything; 我搜索这个但找不到任何东西;

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_mpegts to 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 uri显然
  • the (cwd) location that you wish to save the ts files (mine was destinationDirectory) 你希望保存ts文件的(cwd)位置(我的是destinationDirectory)
  • preferLowQuality set it to false if you prefer the highest quality it will find preferLowQuality如果您希望找到最高质量,则将其设置为false
  • the location that you read your ts files from after you've downloaded them (mine was destinationDirectory/bandwidth-198000) 您下载ts文件后读取ts文件的位置(我的目标是目录目录/带宽198000)

I hope this code helps someone in the future. 我希望这段代码可以帮助将来的某个人。 Special thanks to Tenacex for helping me out on this. 特别感谢Tenacex帮助我解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM