Getting MP3 file duration using VBS

Share your advanced PureBasic knowledge/code with the community.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

Hi RASHAD,

It's very complicated the mp3 :D

Thank you for all your help
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

Hi RASHAD,

Indeed, I checked with the Winamp player, it also displays the wrong duration (19 minutes instead of 4 minutes!)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting MP3 file duration using VBS

Post by RASHAD »

Hi Jack
I checked ffmpeg,MCI,Wmplayer.OCX and VBS and I suggest stick with VBS it's the fastest and after all it's Microsoft
Happy landing
Egypt my love
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

Hello RASHAD,
OK for vbs but it should be able to read all my directories where there is music and this in a single vbs...
I don't know vbs, I don't know if it's feasible to scan a directory and all its subdirectories to calculate the duration of each music...
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: Getting MP3 file duration using VBS

Post by ZX80 »

Hi, jak64.

Try this:

mp3.vbs

Code: Select all

Dim objArgs, source, mask
Dim objShellApp, objFSO, objFolder
Dim oFolder, oFolderItems

Const LENGTH = 27

Set objArgs = Wscript.Arguments
If objArgs.Count <> 2 Then WScript.Quit -1
source   = objArgs(0)
mask     = objArgs(1)

Set objShellApp = CreateObject("Shell.Application")
Set objFSO      = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder   = objFSO.GetFolder(source)

If Not objFSO.FolderExists(source) Then WScript.Quit -2

ScanSubFolders objFolder

Set objArgs      = Nothing
Set objShellApp  = Nothing
Set objFSO       = Nothing
Set objFolder    = Nothing

WScript.Quit 0

'=============================================================================
Sub ScanSubFolders(objFolder)
  Dim objSubFolder, strPath
    
  strPath          = objFolder.Path
  Set oFolder      = objShellApp.NameSpace(strPath)
  Set oFolderItems = oFolder.Items
  oFolderItems.Filter 64 + 128, mask
    
  If oFolderItems.Count > 0 Then
    For Each objItem In oFolderItems
      WScript.Echo strPath & Chr(9) & objItem.Name & Chr(9) & oFolder.GetDetailsOf(objItem,LENGTH)
    Next
  End If  
    
  Set oFolder      = Nothing
  Set oFolderItems = Nothing

  On Error Resume Next
  For Each objSubFolder In objFolder.SubFolders
    If Err.Number = 0 Then
      On Error Goto 0
      ScanSubFolders objSubFolder
    Else
      Err.Clear
      On Error Goto 0
      WScript.Echo "Can't enumerate subfolders for folder [" & objFolder.Path & "]."
    End If
  Next
End Sub

mp3run.pb

Code: Select all

If InitMovie() = 0
  MessageRequester("Error", "Can't initialize movie playback !", 0)
  End
EndIf

Procedure IsMouseOver(hWnd) 
  GetWindowRect_(hWnd,r.RECT) 
  GetCursorPos_(p.POINT) 
  Result = PtInRect_(r,p\y << 32 + p\x) 
  ProcedureReturn Result 
EndProcedure 

