简体   繁体   English

hls.js 记录文件

[英]Hls.js record file

Hello and thanks for reading,您好,感谢您的阅读,

I have a Hls stream with an m3u8 playlist.我有一个带有 m3u8 播放列表的 Hls stream。 The Video is playing just fine on an Html page with a Video element and https://github.com/video-dev/hls.js视频在带有 Video 元素和https://github.com/video-dev/hls.js的 Html 页面上播放得很好

But if I download the segments to join them they are only white pixels.但是,如果我下载这些片段以加入它们,它们只是白色像素。 VLC and FFmpeg can't handle them. VLC 和 FFmpeg 无法处理它们。 VLC shows a white pixel for 10seconds and FFmpeg says that there's no stream in the file. VLC 显示一个白色像素 10 秒,FFmpeg 说文件中没有 stream。

So now I want to know what this hls.js is doing to make it running.所以现在我想知道这个 hls.js 正在做什么来让它运行。 To me a non-js developer it all looks a bit confusing.对我一个非 js 开发人员来说,这一切看起来有点混乱。 I was able to understand stuff like which function is called when a new segment is loaded.我能够理解加载新段时调用 function 之类的东西。 Unfortunately, I was unable to understand stuff about the data.不幸的是,我无法理解有关数据的内容。 The one character variables are confusing to me.一个字符变量让我感到困惑。

For now, I capture the stream of the video element and download it later but I don't like this solution at all.目前,我捕获了视频元素的 stream 并稍后下载,但我根本不喜欢这个解决方案。

How to help me如何帮助我

It would be very nice if anyone can tell me how to hook into the script and tell it to download directly to the disk so I'm independent of framerate drops.如果有人能告诉我如何挂钩脚本并告诉它直接下载到磁盘上,那就太好了,这样我就不会受到帧率下降的影响。

If anyone can tell how the script is able to convert the data so that the element can use it and I would be able to implement or do it with FFmpeg would be really helpful.如果有人能告诉脚本如何能够转换数据以便元素可以使用它,我将能够使用 FFmpeg 来实现或执行它会非常有帮助。

I also thought it might be possible to have a listener when the blob changes to store its contents.我还认为当 blob 更改以存储其内容时,可能有一个侦听器。

Thanks for everyone helping.感谢大家的帮助。 I'm trying to find a solution for too many hours now.我现在试图找到一个解决方案太多小时了。

I found the solution.我找到了解决方案。 After looking at their great event system https://github.com/video-dev/hls.js/ and this issue which I contributed too and not just copied https://github.com/video-dev/hls.js/issues/1322在查看了他们出色的活动系统https://github.com/video-dev/hls.js/以及我贡献的这个问题之后,而不仅仅是复制https://github.com/video-dev/hls.js/问题/1322

var arrayRecord = [];

function download(data, filename) {
    console.log('downloading...');
    var blob = new Blob([arrayConcat(data)], {
        type: 'application/octet-stream'
    });
    saveAs(blob, filename);
}

function arrayConcat(inputArray) {
    var totalLength = inputArray.reduce(function (prev, cur) {
        return prev + cur.length
    }, 0);
    var result = new Uint8Array(totalLength);
    var offset = 0;
    inputArray.forEach(function (element) {
        result.set(element, offset);
        offset += element.length;
    });
    return result;
}

function saveAs(blob, filename) {
    var url = URL.createObjectURL(blob);
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";
    a.href = url;
    a.download = filename;
    a.click();
    window.URL.revokeObjectURL(url);
}

function stopRecord() {
    arrayRecord.forEach(function (item) {
        download(item.data['video'], "video.mp4");
        download(item.data['audio'], "audio.mp4");
        item.hls.destroy();
        return false;
    });
}

function startRecord() {
    var video = document.getElementById('video');
    var dataStream = {
        'video': [],
        'audio': []
    };
    var hls = new Hls();
    hls.loadSource("Your playlist");
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED, function () {
        video.play();
        hls.on(Hls.Events.BUFFER_APPENDING, function (event, data) {
            console.log("apending");
            dataStream[data.type].push(data.data);
        });
    });
    arrayRecord.push({
        hls: hls,
        data: dataStream
    });
    video.onended = function (e) {
        stopRecord()
    }

}

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

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