Dorian Fevrier's blog - Mot-clé - imageJe m’appelle FEVRIER Dorian, je suis infographiste 3D passionné par mon métier, l’informatique en général, l’internet, la programmation et l’évolution de tout ce petit monde. Vous trouverez sur ce blog des tutoriaux, mes coups de cœurs, avis, etc.2024-01-02T23:48:05+01:00FEVRIER Dorianurn:md5:695d9c73474c33ce3dab043823509c4bDotclearMediaInfo: Lire les informations des fichiers vidéo et audio en ligne de commande. (Part 2)urn:md5:793201b93c64f690294afcb0ee05c9bf2009-11-29T18:59:00+01:002013-07-26T22:36:20+02:00NarannScript et codeaudiofichiersfrimageinformationligne de commandeliremediainfopythontutovideo<p><img src="https://www.fevrierdorian.com/blog/public/billets/mediainfo/python_fouilleQuicktime150.png" alt="python_fouilleQuicktime150.png" style="float:left; margin: 0 1em 1em 0;" title="python_fouilleQuicktime150.png, nov. 2009" height="150" width="150" />Lors du <a href="https://www.fevrierdorian.com/blog/post/2009/10/20/MediaInfo%3A-Lire-les-informations-des-fichiers-vid%C3%A9o-et-audio-en-ligne-de-commande.">billet précédent</a> nous avons vu comment extraire une information (le nombre d'image) d'un fichier Quicktime en ligne de commande. Je vous propose ici la suite de ce tutorial en utilisant Python afin d'étendre cette possibilité à tout les fichiers d'un dossier.</p>
<p>MediaInfo reconnaissant un bon paquet de format, à la fin de ce tuto, vous aurez la possibilité d'étendre ce script suivant votre usage.</p> <h5>Ce qu'on veux faire <a name="Ce_quon_veux_faire"></a></h5>
<p>L'idée est de récupérer une liste de ce type:</p>
<p>SEQ006PL055.mov -> 134 images<br />
SEQ006PL056.mov -> 97 images<br />
SEQ006PL057.mov -> 63 images<br />
etc...</p>
<p>Ladite liste étant les fichiers Quicktime d'un dossier...</p>
<h5>Et maintenant, le python <a name="et_mnt_le_python"></a></h5>
<pre class="python python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">def</span> frameCount<span style="color: black;">(</span>path<span style="color: black;">)</span>:
<span style="color: #ff7700;font-weight:bold;">for</span> fileName <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">(</span>path<span style="color: black;">)</span>:
filePath = path+<span style="color: #483d8b;">"/"</span>+fileName <span style="color: #808080; font-style: italic;">#"C:/myQuicktimeFiles" + "/" + "SEQ006PL055.mov"</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">(</span><span style="color: #dc143c;">os</span>.<span style="color: black;">access</span><span style="color: black;">(</span>filePath, <span style="color: #dc143c;">os</span>.<span style="color: black;">F_OK</span><span style="color: black;">)</span><span style="color: black;">)</span>: <span style="color: #808080; font-style: italic;">#verifie que le chemin filePath est bien un fichier et qu'on peut y acceder</span>
handle = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">(</span><span style="color: #483d8b;">"C:/MediaInfo/MediaInfo.exe --Inform=Video;%FrameCount% "</span>+filePath<span style="color: black;">)</span>.<span style="color: black;">read</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#recupere la sortie du programme</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span>fileName+<span style="color: #483d8b;">" -> "</span>+<span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">(</span><span style="color: #483d8b;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #483d8b;">""</span>, handle<span style="color: black;">)</span>+<span style="color: #483d8b;">" images"</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;">#affiche le resultat</span>
frameCount<span style="color: black;">(</span><span style="color: #483d8b;">"C:/myQuicktimeFiles"</span><span style="color: black;">)</span></pre>
<p>Dans un premier temps, il faut lister les fichiers contenus dans le dossier spécifié. Pour ça, nous utilisons la methode os.listdir():</p>
<pre class="python python"><span style="color: #66cc66;">>>></span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">(</span><span style="color: #483d8b;">"C:/myQuicktimeFiles"</span><span style="color: black;">)</span>
<span style="color: black;">[</span><span style="color: #483d8b;">'SEQ006PL055.mov'</span>, <span style="color: #483d8b;">'SEQ006PL056.mov'</span>, <span style="color: #483d8b;">'SEQ006PL057.mov'</span><span style="color: black;">]</span></pre>
<p>Pour exécuter une commande système en python on peut utiliser plusieurs méthodes. La première que j'ai utilisé fut:</p>
<pre class="python python"><span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">(</span><span style="color: #483d8b;">"ma commande"</span><span style="color: black;">)</span></pre>
<p>Mais le problème est que cette commande renvois la valeur de retour du programme (souvent "0" ce qui signifie que tout c'est bien passé) mais elle ne renvois pas ce qu'écrit le programme et c'est précisément ça qui nous intéresse.</p>
<p>Après quelques recherche j'ai trouvé exactement la commande qui m'intéressait:</p>
<pre class="python python"><span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">(</span><span style="color: #483d8b;">"ma commande"</span><span style="color: black;">)</span></pre>
<p>Cette commande renvois donc tout ce qu'a sortie la commande exécutée dans un "tunnel de communication"... J'ai un peu galéré pour savoir comment "réinterpréter" tout ça mais voici la commande que j'ai trouvé <a href="http://techkr.blogspot.com/2007/02/python-how-to-get-output-from-ossystem.html" hreflang="en">ici</a>:</p>
<pre class="python python"><span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">(</span><span style="color: #483d8b;">"ma commande"</span><span style="color: black;">)</span>.<span style="color: black;">read</span><span style="color: black;">(</span><span style="color: black;">)</span></pre>
<p>Avec ça, nous avons une chaine de caractère toute propre comme si elle sortait de notre console! :sourit:</p>
<p>Au niveau du print j'ai du faire une bidouille pour éviter qu'il ne saute deux ligne à chaque ligne:</p>
<pre class="python python"><span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">(</span><span style="color: #483d8b;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #483d8b;">""</span>, handle<span style="color: black;">)</span></pre>
<p>Cette ligne "substitue" les caractère de retour à la ligne "\n" par "rien" dans la chaine de caractère "handle"</p>
<p>Et c'est tout! (Enfin je crois... ^^ )</p>
<p>Ce deuxième tuto n'était qu'une formalité comparé au <a href="https://www.fevrierdorian.com/blog/post/2009/10/20/MediaInfo%3A-Lire-les-informations-des-fichiers-vid%C3%A9o-et-audio-en-ligne-de-commande.">précédent</a> mais il était dans la continuité.</p>
<p>N'hésitez pas à poster un commentaire si vous rencontrez des difficultés lié à l'utilisation de ce tutorial.</p>
<p>A bientôt!</p>