
---- ExifTool ----
ExifTool Version Number : 10.81
---- File ----
File Name : ANTOINE BONFANTI.avi
Directory : /Users/XXXX/Documents/films/A garder/ANTOINE BONFANTI
File Size : 1091 MB
File Modification Date/Time : 2014:02:11 21:32:28+01:00
File Access Date/Time : 2018:10:09 17:21:55+02:00
File Inode Change Date/Time : 2016:07:04 18:26:46+02:00
File Permissions : rwxrwxrwx
File Type : AVI
File Type Extension : avi
MIME Type : video/x-msvideo
BMP Version : Windows V3
Image Width : 768
Image Height : 576
Planes : 1
Bit Depth : 12
Compression : XVID
Image Length : 663552
Pixels Per Meter X : 0
Pixels Per Meter Y : 0
Num Colors : Use BitDepth
Num Important Colors : All
---- RIFF ----
Frame Rate : 25
Max Data Rate : 0 kB/s
Frame Count : 73896
Stream Count : 2
Stream Type : Video
Video Codec : XVID
Video Frame Rate : 25
Video Frame Count : 78909
Quality : 0
Sample Size : Variable
Audio Codec : U
Audio Sample Rate : 16000
Audio Sample Count : 50502426
Encoding : MP3
Num Channels : 2
Sample Rate : 44100
Avg Bytes Per Sec : 16000
Bits Per Sample : 0
Total Frame Count : 73896
Software : MEncoder SVN-r33883(20110719-gcc4.5.2)
---- Composite ----
Duration : 0:49:15
Image Size : 768x576
Megapixels : 0.442
property dossier : "" --déclaration de variables globales
property tsecg : 0
tell application "Finder"
    set chemin to choose folder with prompt "Sélectionnez le dossier contenant les fichiers à renommer"
    set tsecg to 0 -- initialise la variable total general des secondes
    set dossier to name of chemin -- récupère le nom du dossier choisi pour écrire la dernière ligne du log
    my inspecter(chemin) -- appel routine inspecter
end tell
on inspecter(un_dossier)
    tell application "Finder"
      
        set nomdos to name of un_dossier -- recupere le nom du dossier
        set FVideo to {"dv", "avi", "mp4", "mov", "vob", "divx", "mkv"} -- liste des extension à traiter dans FVideo_de_avi avec exftool
        -- mise a 0 des variables duree fichier
        set totsec to 0
        -- traitement des fichiers :
        set les_fichiers to files of un_dossier --réupère tous les fichiers du dossier
        repeat with chaque_fichier in les_fichiers -- répète avec chaque fichier du dossier en cour
            -- traitement d'un fichier
            tell application "Finder"
                set nom to name of chaque_fichier --récupère le nom du fichier
                set AppleScript's text item delimiters to {"."} -- defini le separateur
                set lextension to get last text item of nom -- recupère l'extension
                set duree to "" -- mise a blanc de la durée
                set lefichier to chaque_fichier as string --récupère le chemin du fichier au format chaine de caractères
              
              
                try --bloc si erreur
                    --traitement des fichiers MLV
                    if lextension is "mlv" then
                        set infoRec to info for file lefichier --recup infos du fichier par le finder
                        set lataille to size of infoRec -- recup la taille dans les infos
                        set lechemin to container of (path to me) as string -- recup chemin du dossier qui contient le fichier, donc le .py au format chaine chaine
                        set lechemin to lechemin & "duree_mlv.py" as string --fabrique le chemin du fichier .py
                        set fichier_mlv_info to quoted form of POSIX path of lechemin as string --chemin au format unix
                        set infos to do shell script "python " & fichier_mlv_info & " " & quoted form of POSIX path of lefichier as string --execute fichier .py
                        set AppleScript's text item delimiters to {"."} --défini le séparateur de chaine
                        set seconde to text item 1 of infos -- recup le temps en secondes partie avant la virgule
                        set seconde to seconde as number -- converti en nombre
                        if seconde = 0 then -- si le durée est inférieure à la seconde met la durée à 1
                            set seconde to 1
                        end if
                        -- fait le total du dossier en cours
                        set totsec to totsec + seconde as string
                        -- fait total general
                        set tsecg to tsecg + seconde
                        my ecriture(lefichier, seconde, lataille) --appel pour ecriture dans fichier texte du fichier
                    end if
                    -- Traitement des fichier video sauf MLV
                    if lextension is in FVideo then -- si fichier video de la liste FVideo
                        set infoRec to info for file lefichier --recup taille
                        set lataille to size of infoRec
                        set duree to do shell script "/usr/local/bin/exiftool -Duration " & quoted form of POSIX path of lefichier --récupère la durée au format texte exiftool
                      
                        -- traitement si valeur trouve avec exiftool
                        if duree is not "" then
                            set AppleScript's text item delimiters to {":"} --defini le séparateur
                          
                            -- Traitement de durée inférieur a 1 seconde ecris 0
                            if text item 2 of duree = " 0 s" then
                                set duree to "duration : 0.0"
                            end if                            -- Traitement des fichiers > 1 sec et <30 sec
                            set NB to count of text items of duree -- verifi sur duree est inférieur à 30 secondes (notation différente)
                            if NB = 2 then --recup duree en secondes
                                set lesseconde to text item 2 of duree
                                set AppleScript's text item delimiters to {"."}
                                set seconde to text item 1 of lesseconde
                                set minute to 0
                                set heure to 0
                                set AppleScript's text item delimiters to {":"}
                            else -- recup duree si supérieure à 30 sec (format hh : mn : sec )
                                set seconde to get last text item of duree as string -- récupère les secondes
                                set minute to text item 3 of duree as string -- récupère les minutes
                                set heure to text item 2 of duree as string --récupère les heures
                                set seconde to seconde + (minute * 60) + (heure * 3600)
                            end if
                        end if
                       
                        -- traitement des fichiers non lus par exiftool
                        if duree = "" then
                            set lessecondes to do shell script "mdls -name kMDItemDurationSeconds " & quoted form of POSIX path of lefichier --récupère la durée en secondes format mdls
                            set duree to word 3 of lessecondes --extrait la durée en secondes dans la variable les secondes
                            set AppleScript's text item delimiters to {"."}
                            set seconde to text item 1 of duree
                        end if
                       
                       
                        -- fait le total du dossier en cours
                        set totsec to totsec + seconde as string
                        -- fait total general
                        set tsecg to tsecg + seconde
                        my ecriture(lefichier, seconde, lataille) --appel pour ecriture dans fichier texte du fichier
                       
                    end if
                   
                on error the errorMessage number the errorNumber --recup message si erreur
                    display dialog "erreur: " & errorMessage & " sur le  fichier " & lefichier --affiche message si erreur
                end try
            end tell
        end repeat -- fichier suivant
        if totsec > 0 then
            my total(nomdos, totsec, un_dossier) --appel pour ecriture dans fichier texte total dossier si supérieur à 0
        end if
        -- traitement des dossiers :
        set les_dossiers to folders of un_dossier
        repeat with chaque_dossier in les_dossiers -- dossier suivant
            my inspecter(chaque_dossier) -- appel pour dossier suivant
        end repeat
    end tell
