[英]How to get the duration of a mp3 file using Cloud Functions for FIrebase when uploaded to firebase storage
I am in the process of building a music library for a dance app. 我正在为舞蹈应用程序建立音乐库。 Currently I have the Cloud Function automatically create an database instance when a mp3 file is uploaded to Firebase storage, I still need to fetch the duration of that mp3 file in seconds somehow and store that info in database.
当前,当将mp3文件上传到Firebase存储时,云功能可以自动创建数据库实例,我仍然需要以秒为单位获取该mp3文件的持续时间,并将该信息存储在数据库中。 I am new to javascript and first time doing node.js, some sources point to using the mp3 metadata which I have no experience playing with either.
我是javascript的新手,也是第一次使用node.js,一些消息源指向使用我没有经验的mp3元数据。 Some help would be much appreciated.
一些帮助将不胜感激。
The easiest way to do this would be have your cloud function download each MP3 file, open it via some MP3-reading library, and get the length using some library, like mp3-duration
. 最简单的方法是让您的云函数下载每个MP3文件,通过一些MP3读取库将其打开,然后使用诸如
mp3-duration
类的库来获取长度。
The main problem here is that downloading the entire MP3 just to calculate its duration is something of a waste of bandwidth. 这里的主要问题是,下载整个MP3只是为了计算其持续时间是浪费带宽。 You could probably improve on that, but doing so would probably be somewhat complex unless MP3 files happen to conveniently list their duration somewhere in the header (which I don't think is the case, but I'm not very familiar with the format).
您可能会对此进行改进,但这样做可能会有些复杂,除非MP3文件碰巧在标头中的某个位置方便地列出了它们的持续时间(我认为不是这种情况,但我对格式不是很熟悉) 。
It might be better to have whatever tool is uploading the objects calculate the duration locally and then add a piece of custom metadata to the object being uploaded that indicates its duration. 最好使用任何正在上载对象的工具,在本地计算持续时间,然后向正在上载的对象添加一段指示其持续时间的自定义元数据。 The custom metadata will be available to the cloud function, which can them make use of it.
自定义元数据将对云功能可用,他们可以利用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.