1

I'm trying to convert a WAV file into MP3 file using LAME (win7,vs2010,c++).

I found this code:

convert wav to mp3 using lame

The convert works fine, but when i'm trying to open the file using windows media player the length of the file is wrong.

Is there any way to fix this using lame lib?(not with another program or another lib or command line,only with c++ code...)

EDITED: after some reading i did i tried to use the lame_get_lametag_frame function as sellibitze suggested. here is my code:

#include <stdio.h>
#include <lame/lame.h>

int main(void)
{
  int read, write;

  FILE *pcm = fopen("in.pcm", "rb");
  FILE *mp3 = fopen("out.mp3", "wb");

  const int PCM_SIZE = 8192;
  const int MP3_SIZE = 8192;

  short int pcm_buffer[PCM_SIZE*2];
  unsigned char mp3_buffer[MP3_SIZE];

  lame_t lame = lame_init();
  lame_set_in_samplerate(lame, 44100);
  lame_set_VBR(lame, vbr_default);
  lame_set_write_id3tag_automatic(lame, 0);
  lame_init_params(lame);

  char buffer[256];
  int imp3=lame_get_id3v2_tag(gfp, buffer, sizeof(buffer));
  fwrite(buffer, 1, imp3, outf);
  long audio_pos=ftell(outf); // store beginning of audio data

  do {
    read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
    if (read == 0)
        write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
    else
        write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
    fwrite(mp3_buffer, write, 1, mp3);
  } while (read != 0);

  imp3=lame_get_id3v1_tag(gfp, buffer, sizeof(buffer));
  fwrite(buffer, 1, imp3, outf);

  imp3=lame_get_lametag_frame(gfp, buffer, sizeof(buffer));
  fseek(outf,audio_pos,SEEK_SET); // remember beginning of audio data
  fwrite(buffer, 1, imp3, outf);

  lame_close(lame);
  fclose(mp3);
  fclose(pcm);

  return 0;
}

FIXED:

I manged to fix the problem but i don't really understand how it fix it. i change the name of the mp3 file from "out.mp3" to any other name and wmp show the right length. also i tried to change the name of files already created from out to something else and it worked. can anybody explain to me way it's happened? is the name out.mp3 saved?

Community
  • 1
  • 1
David
  • 287
  • 2
  • 3
  • 13

1 Answers1

0

The example code you liked to uses the VBR mode. Length information in that case is typically put into the first frame as metadata. This is known as Xing/VBR header. It also includes a low accuracy seek table. But this information is obviously only available after you passed all the audio data to LAME. I suggest you look for a function in the LAME API that is able to update the Xing/VBR header to reflect the correct length and seek table and call it before you close the file.

lame_encode_flush does not take your FILE* thingy so it cannot seek back to the beginning of the file and update the first mp3 frame with the Xing/VBR header.

sellibitze
  • 27,611
  • 3
  • 75
  • 95
  • isn't the function lame_encode_flush() write the id3 tag and the correct length? i always get the same legth of 1:24 min. But when i'm looking in the win 7 explorer it shows the correct length. is there any way to get the value from windows? how this value in the Xing/VBR header is saved?(in which format,how many bytes etc...) – David Apr 07 '13 at 19:14
  • @user2171244: see my amendment – sellibitze Apr 07 '13 at 19:50
  • is there any func that does that?(in lame API) or the only way is to update the id3v2? if i will change it to use CBR,will it fix it? – David Apr 07 '13 at 20:48
  • @user2171244: Please read the documentation in the header file with respect to `lame_get_lametag_frame` – sellibitze Apr 07 '13 at 21:34
  • The new changes to the code i did change the time bar in wmp but it still not the correct length. Am i doing something wrong? – David Apr 09 '13 at 18:45
  • @user2171244: Have you made sure that WMP is actually smart enough to figure out the correct length according to a Xing/VBR/Lame tag? Try foobar2000 as player. It definitely supports this kind of tag. – sellibitze Apr 10 '13 at 17:23
  • i tried wmp in xp and it worked there fine, but in win 7 wmp don't show it right.is there anyithing i can do with lame to fix it? – David Apr 10 '13 at 18:18
  • @user2171244: Looks OK to me at first glance. Check other software like Foobar2000 that actually supports the VBR tag. – sellibitze Apr 11 '13 at 11:51
  • I don't understand why I need to check in foobar2000 if my Convert works. I only want it to work in wmp that is provided with win 7 – David Apr 11 '13 at 11:58
  • @user2171244: I know. But it does not seem to work in WMP, does it? What are the possible reasons for your observations? Name at least two! – sellibitze Apr 11 '13 at 21:52
  • i'm trying to make the file work on wmp in win 7, i tried to paly the file in wmp in xp and it work just fine, i read that in win 7 wmp is looking for id3v2 TLEN tag to read the length of the track from it,because i'm using vbr he read the first frames and calculate the length by that. I'm just trying to simply convert a wav file to mp3 file, i don't really care how to do it exept i want to use only lame as a third party lib. is there any way to simply do that? – David Apr 11 '13 at 22:37
  • @user2171244: The thing is: I don't know whether you program as it is now actually writes a proper LAME tag. I also don't know whether WMP cares about the LAME tag. I won't do this investigation for you. This is something you have to do on your own. I suggested to try FB2K so that you can be sure of your program generating the correct LAME tag. If FB2K reports the wrong length, your program is buggy. If it reports the correct length, this means that WMP does not care about the LAME tag, in which case you have to figure out if and how WMP can be made to report the correct time. Good luck! – sellibitze Apr 12 '13 at 13:14
  • I just want to add that the Xing/VBR/LAME tag is a quasi standard. Under the assumption that your program does what you intend, Microsoft seems to be ignorant about this quasi standard. I would consider the MP3 file to be proper but WMP to be too stupid to report the correct length. My job is done here. – sellibitze Apr 12 '13 at 13:19