XL 2010 Récupérer la DURÉE d'un fichier AUDIO ou VIDEO

Magic_Doctor

XLDnaute Barbatruc
Bonsoir

Tout est dans le titre.

Après avoir activé la référence "Microsoft Scripting RunTime", on peut récupérer nombre de donnés concernant un fichier (taille, type...), mais peut-on récupérer, quand il s'agit d'un fichier Audio ou Vidéo, la durée ?
Je pense que cela doit être possible puisque quand dans la bibliothèque on "clique-droit" sur ce type de fichier, à Propiétés/Détails on peut voir la durée du fichier. Du reste, on peut la mettre une fois pour toutes dans la configuration de la feuille.
 

Staple1600

XLDnaute Barbatruc
Bonsoir

Un exemple tout sorti du net anglophone
VB:
Function GetVideoDuration(fPName As String) As Long
'returns the duration in seconds of the specified video path+fPNamename
    Dim oFolder, ofPName
    With CreateObject("Shell.Application")
        Set oFolder = .Namespace(Left(fPName, InStrRev(fPName, "\") - 1))
        Set ofPName = oFolder.ParseName(Right(fPName, Len(fPName) - InStrRev(fPName, "\")))
        GetVideoDuration = GetVideoDuration = oFolder.GetDetailsOf(ofPName, 27)
    End With
End Function

Sub Test()
MsgBox GetVideoDuration("Path To Video Here\myVideo.mp4")  ''///returns a duration in seconds
End Sub
 

Staple1600

XLDnaute Barbatruc
Bonjour Job75

Non, je n'ai pas testé car je n'ai de fichier vidéo sur ce PC
(vu que l'installation de Windows 10 date de ce matin)
Et j'ai fait un bête copier/coller
Donc la version à tester
VB:
Function GetVideoDuration(fPName As String) As Long
'returns the duration in seconds of the specified video path+fPNamename
    Dim oFolder, ofPName
    With CreateObject("Shell.Application")
        Set oFolder = .Namespace(Left(fPName, InStrRev(fPName, "\") - 1))
        Set ofPName = oFolder.ParseName(Right(fPName, Len(fPName) - InStrRev(fPName, "\")))
        GetVideoDuration = oFolder.GetDetailsOf(ofPName, 27)
    End With
End Function

Sub Test()
MsgBox GetVideoDuration("Path To Video Here\myVideo.mp4")  ''///returns a duration in seconds
End Sub
 

TooFatBoy

XLDnaute Barbatruc
Bonsoir Marcel32

Oui désolé pour le copier/coller sans relecture.
No problemo, man ! 😎

Nb: non encore testé sur mon PC (je n'ai pas de MP3 non plus ;))
Euh... y a une VideoDuration pour un MP3 ??? 🤔


Pour info, #4 ne fonctionne pas chez oim : incompatibilité de gars qu'il dit le type (ou l'inverse)...

Ceci dit, je trouve que Windows est tellement peu fiable au niveau des informations des fichiers A/V que je ne comprends vraiment pourquoi l'utiliser pour tenter de les récupérer.
 

Staple1600

XLDnaute Barbatruc
Re

Test OK chez moi avec des MP3 (au nombre de 3)
Et de très bonne facture ;)

Voici le script Powershell
NB: Adapter le chemin de $Directory en conséquence.
VB:
$Directory = 'C:\Users\STAPLE\Music'
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {
    $Folder = $Shell.Namespace($_.DirectoryName)
    $File = $Folder.ParseName($_.Name)
    $Duration = $Folder.GetDetailsOf($File, 27)
    [PSCustomObject]@{
        Name = $_.Name
        Duration = $Duration
    }
} | Export-Csv -Path "./test.csv" -Delimiter ';' -NoTypeInformation
A copier dans le bloc-notes et enregistrer sous duréeMedia.ps1
(ailleurs que dans le dossier contenant les fichiers médias)
Puis clic-droit sur ce fichier *.ps1 et choisir Exécuter avec PowerShell
Ouvrir ensuite le *.csv obtenu avec Excel.
 

Magic_Doctor

XLDnaute Barbatruc
Bonsoir à tous,

J'avais trouvé la même fonction sur le même site anglophone.
J'ai fait des essais. Ça marche pour .mp3, .mp4, .avi, mais pas .mkv.

Pour le MessageBox, plutôt que d'avoir des secondes, j'ai fait la modification suivante :
VB:
Sub Tiempo()

    Dim TotalSecondes%
    
    TotalSecondes = GetVideoDuration("I:\FILMS\Clara película\Clara.2018.720p.WEBRip.x264-[YTS.AM].mp4")
    
    MsgBox Format(TotalSecondes / 86400, "hh:mm:ss")
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 201
Messages
2 086 166
Membres
103 151
dernier inscrit
nassim