简体   繁体   English

我将如何将 mp3 文件打包到 mp4 容器中?

[英]How would I go about packaging an mp3 file into an mp4 container?

So here's the issue that I'm currently having.所以这就是我目前遇到的问题。 I need an audio player for iOS that will play mp3.我需要一个可以播放 mp3 的 iOS 音频播放器。 Now at first glance this may seem like a trivial issue, just create an audio tag and give it the URL to the mp3 file.现在乍一看这似乎是一个微不足道的问题,只需创建一个audio标签并为其提供 mp3 文件的 URL。 While this technically works, it's basically unusable since iOS needs to download the entire file before starting to play it.虽然这在技术上可行,但它基本上无法使用,因为 iOS 需要在开始播放之前下载整个文件。 This results in a really long wait time when trying to play large mp3 files (could get close to a minute).这会导致在尝试播放大型 mp3 文件时等待时间非常长(可能接近一分钟)。 So the first thing I tried was to manually mimic the chunking that chrome does for playing mp3 files.所以我尝试的第一件事是手动模拟 chrome 播放 mp3 文件时的分块。 I first sent a HEAD request to get the byte length of the audio file.我首先发送了一个HEAD请求来获取音频文件的字节长度。 Used that length / duration in seconds to get the average bytes per second and use that data to request chunks based on where the user seeks to.使用该长度/持续时间(以秒为单位)来获取每秒的平均字节数,并根据用户寻求的位置使用该数据来请求块。 That didn't work since sometimes mp3 files contain metadata that throw off the calculation (like a cover image).这不起作用,因为有时 mp3 文件包含会影响计算的元数据(如封面图片)。 Additionally, sometimes mp3 files use VBR (Variable Bit Rate) and then I'm well and truly screwed.此外,有时 mp3 文件使用 VBR(可变比特率),然后我就真的搞砸了。

So this lead me to thinking, Safari couldn't possibly require the end user to download an entire mp4 file before playing it.所以这让我想到,Safari 不可能要求最终用户在播放之前下载整个 mp4 文件。 So I took an mp3 file, converted it to mp4 on an online converter and tested my theory out.所以我拿了一个 mp3 文件,在一个在线转换器上将它转换成 mp4 并测试了我的理论。 Voila, it worked.瞧,它奏效了。 Safari was streaming the mp4 file in chunks and the wait time went to close to 0. Alright, so now all I need to do is convert mp3 files to mp4 files. Safari 正在分块传输 mp4 文件,等待时间接近于 0。好吧,现在我需要做的就是将 mp3 文件转换为 mp4 文件。 The problem now is, I have requirement not to use my server for converting these files.现在的问题是,我要求不要使用我的服务器来转换这些文件。 I want to offload this expensive operation to the client.我想将这个昂贵的操作卸载给客户端。 After looking around for a bit I found a few wasm libraries to do this, great!环顾四周后,我发现了一些wasm库可以做到这一点,太棒了! Nope.不。 These wasm libraries are huge (24 MB) and would add an unacceptable amount to my already large bundles files.这些wasm库很大(24 MB)并且会给我已经很大的包文件增加不可接受的数量。 So this brings me to my question.所以这让我想到了我的问题。 Is there any way to "trick" safari into thinking that my mp3 file is mp4.有什么办法可以让 safari 认为我的 mp3 文件是 mp4。 What I've tried already:我已经尝试过的:

On input change event -> get the file -> clone it into a new Blob with a mimeType of video/mp4 and then upload that file to the server.在输入更改事件 -> 获取文件 -> 将其克隆到一个新的 Blob 中,mimeType 为video/mp4 ,然后将该文件上传到服务器。 Chrome plays this file no problem (probably because it detects it's an mp3 file), on Safari however it's unable to play the file. Chrome 播放此文件没问题(可能是因为它检测到它是一个 mp3 文件),但在 Safari 上无法播放该文件。

So my question is, is there any way to package the mp3 file in an mp4 container (Client Side !important) to "force" Safari into chunking the file.所以我的问题是,有没有办法将 mp3 文件打包在 mp4 容器(客户端!重要)中,以“强制”Safari 对文件进行分块。

Is there any way to "trick" safari into thinking that my mp3 file is mp4有什么办法可以让 safari 认为我的 mp3 文件是 mp4

mp4 files are not seekable because the client "thinks" they are mp4. mp4 文件不可查找,因为客户端“认为”它们是 mp4。 They have an index in the file that is required for the seeking to work.他们在文件中有一个索引,这是搜索工作所需的。 That index does not magically exist because the mime type was changed.该索引并不神奇地存在,因为 MIME 类型已更改。

is there any way to package the mp3 file in an mp4 container (Client Side...有什么方法可以将 mp3 文件打包到 mp4 容器中(客户端...

Yes, totally.是的,完全。 But you must write the code and it is very complicated.但是你必须写代码,而且非常复杂。 You can read ISO 14496-12 document to understand how mp4 files are structured.您可以阅读 ISO 14496-12 文档以了解 mp4 文件的结构。 and ISO 11172-3 and ISO 13818-3 to parse the Mp3 into frames that can be written to the MP4和 ISO 11172-3 和 ISO 13818-3 将 Mp3 解析为可以写入 MP4 的帧

I did this very thing last month, i only needed it for a 128kbps CBR mp3 live stream so i hard coded something to support that specific format, it can be modified to specifically support different bit rates though, but probably not VBR :/.我上个月做了这件事,我只需要它用于 128kbps CBR mp3 实时流,所以我硬编码了一些东西来支持该特定格式,虽然可以修改它以专门支持不同的比特率,但可能不是 VBR :/。 Posting it here in case its of use to anyone dealing with firefox not playing an mp3 using mediasource extensions: https://gist.github.com/fanfare/0fa525af28b275fd6623942d7e9d70dd将其张贴在这里以防任何处理 Firefox 的人不使用媒体源扩展播放 mp3: https ://gist.github.com/fanfare/0fa525af28b275fd6623942d7e9d70dd

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

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