lame编程实现wav转mp3后时长错误的问题
生活随笔
收集整理的這篇文章主要介紹了
lame编程实现wav转mp3后时长错误的问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
網(wǎng)上很多關于lame使用教程的代碼幾乎都是在使用lame_encode_flush后就結束了編碼工作,雖然編碼后的文件能播放,但是音頻顯示時長已經(jīng)錯誤了,這會引起前進后退等操作出現(xiàn)問題。
使用官方的lame.exe去編碼就不會有問題,我于是翻了下官方的命令行程序源碼,發(fā)現(xiàn)在編碼結尾后會調用lame_get_lametag_frame來獲取一個幀數(shù)據(jù)并寫入到第一幀中去。
/** OPTIONAL:* lame_get_lametag_frame copies the final LAME-tag into 'buffer'.* The function returns the number of bytes copied into buffer, or* the required buffer size, if the provided buffer is too small.* Function failed, if the return value is larger than 'size'!* Make sure lame_encode flush has been called before calling this function.* NOTE:* if VBR tags are turned off by the user, or turned off by LAME,* this call does nothing and returns 0.* NOTE:* LAME inserted an empty frame in the beginning of mp3 audio data,* which you have to replace by the final LAME-tag frame after encoding.* In case there is no ID3v2 tag, usually this frame will be the very first* data in your mp3 file. If you put some other leading data into your* file, you'll have to do some bookkeeping about where to write this buffer.*/ size_t CDECL lame_get_lametag_frame(const lame_global_flags *, unsigned char* buffer, size_t size);注釋最后幾行說了,lame默認會在mp3開頭插入一個空幀(稱為XING Frame),所以我們應該在lame_encode_flush操作結束后調用lame_get_lametag_frame來取得XING Frame的數(shù)據(jù)并寫入到第一幀中,這樣mp3的時長才是正確的。
lame也提供了lame_set_bWriteVbrTag函數(shù)來關閉插入頭幀的行為,但幾乎用不上。另外還有一個相關的函數(shù)lame_mp3_tags_fid。
參考資料:
http://blog.csdn.net/iteye_7030/article/details/82450005
http://gabriel.mp3-tech.org/mp3infotag.html
總結
以上是生活随笔為你收集整理的lame编程实现wav转mp3后时长错误的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python PIL 图像增强
- 下一篇: 多家波卡生态项目招聘开发者,高薪职位等你