Forum > General

Accessing File Properties

<< < (2/3) > >>

BobDog:

Regarding:
"I am thinking ahead and will need to add another feature.  I want to add up the play time of each MP3 file that is in the Memo"
...
Windows using mcisendstring-- duration
and using filesize -- size in bytes (same as file -properties)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- function mciSendString(s:ansistring;p1:pointer;p2:longword;p3:longword):integer;cdecl external 'Winmm.dll' name 'mciSendStringA' ;        function _filesize(_file:string):int64; //standard filelength in bytes     var        f:file of byte;        begin        AssignFile(f,_file);        Reset(f);        _filesize:=FileSize(f);        close(f);        end;            function mp3length(_file:string):int64;      type    chars = array[0..50] of char;    var    open,lngth,finish:ansistring;    length:chars;    tmp:integer;    l:int64;    begin     open:= 'open  '+_file+' type mpegvideo alias file1' ;      finish:= 'close file1';     mciSendString(pchar(open), nil, 0, 0);     lngth:= 'status file1 length';     mciSendString(pchar(lngth),@length, 50,0) ;     val(length,l,tmp);       mciSendString(pchar(finish),nil, 0, 0);     exit(l);    end;        var    myfile:ansistring='C:\Users\Computer\Desktop\fb\pascal\mystuff\country.mp3'; //<<----- put in your path here.    begin   writeln('mcisendstring method  = ', mp3length(myfile));  writeln('filesize method       = ' ,_filesize(myfile));  writeln('Press enter to end . . .');  readln;  end.  Win 10 64 bits.
fpc 3.2.2
geany ide

stephanos:
Dear All

I have been steered in the direction of a plugin called Bass.  It might work.  I have joined the Unforseen forum for Bass and have received some help.  But it is pitched too high and I am struggling.  My Bass posts are dominated by one person who will not/cannot answer a straight question, so for the time being I am trying here again.  It is to be found here:
https://www.un4seen.com/forum/?topic=19590.msg137071;topicseen#msg137071

I have placed copies of bass.pass and bass.dll in the project file along with the mp3 I am practising on: BAILE MINHA MADEIRA.mp3

With some help I have adopted this solution and it compiles:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; //{$mode objfpc} {$H+}uses  bass, FileCtrl, StdCtrls, Graphics, Controls, Classes, SysUtils, LResources, Forms, Dialogs,  Interfaces;var                 EC : integer; H : HSTREAM; Info : BASS_CHANNELINFO; FloatLen : double;        L_Bytes : QWORD;  // length in bytes    L_Seconds : double;     // length in seconds        FBitrate : single;       //  begin     // File size is 2469 KB = 2,528,256 Bytes = 20,226,048 bits          H := BASS_StreamCreateFile(FALSE, pchar(Utf8Encode('BAILE MINHA MADEIRA.mp3')), 0, 0, BASS_STREAM_DECODE);      //     writeln('H = ', H);  // Output: zero     BASS_ChannelGetAttribute(H,BASS_ATTRIB_BITRATE, FBitrate); //     writeln('The bit rate is = ', FBitrate); //  Output: 0.000000000E+00     if (H <> 0) then // when H is equal to zero  the stream is not opened     begin            L_Bytes := BASS_ChannelGetLength(H, BASS_POS_BYTE); // length in bytes            writeln(L_Bytes); // -1          L_Seconds := BASS_ChannelBytes2Seconds(H, L_Bytes);   // convert bytes to seconds          writeln('True, number of seconds = ', L_Seconds);     // Output  -1          EC := BASS_ErrorGetCode();  write('Error Code = ', EC);  // Output     end     else     begin          writeln('False = H is ', H);     // False = H is 0          EC := BASS_ErrorGetCode();  writeln('Error Code = ', EC);          writeln(); writeln(); //      end;     BASS_StreamFree(H); // close the stream and free the resources  {**}     readln();end.

There appear to be a problem.  The value of H is always zero.  At first I thought this value was one of the error codes found here: https://www.un4seen.com/doc/#bass/BASS_ErrorGetCode.html
At first I thought that the zero in this list (BASS_OK ) was the same as the value in H.  Although no one from the forums has put me straight.  I am proceeding on the basis that they are two different values.  H is zero if the stream fails to open.  Assuming this is correct then my problem is that the stream to the MP3 files fails to open.  The problem might be with this line:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---H := BASS_StreamCreateFile(FALSE, pchar(Utf8Encode('BAILE MINHA MADEIRA.mp3')), 0, 0, BASS_STREAM_DECODE);
I have a little knowledge of pointers.  So could it be: pchar() not working.  Does pchar have to assign to a variable?  Something like:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---pc : pchar;pc := pchar('BAILE MINHA MADEIRA.mp3');so changing the stream to:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---H := BASS_StreamCreateFile(FALSE, pc, 0, 0, BASS_STREAM_DECODE);
I tried the above and it compiled but H was still zero.
Have I not integrated Bass into Lazarus properly?

The output looks as though the stream has not opened and data has not been read.

Any help appreciated and hopefully understood

Jorg3000:
If you need the extended properties of a media file, e.g. the playing time, look at this example
https://stackoverflow.com/questions/66803770/get-image-dimensions-without-loading-using-meta-data

Fred vS:
Hello.

If you dont have luck with other propositions, you may try uos.
There are plenty of demos in /uos/examples/ for Lazarus that work out-of-the-box.

Fre;D

dbannon:

stephanos, to be honest, it appears to me that "Chris" has given you some pretty detailed and complete answers. I suggest you go back and have a further look at using Bass.

Chris suggested a loop because thats how most repetitive tasks are handled in computers generally. Reading the details of each file is a repetitive task.

Its often hard to read code that new to you, have a read of Chris's code, line by line, understand what each line is doing and only then try to think of it as comple 'function'. There are a identifier names there that are probably described in Bass's documentation.

Good luck.

Davo

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version