Solved

.length to minutes and seconds format

Posted on 1998-05-04
17
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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