asp.net 从 mp3 创建波形图像
我正在寻找一种方法来创建上传到服务器的 mp3 文件的图形波形。经过一番研究,我相信 mp3 需要首先转换为原始格式...但我不知道如何为该声音文件创建 .gif 波形格式。
90% 的声音文件长度超过 60 分钟。
我知道这无法在上传后立即完成,需要将其放入队列中并进行处理。
我看过几个库,例如lame,但似乎没有一个能够实现我正在寻找的东西。
任何指向正确方向的指示将不胜感激!
非常感谢! 保罗
I am looking for a way to create a graphical waveform of mp3 files uploaded to a server. From a little research i beleive the mp3 would need converting to a raw format first...but i have no idea on how to create a .gif format of the waveform for this sound file.
90% of the sound files would be over 60 minutes in length.
I understand this wouldn't be able to done right after it is uploaded, it would need to be placed into a queue and processed.
I have looked at several libaries such as lame, but none seem to be able to achieve what i am looking for.
Any pointers into the right direction would be greatly appreciated!
Thank you very much!
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一步是解压缩 mp3。只要您将其作为批处理作业执行,而不是使用 LAME 作为库,只需使用现有的命令行程序将 mp3 转换为临时 WAV 文件,就会容易得多。然后找到一个库来读取 WAV 文件 - 这是一种相对简单的格式,您应该在网上找到大量示例代码,或者您可以在一个下午编写自己的代码。
假设您的歌曲长 60 分钟:60 分钟 * 60 秒/分钟 * 44100 个样本/秒 = 158,760,000 个样本。 (如果是立体声歌曲,则为两倍。)如果图像的宽度为 1000 像素,则您只想为每 158,760 个样本显示一个样本。
(顺便说一句,在该分辨率下您看不到太多细节。也许更好的解决方案是仅显示前 5 分钟的波形,或者渲染用户可以滚动的更大图像?)
无论如何,您想要读取 158,760 个样本的每个块的音频样本(在本例中),并将其渲染为一条垂直线,表示该部分音频的信号强度。有两种方法可以实现此目的:
最大值将显示峰值,而 RMS 将显示峰值向您展示总体感知响度。两者都很容易实现;两者都尝试一下,看看哪一个看起来最好。
然后你只需要把生成的图像转换成 gif 即可。因为无论如何这是一个批处理作业,如果我是你,我会写出一个 BMP 文件(一种非常简单的文件格式),然后使用像 ImageMagick 的“convert”这样的命令行程序将其转换为 GIF。
最后,最后一点:如果您真的很狡猾,您可以读取 MP3 帧并直接从比特流中提取增益,而无需解码整个内容。这就是我所做的这里,欢迎您使用它 - 但它不适合胆小的人。它比解码完整 MP3 大约快 100 倍,但您得到的波形将是一个粗略的近似值。
The first step is to uncompress the mp3. As long as you're doing this as a batch job, rather than use LAME as a library, just use an existing command-line program to convert the mp3 to a temporary WAV file, that will be much easier. Then find a library to read WAV files - it's a relatively simple format and you should find lots of sample code online, or you could write your own in an afternoon.
Suppose your song is 60 minutes long: 60 minutes * 60 seconds/minute * 44100 samples/second = 158,760,000 samples. (Twice that if it's a stereo song.) If your image is 1000 pixels wide, you only want to display one sample for every 158,760 samples.
(As an aside, you won't see much detail at that resolution. Perhaps a better solution would be to show a waveform of just the first 5 minutes, or render a larger image that the user can scroll?)
Anyway, you want to read the audio samples for each block of 158,760 samples (in this example), and render it as a vertical line representing the strength of the signal over that portion of the audio. There are two ways to do this:
Maximum will show you peaks, while RMS will show you the overall perceived loudness. Both are easy to implement; try both and see which one looks best.
Then you just need to turn that resulting image into a gif. Since this is a batch job anyway, if I were you, I would write out a BMP file (a really easy file format) and then use a command-line program like ImageMagick's "convert" to turn that into a GIF.
Finally, one last note: if you're really tricky, you could read the MP3 frames and extract the gain directly from the bitstream without decoding the whole thing. That's what I did here, and you're welcome to use it - but it's not for the faint of heart. It's roughly 100x faster than decoding the full MP3, but the waveform you get will be a crude approximation.
您可能会找到 FMOD 或 BASS 可以为您轻松做到这一点。我相信 FMOD 下载附带了一个可视化示例,因此您可以从中推断。他们都读取 MP3 并执行必要的计算。
You may find FMOD or BASS can do this for you, quite easily. I believe the FMOD download comes with a visualisation example, so you can extrapolate from that. They both read in MP3's and perform the necessary calculations.
您可以使用此处的C#代码来生成波形图像。然后,您应该使用通用的 System.Drawing 库将其保存到 GIF 文件。
You can use the C# code here to generate the wave form image. Afterward, you should use the common
System.Drawing
library to save it to a GIF file.