使用LAME的id3tag_set_albumart进行挑战 [英] Challenge with id3tag_set_albumart of LAME

查看:79
本文介绍了使用LAME的id3tag_set_albumart进行挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LAME将波形文件转换为mp3(我已成功完成)。我也尝试将id3tag写入mp3,这在id3tag_set_albumart方面取得了成功。

通过前端文件夹中的parse.c,我发现id3tag_set_albumart的第二个参数(图片)将是图像文件的内容,而第三个参数将是图像文件的大小。

但是当我这样做(下面的代码)时,它返回0,根据lame.h是成功但编码拒绝发生(即波形文件转换为mp3)。



 FILE * fpi =  0 ; 
char * albumart = 0 ;
char * filename = ty.jpg ;
fpi = fopen(filename, rb);
size_t 大小;
fseek(fpi, 0 ,SEEK_END);
size = ftell(fpi);
fseek(fpi, 0 ,SEEK_SET);
albumart =( char *)malloc(size);
fread(albumart, 1 ,size,fpi);
id3tag_set_albumart(lame,albumart,size);
免费(albumart);
fclose(fpi);





编辑:---

我什么时候打电话给 id3tag_set_albumart lame_encode_buffer_interleaved 返回-1,根据lame.h表示 mp3buf 太小。我不认为这与数组的大小有关,因为我将它增加到目前大小的两倍,但函数仍然返回-1

解决方案

好吧,让我们仔细看看:



 FILE * fpi = 0; //初始文件结构设置为零。 
char * albumart = 0; //初始char指针设置为零。
char * filename =ty.jpg; //初始文件名设置为ty.jpg。
size_t size; //初始大小变量未初始化,_B $ unsigned int类型在
//我的系统上,你的可能是_W32 unsigned int类型。

//在这里,我假设你在READ |中打开JPG二进制模式。
fpi = fopen(filename,rb);
fopen(& fpi,filename,rb,IMG = encoding); //这就是它正确完成的方式。
/ *
r打开阅读。如果文件不存在或找不到,则fopen调用失败。
b以二进制(未翻译)模式打开;涉及回车符和换行符的翻译被抑制。

在这里你'寻找'文件,因为你没有注明,因为你不知道。
如何定义:int fseek(FILE * stream,long offset,int origin);

从结尾查看文件,如果有返回值则不知道。
* /
fseek(fpi,0,SEEK_END); //没有为你做任何事
//在这里,我认为,有可能打开文件,你试图获得大小它的。
size = ftell(fpi); //但是,由于您以二进制模式打开它,它可能不适用于_W64 unsigned int类型。 ftell和_ftelli64返回当前文件位置。 ftell和_ftelli64返回的值可能无法反映在文本模式下打开的流的物理字节偏移量,因为文本模式会导致回车换行转换。将ftell与fseek或_ftelli64与_fseeki64一起使用可以正确返回文件位置。出错时,ftell和_ftelli64会调用无效参数处理程序,如参数验证中所述。

//由于你在文件末尾并使用fseek移动了0Bytes,ftell告诉你在EOF
//通过读取数据移动指针:
fread (list,sizeof(char),100,stream);
//读取后获取头寸:
position = ftell(stream);
printf(尝试读取100个字节后的位置:%ld \ n,
位置);
//您的头寸是EOF,
/ *
SEEK_SET =文件开头。

您可以使用fseek和_fseeki64将指针重新定位到文件中的任何位置。指针也可以位于文件末尾之外。 fseek和_fseeki64清除文件结束指示符,并否定任何先前的ungetc调用对流的影响。

打开文件以附加数据时,当前文件位置由上一次I / O操作确定,而不是由下一次写入的位置决定。如果在打开以进行追加的文件上尚未发生I / O操作,则文件位置是文件的开头。

对于以文本模式打开的流,fseek和_fseeki64限制使用,因为回车换行翻译可能导致fseek和_fseeki64产生意外结果。

