6

我不能使用任何由 Fraunhofer 获得专利的 mp3 代码,所以没有编码器或解码器(例如 ffmpeg、lame、MAD 等),而且它太大了。

我在 Windows 上执行此操作,但 DirectShow 的 IMediaDet 似乎会随着时间的推移而变慢,调用它几百次会使我的系统爬行,甚至重新使用相同的接口对象并只放置文件名并获取持续时间!

那么,是否有一些代码可以使用 C/C++ 读取 VBR 文件并获取持续时间?

这里有另一篇文章用 C++ 进行 CBR,但是代码做了很多假设,当然不适用于 VBR。

4

4 回答 4

4

大多数 MP3 文件都有一个ID3 标头。解码并获得持续时间并不难。

这是一些非常基本且丑陋的代码来说明该技术。

#include <iostream>
#include <iomanip>

size_t GetMP3Duration(const std::string sFileName);

int main(int argc, char* argv[])
{
    try
    {
        size_t nLen = GetMP3Duration(argv[1]);
        if (nLen==0)
        {
            std::cout << "Not Found" << std::endl;
        }
        else
        {
            std::cout << nLen << " miliseconds" << std::endl;
            std::cout << nLen/60000 << ":";
            nLen %= 60000;
            std::cout << nLen/1000 << ".";
            std::cout << std::setw(3) << std::setfill('0') << nLen%1000 << std::endl;
        }
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
    }
    return 0;
}

#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>

unsigned DecodeMP3SafeInt(unsigned nVal)
{
    // nVal has 4 bytes (8-bits each)
    //  - discard most significant bit from each byte
    //  - reverse byte order
    //  - concatenate the 4 * 7-bit nibbles into a 24-bit size.
    unsigned char *pValParts = reinterpret_cast<unsigned char *>(&nVal);
    return (pValParts[3] & 0x7F)         |
            ((pValParts[2] & 0x7F) << 7)  | 
            ((pValParts[1] & 0x7F) << 14) | 
            ((pValParts[0] & 0x7F) << 21);
}

#pragma pack(1)
struct MP3Hdr {
    char tag[3];
    unsigned char maj_ver;
    unsigned char min_ver;
    unsigned char flags;
    unsigned int  size;
};
struct MP3ExtHdr {
    unsigned int  size;
    unsigned char num_flag_bytes;
    unsigned char extended_flags;
};
struct MP3FrameHdr {
    char frame_id[4];
    unsigned size;
    unsigned char flags[2];
};
#pragma pack()

size_t GetMP3Duration(const std::string sFileName)
{
    std::ifstream fin(sFileName.c_str(), std::ifstream::binary);
    if (!fin) 
        throw std::invalid_argument("Cannot open file");

    // Read Header
    MP3Hdr hdr = { 0 };
    fin.read(reinterpret_cast<char *>(&hdr), sizeof(hdr));
    if (!fin.good())
        throw std::invalid_argument("Error reading file");

    if (0 != ::memcmp(hdr.tag, "ID3", 3))
        throw std::invalid_argument("Not an MP3 File");

    // Read extended header, if present
    if (0 != (hdr.flags&0x40))
    {
        fin.seekg(sizeof(MP3ExtHdr), std::ifstream::cur);
        if (!fin.good())
            throw std::invalid_argument("Error reading file");
    }

    // read a chunk of file.
    const size_t nDefaultSize(2048);
    std::vector<char> vBuff(nDefaultSize);
    fin.read(&vBuff[0], vBuff.size());
    size_t nSize = fin.gcount();
    if (!nSize)
        throw std::invalid_argument("Error reading file");
    vBuff.resize(nSize);

    size_t nUsed = 0;
    while (nSize-nUsed > sizeof(MP3FrameHdr))
    {
        MP3FrameHdr *pFrame = reinterpret_cast<MP3FrameHdr *>(&vBuff[nUsed]);
        nUsed += sizeof(MP3FrameHdr);
        size_t nDataLen = DecodeMP3SafeInt(pFrame->size);
        if (nDataLen > (nSize-nUsed))
            throw std::invalid_argument("Corrupt file");

        if (!::isupper(pFrame->flags[0])) // past end of tags
            return 0;

        if (0 == ::memcmp(pFrame->frame_id, "TLEN", 4))
        {
            // skip an int
            nUsed += sizeof(int);
            // data is next
            return atol(&vBuff[nUsed]);
        }
        else
        {
            nUsed += nDataLen;
        }
    }
    return 0;
}
于 2010-08-19T09:33:33.350 回答
2

杰夫,

唯一有效的方法是遍历整个 mp3 文件,找到其中的每个 mp3 帧并计算它们的总持续时间。

mp3 文件的主要特点是它们的密度可能不同,并且其中可能包含大量其他二进制数据。例如 ID3 标签,任何解码器在读取时都会跳过。

无论如何 - 在这里查找 mp3 帧头信息:

http://www.mp3-converter.com/mp3codec/mp3_anatomy.htm

尝试创建将正确解析标题的代码,计算它们的持续时间(从采样频率),然后总计所有帧的持续时间。

您不必解码帧,只需使用它们的标头即可。

如果您不介意 LGPL,请尝试http://sourceforge.net/projects/mpg123net/

于 2010-09-07T11:43:36.027 回答
1

我找到了一个库,LGPL v3: http: //www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx

于 2010-08-17T18:18:30.880 回答
-1

tagLibid3lib怎么样?

它们本身不是解码器,它们更多的是提取曲目/艺术家/专辑和其他信息,这些信息将使您能够做您需要做的事情......

于 2010-08-17T18:28:40.307 回答