有没有一种方法可以在Safari中从HTML视频标签捕获音频和视频?

huangapple go评论57阅读模式
英文:

Is there a way to capture audio and video from an HTML video tag in Safari?

问题

在Chrome和Firefox中,我们可以使用HTMLMediaElement.captureMediaStream()来捕获流。但是HTMLMediaElement.captureMediaStream()在Safari中不受支持。

视频标签的源是以段接收的HLS数据。

在Safari中是否有其他方式来捕获媒体数据作为MediaStream?

英文:

In chrome and firefox, we can user HTMLMediaElement.captureMediaStream() to capture a stream. But HTMLMediaElement.captureMediaStream() is not supported by Safari.

Source of the video tag is hls data received in segments.

Is there any other way capture media data as a MediaStream in Safari?

答案1

得分: 1

直接采用captureMediaStream方法在Safari中不受支持,至少在目前这个时候是这样的,正如你所见,但你可以使用基于浏览器的ffmpeg方法来实现。

在过去,在浏览器中运行ffmpeg对大多数任务来说都不现实,因为它太大且速度太慢,但是最近基于WASM(Web Assembly)的ffmpeg实现已经使其更容易访问。

你可以构建自己的基于ffmpeg的解决方案,利用像这样的库:

你需要注意需要支持SharedArrayBuffer:

只有支持SharedArrayBuffer的浏览器才能使用ffmpeg.wasm,你可以在这里查看完整列表。

上面提到的链接在这里:https://caniuse.com/sharedarraybuffer

我发现它在许多常见的ffmpeg任务中表现良好,但对于较大的视频文件很敏感 - 我还没有尝试过将HLS转换为文件,所以你需要进行实验以确定是否符合你的需求。这里有一个演示,你可以在上面测试你的用例:https://ffmpegwasm.netlify.app/#demo

你想要使用的ffmpeg命令取决于音频,但很可能是:

ffmpeg -i in.m3u8 -acodec copy -bsf:a aac_adtstoasc -vcodec copy out.mp4

(查看此问题和答案以获取更多信息:https://stackoverflow.com/q/33108105/334402)

还有一些开源的现成解决方案,你可以查看 - 例如:

英文:

The direct captureMediaStream approach is not supported in Safari, at least at this time, as you saw, but one way you could do this is to use a browser ffmpeg based approach.

In the past running ffmpeg in the browser was not realistic for most tasks as it was simply too big and too slow, but recent WASM (web assembly) based ffmpeg implementations have made it more accessible.

You can build your own ffmpeg based solution leveraging a library like:

You do need to be aware of the need for SharedArrayBuffer support:

> Only browsers with SharedArrayBuffer support can use ffmpeg.wasm, you can check HERE for the complete list.

The link referred to above is here: https://caniuse.com/sharedarraybuffer

I have found that it works well for many common ffmpeg tasks but it is sensitive to larger video files - I have not tried HLS to file conversion so you would need to experiment to see it meets your needs. There is a demo here which you may be able to test your use case on: https://ffmpegwasm.netlify.app/#demo

The ffmpeg command you want to use depends on the audio but it likely:

> ffmpeg -i in.m3u8 -acodec copy -bsf:a aac_adtstoasc -vcodec copy out.mp4

(see this question and answers for more info: https://stackoverflow.com/q/33108105/334402)

There are also some open source ready built solutions which you would look at - e.g:

huangapple
  • 本文由 发表于 2023年6月1日 23:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定