保证在文本模式下打开的流上运行的唯一fseek和_fseeki64操作是:
•相对于任何原始值寻求偏移量为0。
•在使用fseek时,从文件的开头寻找一个从ftell调用返回的偏移值。

这表示要启动文件并移动0个度量单位。
fseek(fpi,0,SEEK_SET); //
albumart =(char *)malloc(size); //不起作用,因为size = 0
size = fseek(fpi,SEEK_END,SEEK_SET); //这可能效果更好

但是:
* /
// albumart =(char *)malloc(size); fread(albumart,1,size,fpi);这更好:
// size_t fread(void * buffer,size_t size,size_t count,FILE * stream); //读取文件


size = fread(albumart,(char),sizeof(fpi),fpi); //这将读取文件并提供正确的大小并存储albumart中的数据。

id3tag_set_albumart(lame,albumart,size); //所以现在如果你做了上面的陈述,那么这可能会奏效。
免费(albumart);
fclose(fpi);



或类似的东西......


 FILE * fpi = 0; 
char * albumart = 0;
char * filename =ty.jpg; //初始文件名设置为ty.jpg。
size_t size;
fopen(& fpi,filename,rb,IMG = encoding);
size = fseek(fpi,SEEK_END,SEEK_SET); //这会让你获得fpi
fread(albumart,(char),size,fpi)的大小; //这将根据'char'的字节大小将图像数据放入'albumart'中,并将其读取并计入'size'

// OR
// size = fread(albumart,(char),sizeof(fpi),fpi); //在一个陈述中做同样的事情。
id3tag_set_albumart(lame,albumart,size);
免费(albumart);
fclose(fpi);





参数

--------- -------------------------------------------------- ---------------------

buffer =数据的存储位置。

size =项目大小(字节) 。

count =要读取的最大项目数。

stream =指向FILE结构的指针。



size_t =返回值

-------------------------------------- ------------------------------------------

fread返回实际读取的完整项目的数量,如果发生错误或者在到达计数之前遇到文件末尾,则可能小于计数。使用feof或ferror函数将读取错误与文件结束条件区分开来。如果size或count为0,则fread返回0并且缓冲区内容不变。如果stream或buffer是空指针,则fread将调用无效参数处理程序,如参数验证中所述。如果允许继续执行,则此函数将errno设置为EINVAL并返回0.

------------------------- -------------------------------------------------- ---------

AND如果你的值只有4,对于sizeof(fpi),则你没有打开文件或读取它。


查看 id3lib ,我想我会满足于阅读和编辑mp3中的id3标签。它对于id3标签写入甚至比lame有一些优势,例如对用于albumart和其他东西的图​​像文件的大小没有限制。


Am presenting trying to convert a wave file to mp3 using LAME (which I have succeeded in doing). I also tried writing the id3tag to the mp3 which had been a success up to the point of id3tag_set_albumart.
Going through the parse.c in the frontend folder, I discovered that the second parameter of id3tag_set_albumart (image) will be the content of the image file while the third parameter will be the size of the image file.
But when I did this (code below), it return 0 which according to the lame.h is success but the encoding refuse to take place (i.e. The wave file converting to mp3).

FILE* fpi = 0;
char *albumart = 0;
char *filename = "ty.jpg";
fpi = fopen(filename, "rb");
size_t size;
fseek(fpi, 0, SEEK_END);
size = ftell(fpi);
fseek(fpi, 0, SEEK_SET);
albumart = (char*)malloc(size);
fread(albumart, 1, size, fpi);
id3tag_set_albumart(lame, albumart, size);
free(albumart);
fclose(fpi);



EDIT:---
When ever I call id3tag_set_albumart, lame_encode_buffer_interleaved returns -1 which according to lame.h means mp3buf was too small. I don't think this as to do with the "size of the array" because I increased it to twice it present size but the function still returns -1

解决方案

Well let's take a closer look:

    FILE* fpi = 0;  //Initial file structure set to zero.
    char *albumart = 0; //Initial char pointer set to zero.
    char *filename = "ty.jpg";//Initial file name set to "ty.jpg".
    size_t size;//Initial size variable not initialized, of _W64 unsigned int type on 
                // my system, yours may be a _W32 unsigned int type.
  
