Solved

.length to minutes and seconds format

Posted on 1998-05-04
17
207 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

744 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

13 Experts available now in Live!

Get 1:1 Help Now