Solved

.length to minutes and seconds format

Posted on 1998-05-04
17
208 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now