Page 1 sur 1

Recover duration mp3 file

Publié : jeu. 09/mars/2023 11:43
par jak64
Hello all,
I am writing a program in Purebasic which displays, in a ListIconGadget, the list of mp3 files that I have in my folders.
I want to display the duration of each song to the right of the title.
Looking on the forum, I found codes but, in these codes, we open each file to find the duration.
With several hundred mp3 files, the execution time is very, very long.
Isn't there another way to recover the duration of the mp3 without having to open the file?

For example, in the Windows explorer, by right-clicking on the file, then properties, then detail tab, the duration of the mp3 is displayed, without having to open the file.

How to recover this duration without opening the file?

Thank you.

falsam : Traduction avec Deepl. (Bien mieux que google translate à mon avis)

Bonjour à tous,
J'écris un programme en Purebasic qui affiche, dans un ListIconGadget, la liste des fichiers mp3 que j'ai dans mes dossiers.
Je souhaite afficher la durée de chaque chanson à droite du titre.
En cherchant sur le forum, j'ai trouvé des codes mais, dans ces codes, on ouvre chaque fichier pour trouver la durée.
Avec plusieurs centaines de fichiers mp3, le temps d'exécution est très, très long.
N'y a-t-il pas un autre moyen de récupérer la durée du mp3 sans devoir ouvrir le fichier ?

Par exemple, dans l'explorateur Windows, en faisant un clic droit sur le fichier, puis propriétés, puis onglet détail, la durée du mp3 est affichée, sans devoir ouvrir le fichier.

Comment récupérer cette durée sans ouvrir le fichier ?

Je vous remercie.

Re: Recover duration mp3 file

Publié : jeu. 09/mars/2023 12:22
par SPH
Please : speak in french. (google traduc)

Thx :wink:

Re: Recover duration mp3 file

Publié : jeu. 09/mars/2023 15:11
par falsam
SPH a écrit : jeu. 09/mars/2023 12:22 Please : speak in french. (google traduc)
Voila qui est fait :wink:

Re: Recover duration mp3 file

Publié : jeu. 09/mars/2023 16:16
par falsam
Testé avec PB 6.0 - Une solution avec FMODEX
Télécharger l'include et les dll 32 et 64 bits hébergés sur la plateforme collaborative GitHub
:arrow: GitHub https://github.com/pbcodex/FMODEX-MIN-UNICODE

:arrow: LienDirect https://github.com/pbcodex/FMODEX-MIN-U ... master.zip

Le code permet de lire le durée d'un fichier mp3, ogg ou flac.
Il retourne le temps en milliseconde qui sera converti en wk day hr min sec avec la procédure MsToTime(NbMs) inclue dans ce code.

Code : Tout sélectionner

EnableExplicit

IncludeFile "fmodex-min.pbi"

Global File.s, FmodSystem.i, Channel.i, Sound.i, lenght.i

Declare.s MsToTime(NbMs)

;Declare FMOD System
FMOD_System_Create(@fmodsystem)

;Init FMOD System
FMOD_System_Init(FmodSystem, 32, #FMOD_INIT_NORMAL, 0)

; Sélectionner un son
File = OpenFileRequester("Selectionner un fichier mp3","","Musique|*.mp3;*.wav;*.ogg;*.flac",0)
If File <> ""
  
  ; Création du stream de lecture 
  FMOD_System_CreateStream(FmodSystem, Ascii(File), #FMOD_SOFTWARE, 0, @sound)
  
  ; Lecture de la lecture du son. Retourne la durée en milliseconde
  FMOD_Sound_GetLength(sound, @lenght, #FMOD_TIMEUNIT_MS)
  
  Debug MsToTime(lenght)
EndIf

Procedure.s MsToTime(NbMs)
  Protected weeks, days, hours, minutes, seconds
  Protected divisor, remainder
  Protected duration$ = ""
  Protected NbSeconds = NbMs/1000
  
  divisor = 7 * 24 * 60 * 60 ; seconds in a week
  weeks = NbSeconds / divisor
  remainder = NbSeconds % divisor
  divisor / 7 ; seconds in a day
  days = remainder / divisor
  remainder % divisor
  divisor / 24 ; seconds in an hour
  hours = remainder / divisor
  remainder % divisor
  divisor / 60 ; seconds in a minute
  minutes = remainder / divisor
  seconds = remainder % divisor
  
  If weeks > 0
    duration$ + Str(weeks) + " wk, "
  EndIf
  
  If days > 0
    duration$ + Str(days) + " d, "
  EndIf
  
  If hours > 0
    duration$ + Str(hours) + " hr, "
  EndIf
  
  If minutes > 0
    duration$ + Str(minutes) + " min, "
  EndIf
  
  If seconds > 0
    duration$ + Str(seconds) + " sec"
  EndIf
  
  If Right(duration$, 2) = ", "
    duration$ = Mid(duration$, 0, Len(duration$) - 2)
  EndIf
  
  ProcedureReturn duration$
EndProcedure