網上很多關於lame使用教程的代碼幾乎都是在使用lame_encode_flush後就結束了編碼工作,雖然編碼後的文件能播放,但是音頻顯示時長已經錯誤了,這會引起前進後退等操作出現問題。
使用官方的lame.exe去編碼就不會有問題,我於是翻了下官方的命令行程序源碼,發現在編碼結尾後會調用lame_get_lametag_frame來獲取一個幀數據並寫入到第一幀中去。
/*
* 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的數據並寫入到第一幀中,這樣mp3的時長才是正確的。
lame也提供了lame_set_bWriteVbrTag函數來關閉插入頭幀的行爲,但幾乎用不上。另外還有一個相關的函數lame_mp3_tags_fid。
參考資料:
http://blog.csdn.net/iteye_7030/article/details/82450005
http://gabriel.mp3-tech.org/mp3infotag.html