LoadFont(0,"Comic Sans ms",14)
hWnd = OpenWindow(0, 0, 0, 800, 600, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If hWnd
  LIG_Wnd = ListIconGadget(0, 10, 10, 780, 500, "", 0, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(0, 1, "MP3 File Name", 620)
  AddGadgetColumn(0, 2, "Duration", 155)
  ButtonGadget(1,10,565,80,24,"RUN")
  SetGadgetFont(0,FontID(0))
  
  InitialPath$ = "E:\Music"
  InitialMask$ = "*.mp3"
  If FileSize(InitialPath$) <> -2
    InitialPath$ = "C:\"
  EndIf
  vbs$ = "mp3.vbs"
  out$ = "Text.txt"
  Volume = 100
    
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_LBUTTONDBLCLK
        If IsMouseOver(LIG_Wnd) <> 0
          CountItems = CountGadgetItems(0) -1
          If CountItems <> -1
            For a = 0 To CountItems
              If GetGadgetItemState(0, a) & #PB_ListIcon_Selected
                If GetGadgetItemText(0, a, 2) <> ""
                  SoundFileName$ = GetGadgetItemText(0, a, 0)
                  If LoadMovie(0, SoundFileName$)
                    PlayMovie(0, hWnd)
                    MovieAudio(0, Volume, 0)
                  Else
                    MessageRequester("Error", "Can't load the sound.", 0)
                  EndIf
                EndIf
                Break
              EndIf
            Next
          EndIf
        EndIf

      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Path$ = PathRequester("Specify a folder to scan", InitialPath$)
            If Path$ <> ""
              InitialPath$ = Path$
              ClearGadgetItems(0)
              
              RunProgram("cmd "," /c cscript //nologo "+vbs$+" "+#DQUOTE$+Path$+#DQUOTE$+" "+InitialMask$+" > "+out$,"",#PB_Program_Open|#PB_Program_Wait|#PB_Program_Hide)
              If ReadFile(0, out$)
                FormatFile = ReadStringFormat(0)
                Path$ = ""

                While Eof(0) = 0 
                  text$ = ReadString(0, FormatFile)
                  fullpath$ = StringField(text$, 1, Chr(9))
                  filename$ = StringField(text$, 2, Chr(9))
                  duration$ = StringField(text$, 3, Chr(9))
                  
                  If fullpath$ <> Path$
                    Path$ = fullpath$
                    AddGadgetItem(0, -1, "")
                    AddGadgetItem(0, -1, Chr(10) + Path$)
                    AddGadgetItem(0, -1, Chr(10) + RSet(Chr(175), Len(Path$), Chr(175)))
                  EndIf
                  AddGadgetItem(0, -1, fullpath$ + "\" + filename$ + Chr(10) + filename$ + Chr(10) + duration$)
                Wend
                CloseFile(0)
                DeleteFile(out$,#PB_FileSystem_Force)
              Else
                MessageRequester("Information","Couldn't open the output-file!")
              EndIf
            EndIf
        EndSelect
    EndSelect
  Until Quit = 1
EndIf

Unfortunately, I don't have time for that anymore. Maybe someone else will improve this.
The problem: I couldn't get the utf-8 encoded script to work properly. As a result, some characters in the track names may be distorted. :(

Good luck !


P.S. Oh yeah... I forgot to say that you have to double click on the selected track to play it. I hope that this is already clear from the code.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Getting MP3 file duration using VBS

Post by Bisonte »

Do you have looked at : viewtopic.php?t=81046 ?
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: Getting MP3 file duration using VBS

Post by ZX80 »

Good time, Bisonte.

Yes, I've seen similar code before. I can tell you even more... This code is somewhere on my hard drive.

But...

Mr. RASHAD strongly recommended using vbs. This is the reason why I slightly changed his two codes above. Just like jak64 asked. Yes, I know it's slow... there's a lot of string manipulation, writing to a file, reading line by line, etc. But it was just a sketch to show jak64 that things can be made simpler. This is the first step or just a push for the further development of his program. I just saw his diligence and I liked it. I couldn't get past.

There are a lot of things that I don't know myself, and I'm still learning from experienced programmers. Thanks to everyone who shares their experience.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Getting MP3 file duration using VBS

Post by ricardo »

Use FFMPEG,

Just run

ffmpeg -i yourfile.mp3 and you get all info
ARGENTINA WORLD CHAMPION
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

Hello Ricardo,

I don't know how to use this command.

I executed :
ffmpeg -i "D:\Purebasic\MY MUSIC\Songs\2 Unlimited\2 Unlimited - Jump for joy.mp3"

in dos command but I have an error message
User avatar
GG
Enthusiast
Enthusiast
Posts: 266
Joined: Tue Jul 26, 2005 12:02 pm
Location: Lieusaint (77), France

Re: Getting MP3 file duration using VBS

Post by GG »

jak64, please download here :

https://ffmpeg.org/
Purebasic 6.12 64 bits - Windows 11 Pro 64 bits 23H2
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

Hello GG,
Thank you
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Getting MP3 file duration using VBS

Post by ricardo »

jak64 wrote: Wed Apr 05, 2023 10:40 am Hello Ricardo,

I don't know how to use this command.

I executed :
ffmpeg -i "D:\Purebasic\MY MUSIC\Songs\2 Unlimited\2 Unlimited - Jump for joy.mp3"

in dos command but I have an error message
Just run

ffmpeg -i yourfile.mp3

-i is for telling that its the input file. And thats it, you will get te info and you can read is as any console from within PB.

Yes, i forgot to explain tht you need to download this command line tools that is GREAT.

https://ffmpeg.org
ARGENTINA WORLD CHAMPION
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Getting MP3 file duration using VBS

Post by jak64 »

OK Ricardo,
Thank you
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Getting MP3 file duration using VBS

Post by ricardo »

:D :D
ARGENTINA WORLD CHAMPION
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Getting MP3 file duration using VBS

Post by Kwai chang caine »

Thanks RASHAD and ZX80 for all your precious codes all worked here 8)
ImageThe happiness is a road...
Not a destination
Post Reply