Solved

.length to minutes and seconds format

Posted on 1998-05-04
17
210 Views
Last Modified: 2010-04-06
hi,

I making a program using the mpegdll.dll, it has a .length property, to display the time, but it is in milliseconds. I tried converting it to Minutes and seconds but had no luck, can anyone show me how to do it. Currently I have this

T := ((MPEG1.Length div 1000) div 60);
I := IntToStr(T);
Label3.Caption := I

Now the time I get is rounded like say 4 minutes, how do I get the seconds too, and how to format the label so it is displayed like this 0:00 (Minutes:Seconds). I'm not even sure my calculation is correct so please show the full procedure.
Thanks
0
Comment
Question by:hellfire052497
  • 8
  • 5
  • 2
  • +1
17 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1341169
Hi Hellfire,

If you use div you will always get an integer; It rounds everything so you can assign the result to an integer.

Use / and you get a float or double ---> if you need numbers after comma, just take this float minus the integer part of it and you got it!

Regards ZiF.
0
 

Author Comment

by:hellfire052497
ID: 1341170
Ok, but how do I format the Label. Can you show me
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341171
Well, I assume we can't use TimeToStr --> this uses the numbers behind the comma as time...

We could do it like this :

T := ((MPEG1.Length / 1000) / 60);

label3.Caption := IntToStr(Trunc(T)) + ':'+IntToStr(Frac(T));

Regards, Zif.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:hellfire052497
ID: 1341172
I got the double, but I cannot convert it to Integer, I always get "invalid typecast) when using S := (Integer(T));
A sample would help.
Thanks
0
 

Author Comment

by:hellfire052497
ID: 1341173
Added the last question before your second answer, will try that first.
0
 

Author Comment

by:hellfire052497
ID: 1341174
nope, double and string are no go. Still no luck with the Double to Integer part.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341175
Hi hellfire,

T is double? Correct?
S is String? Correct?

To translate T to string you must use FloatToStr....

OR to get minutes ... You need :

 IntToStr(Trunc(T)) ---> Trunc gets the integer value of T;

to get minutes :
 
 Frac(T) ---> gets the fractional part of T....

So if you got 2 values after comma --->

var seconds : string;
 
 seconds :=  Trunc(Frac(T)*100);

 Label3.Caption := IntToStr(Trunc(T)) + ':'+IntToStr(Trunc(Frac(T)*100));
0
 

Author Comment

by:hellfire052497
ID: 1341176
I must be missing something here,

ok here is the full code

Var
T : Double;
Begin
T := ((MPEG1.length /1000) /60);
Label3.Caption := IntToStr(Trunc(T))+':'+ IntToStr(Frac(T)*100); =>> here I get Incompatible types Integer and Real.
Any ideas
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341177
Yep ... Sorry... I corrected this in my last comment .... Forgot to tell you about this...

See Frac returns a real value.... So, we do this :

 If frac(T) is 0.32 ---> * 100 --> 32 ---> change to integer ---> Trunc(32)

This is the code --> IntToStr(Trunc(Frac(T)*100));

Regards, Zif.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1341178
Hi guys,
maybe this is all you need. Drop a TButton, a TLabel and a TEdit on a blank form and put this in the buttons OnClick handler. Now you can test the format of milliseconds to HH:MM:SS:FF.

procedure TForm1.Button1Click(Sender: TObject);
// frames in 25 fps (PAL), can also be 30 (NTSC)
const FPS = 30;
var H, M, S, F, MS : Integer;
begin
  MS := StrToInt(Edit1.Text);
  H := (((MS div 1000) div 60) div 60) mod 60;
  M := ((MS div 1000) div 60) mod 60;
  S := (MS div 1000) mod 60;
  F := ((MS mod 1000) * FPS) div 1000;
  Label1.Caption := Format('%.2d:%.2d:%.2d-%.2d', [H, M, S, F]);
end;

This function outputs *very* fast.

Slash/d003303
0
 

Author Comment

by:hellfire052497
ID: 1341179
it all works now, thanks for the answer
0
 
LVL 6

Expert Comment

by:Holger101497
ID: 1341180
he he... quite some time later (I got here through hellfire's profile and couldn't resist checking if the answer was the one I'd give...)

When reading this thread, I just wanted to say that IntToStr is rather slow and using and rounding lots of floats doesn't help either...

I guess the fastest/shortest way of doing this is:
Label1.Caption:=Format('%d:%.2d', [MPEG1.Length DIV 60000, MPEG1.Length DIV 1000 MOD 60]);
This doesn't display hours, but I think that's ok... few Mpegs will last more than one hour and even then, something like "87 minutes" is quite normal for movies...

P.S.: d003303, what does H := (((MS div 1000) div 60) div 60) mod 60; do? Are you a workaholic and your day "only" has 60 hours? *bg*
0
 

Author Comment

by:hellfire052497
ID: 1341181
Thanks i'll try it out.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1341182
Holger,
hehe, workaholic might be... maybe E-E-holic ;-)
0
 
LVL 6

Expert Comment

by:Holger101497
ID: 1341183
Oh, btw (just to keep this thread alive *lol*):
ZifNab's a workaholic as well... his minute has 100 seconds!
hellfire: If you used ZifNab's code, you should change IntToStr(Trunc(Frac(T)*100)); to IntToStr(Trunc(Frac(T)*60));... but that code is kinda weird/slow anyway!
0
 

Author Comment

by:hellfire052497
ID: 1341184
I noticed then, and I did changed it to 60.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341185
my, my am I under fire in this thread. Sorry for my mistakes people... As I said earlier, I'm not god and I just try to help as much as I can. See my state too, i'm not a programmer like some good people around here.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question