當前位置:首頁 » 編程語言 » mp3解碼c語言

mp3解碼c語言

發布時間: 2023-06-10 11:38:51

『壹』 c語言程序設計MP3或MP4文件基本信息的解析

找了個MP3看了下,comment是29個位元組。
可參考 http://www.chineselinuxuniversity.net/articles/27374.shtml

拿VC的C語言寫了下:
頭文件:
#include <stdio.h>

// TODO: reference additional headers your program requires here

#define MP3_ID3_LENGTH 10
typedef struct tag_ID3_INFO
{

} ID3_INFO;

#define IDENTIFY_LEN 3
#define TITLE_LEN 30
#define ARTIST_LEN 30
#define ALBUM_LEN 30
#define YEAR_LEN 4
#define COMMENT_LEN 30
#define GENRE_LEN 1

// IDENTIFY_LEN + TITLE_LEN +...
#define MP3_INFO_LENGTH 128

typedef struct tag_MP3_INFO
{
char Identify[IDENTIFY_LEN + 1]; //TAG三個字母
//這里可以用來鑒別是不是文件信息內容
char Title[TITLE_LEN + 1]; //歌曲名,30個位元組
char Artist[ARTIST_LEN + 1]; //歌手名,30個位元組
char Album[ARTIST_LEN + 1]; //所屬唱片,30個位元組
char Year[YEAR_LEN + 1]; //年,4個位元組
char Comment[COMMENT_LEN + 1]; //注釋,28個位元組
char Genre[GENRE_LEN + 1]; //類型 ,1個位元組
} MP3_INFO;

C文件:
#include <string.h>

#define MP3_OK 1
#define MP3_ERROR 1

FILE *OpenMp3File(char *pFileName);
int ReadMP3Info(FILE *pFile, MP3_INFO *pstInfo);
int OutputMP3Info(MP3_INFO *pstMp3Info);

int main(int argc, char* argv[])
{
FILE *pFile = NULL;
MP3_INFO stMp3Info = {0};
char *pfname = "E:\\Project\\MP3\\test.MP3";

pFile = OpenMp3File(pfname);
if (NULL == pFile)
{
return MP3_ERROR;
}

ReadMP3Info(pFile, &stMp3Info);

printf("\r\nMP3 file: %s", pfname);
OutputMP3Info(&stMp3Info);

printf("Hello World!\n");
return MP3_OK;
}

FILE *OpenMp3File(char *pFileName)
{
FILE *pFile = NULL;
pFile = fopen(pFileName,"rb");
if (NULL==pFile)
{
printf("open read file error!!");
return NULL;
}

return pFile;
}

int ReadMP3Info(FILE *pFile, MP3_INFO *pstInfo)
{
int len = 0;
if ((NULL == pFile) || (NULL == pstInfo))
{
return MP3_ERROR;
}

fseek(pFile, 0, SEEK_END);
len = ftell(pFile);
if (len <= (MP3_INFO_LENGTH + MP3_ID3_LENGTH))
{
return MP3_ERROR;
}

memset(pstInfo, 0, sizeof(MP3_INFO));

fseek(pFile, -MP3_INFO_LENGTH, SEEK_END);
len = fread((char *)(pstInfo->Identify), 1, IDENTIFY_LEN, pFile);
len += fread((char *)(pstInfo->Title), 1, TITLE_LEN, pFile);
len += fread((char *)(pstInfo->Artist), 1, ARTIST_LEN, pFile);
len += fread((char *)(pstInfo->Album), 1, ALBUM_LEN, pFile);
len += fread((char *)(pstInfo->Year), 1, YEAR_LEN, pFile);
len += fread((char *)(pstInfo->Comment), 1, COMMENT_LEN, pFile);
len += fread((char *)(pstInfo->Genre), 1, GENRE_LEN, pFile);
if (MP3_INFO_LENGTH != len)
{
return MP3_ERROR;
}

return MP3_OK;
}

int OutputMP3Info(MP3_INFO *pstMp3Info)
{
printf("\r\nTag : %s", pstMp3Info->Identify);
printf("\r\nTitle : %s", pstMp3Info->Title);
printf("\r\nArtist : %s", pstMp3Info->Artist);
printf("\r\nAlbum : %s", pstMp3Info->Album);
printf("\r\nYear : %s", pstMp3Info->Year);
printf("\r\nComment: %s", pstMp3Info->Comment);

return MP3_OK;
}

測試:
MP3 file: E:\Project\MP3\test.MP3
Tag : TAG
Title : TrackTitle
Artist : ArtistName
Album : AlbumTitle
Year : 2012
Comment: This is a comment

『貳』 C語言怎麼輸出mp3的二進制編碼

使用fopen以二進制只讀方式打開mp3文件,用fseek求得文件大小,申請一片相應大小的內存空間用作緩沖區,然後將文件整個讀入到緩沖區中。

//二進制方式打開文件
FILE*fp=fopen("example.mp3","rb");
if(NULL==fp)
{
printf("Unabletoopenmp3file. ");
exit(1);
}

//求得文件的大小
fseek(fp,0,SEEK_END);
intsize=ftell(fp);
rewind(fp);

//申請一塊能裝下整個文件的空間
char*buffer=(char*)malloc(sizeof(char)*size);

//讀文件
fread(buffer,size,1,fp);
熱點內容
紅米手機錄音文件夾 發布:2025-02-13 19:41:33 瀏覽:235
android適配屏幕 發布:2025-02-13 19:40:30 瀏覽:792
解壓球0 發布:2025-02-13 19:38:19 瀏覽:642
早春開花植物如何配置 發布:2025-02-13 19:22:19 瀏覽:50
安卓怎麼獲得root錄音許可權 發布:2025-02-13 19:21:22 瀏覽:170
訪問學者留學基金委 發布:2025-02-13 19:21:13 瀏覽:430
制定編程 發布:2025-02-13 19:11:39 瀏覽:58
微商相冊安卓與蘋果機哪個方便 發布:2025-02-13 19:10:02 瀏覽:6
優酷視頻緩存設置 發布:2025-02-13 19:04:03 瀏覽:156
如何識別網路配置 發布:2025-02-13 19:04:02 瀏覽:300