end inspecter -- fin routine inspecteron ecriture(nom, secondes, lataille)
    -- calcule le temps total heure-minutes-secondes et Ecrit dans la fichier texte le nom + tabulation  + la durée
    set heure to secondes div 3600 as string
    set minute to secondes mod 3600 div 60 as string
    set seconde to secondes mod 60 as string
   
    set lataille to (round lataille / 10000 rounding down) / 100 -- la taille en Mo et 2 chiffres representatifs apres la virgule
   
    set lelog to open for access ((path to desktop folder as text) & dossier & ".txt") as text with write permission -- ouvre le fichier log et ecritles valeur séparé par une tabulation
    write nom & (ASCII character 9) & heure & " h " & minute & " mn " & seconde & " sec " & (ASCII character 9) & lataille & " Mo" & return to lelog starting at eof
    close access lelog -- ferme le fichier log
end ecriture
on total(nomdos, totsec, un_dossier)
    -- idem écriture pour les dossiers
    set un_dossier to un_dossier as string
    set infoRec to info for file un_dossier -- recupère la taille du dossier en octets
    set lataille to size of infoRec
   
    set lataille to (round lataille / 10000 rounding down) / 100 -- la taille en Mo et 2 chiffres representatifs apres la virgule
   
   
    -- calcule le temps total heure-minutes-secondes et ecrit dans fichier texte total dossier+dans fichierprovisoire listh+listmn+listsec
    set heure to totsec div 3600 as string
    set minute to totsec mod 3600 div 60 as string
    set seconde to totsec mod 60 as string
    set lelog to open for access ((path to desktop folder as text) & dossier & ".txt") as text with write permission
    write " total dossier " & nomdos & (ASCII character 9) & heure & " h " & minute & " mn " & seconde & " sec" & (ASCII character 9) & lataille & " Mo" & (ASCII character 13) & return to lelog starting at eof
    close access lelog
end total
tell application "Finder"
    -- calcule le temps total heure-minutes-secondes
    set heure to tsecg div 3600 as string
    set minute to tsecg mod 3600 div 60 as string
    set seconde to tsecg mod 60 as string
    -- recup de la taille du dossier initial choisi
    set chemin to chemin as string
    set infoRec to info for file chemin
    set lataille to size of infoRec
   
    set lataille to (round lataille / 10000 rounding down) / 100
   
    --Ecrit dans la fichier texte le nom + tabulation  + la durée
    set lelog to open for access ((path to desktop folder as text) & dossier & ".txt") as text with write permission
    write " total général du dossier " & dossier & (ASCII character 9) & heure & " h " & minute & " mn " & seconde & " sec" & (ASCII character 9) & lataille & " Mo" & return to lelog starting at eof
    close access lelog
end tell
tell application "Finder"
    (display dialog ("waouou ... Ca y est c'est fait !") buttons {"Salut !"})
end tell 
	