//Here, I presume, you're opening the JPG in READ | BINARY mode. 
    fpi = fopen(filename, "rb");
    fopen(&fpi, filename, "rb, IMG= encoding "); //This is how it is done correctly.
/*
"r"   Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"b"   Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed. 

Here you 'seek' the file, for what don't know as you didn't indicate it. 
How it is defined: int fseek(FILE *stream, long offset, int origin );

    Look at the file from end, if there is a return value it is unknown.
*/
    fseek(fpi, 0, SEEK_END);// not doing anything for you
// Here, I presume, that having possibly opened the file, you try to get the size of it.
    size = ftell(fpi);// However since you opened it in binary mode, it may not work with _W64 unsigned int type. ftell  and _ftelli64 return the current file position. The value returned by ftell and _ftelli64 may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return–linefeed translation. Use ftell with fseek or _ftelli64 with _fseeki64 to return to file locations correctly. On error, ftell and _ftelli64 invoke the invalid parameter handler, as described in Parameter Validation.     

//Since you're at the end of the file and moved 0Bytes using fseek, ftell tells you are at EOF 
      // Move the pointer by reading data: 
      fread( list, sizeof( char ), 100, stream );
      // Get position after read: 
      position = ftell( stream );
      printf( "Position after trying to read 100 bytes: %ld\n",
              position );
// Your position is EOF, 
/*
SEEK_SET = Beginning of file.

You can use fseek and _fseeki64 to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek and _fseeki64clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.

When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file. 

For streams opened in text mode, fseek and _fseeki64have limited use, because carriage return–line feed translations can cause fseek and _fseeki64to produce unexpected results. 

The only fseek and _fseeki64 operations guaranteed to work on streams opened in text mode are: 
•Seeking with an offset of 0 relative to any of the origin values.
•Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseek.

     This says to move to start the file and move 0 units of measure.
     fseek(fpi, 0, SEEK_SET); //    
     albumart = (char*)malloc(size);// doesn't work because size = 0
     size = fseek(fpi, SEEK_END, SEEK_SET); //This might have worked better.

However:
*/
//   albumart = (char*)malloc(size); fread(albumart, 1, size, fpi); This is better as:
//  size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); // Read the file


    size = fread(albumart,(char),sizeof(fpi),fpi);// This reads the file and gives you the correct size and stores the data in albumart. 

    id3tag_set_albumart(lame, albumart, size); // So now if you did the above statement then this might work.
    free(albumart);
    fclose(fpi);


Or something like that ...


    FILE* fpi = 0;
    char *albumart = 0;
    char *filename = "ty.jpg";//Initial file name set to "ty.jpg".
    size_t size;
    fopen(&fpi, filename, "rb, IMG= encoding ");     
    size = fseek(fpi, SEEK_END, SEEK_SET); // This gets you the size of fpi
    fread(albumart,(char), size, fpi); //This will put the data of the image into 'albumart' based upon the byte size of 'char' and whatever got read and counted into 'size'

//OR
//  size = fread(albumart,(char), sizeof(fpi), fpi); //Does the same thing in one statement.
    id3tag_set_albumart(lame, albumart, size);
    free(albumart);
    fclose(fpi);



Parameters
--------------------------------------------------------------------------------
buffer = Storage location for data.
size = Item size in bytes.
count = Maximum number of items to be read.
stream = Pointer to FILE structure.

size_t = Return Value
--------------------------------------------------------------------------------
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged. If stream or buffer is a null pointer, fread invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.
------------------------------------------------------------------------------------
AND if your are only getting a value of 4, for sizeof(fpi), then you didn't open the file, or read it.


Looked into id3lib and I think I will settle for that to read and edit the id3 tags inside the mp3. It even has some advantages over lame for id3 tag writing like no restriction for the size of the image file to use for the albumart and other stuffs.


这篇关于使用LAME的id3tag_set_albumart进行挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