Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 13/09/2009, à 16:35

Aldian

lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

Je ne sais pas vous, mais moi j'ai tout un tas de fichiers multimédias dans tout un tas de formats différents et de répertoires différents, et comme il y a du mouvement sans arrêt sur mon ordinateur, je ne sais jamais lesquels ont déjà été convertis en mp3, et lesquels ne l'ont pas encore été.

Alors je ne sais pas si je suis allé au plus simple, mais j'ai fait un script et un programme java qui me dépannent bien.

I - Les codes sources

Le script bash (à compléter selon vos besoins):

#!/bin/bash
find -name "*.flv" > inventaire.txt
find -name "*.mp4" >> inventaire.txt
find -name "*.mp3" >> inventaire.txt

Le code java à enregistrer dans le fichier "FindNonConvertedFiles.java"

/*   FindNonConvertedFiles: This program retrieves all the flv and mp4 
 *   files in the arborescence that have not been converted to mp3 yet.
 *  
 *   Copyright (C) 2009  Aldian
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

public class FindNonConvertedFiles {
    private static boolean DEBUG = false ;
    private static Hashtable<String, String> table =null;
    //private static ArrayList<String> list = null;
    private static ArrayList<String> mp3List = null;
    private static ArrayList<String> nonMp3List = null;
    private static ArrayList<String> nonConvertedList = null;
    private static ArrayList<String> result = null;
    
    public static void main(String args[]) throws Exception {
        printLicense();
        System.out.println("Voici la liste des fichiers non encore convertis. Cette liste a été stockée dans le fichier non_converted_flv_and_mp4.txt");
        //generateInventaire();
        fillHashtableAndLists();
        fillNonConvertedList();
        generateResult();
        outputResult();
    }
    
    public static void generateInventaire() throws Exception{
        Runtime runtime = Runtime.getRuntime();
        boolean debug =  true;
        
        String command0 = "find -name \"*.flv\" > inventaire.txt";
        debug(command0,debug);
        Process p0 = runtime.exec(command0);
        p0.waitFor();
        
        String command1 = "find -name \"*.mp4\" >> inventaire.txt";
        debug(command1,debug);
        Process p1 = runtime.exec(command1);
        p1.waitFor();
        
        String command2 = "find -name \"*.mp3\" >> inventaire.txt";
        debug(command2,debug);
        Process p2 = runtime.exec(command2);
        p2.waitFor();
    }
    
    /**
     * maps name and path to file
     * fill list of current mp3s
     * fill list of current non mp3 files
     * @throws Exception
     */
    public static void fillHashtableAndLists() throws Exception{
        table = new Hashtable<String, String>();
        mp3List = new ArrayList<String>();
        nonMp3List = new ArrayList<String>();
        BufferedReader in = new BufferedReader(new FileReader("inventaire.txt"));
        
        String line = null, name = null;
        String [] split = null;
        while ((line = in.readLine()) != null) {
            name = retrieveName (line);
            // checks doubles
            if (!table.containsKey(name)) {
                debug(name);
                // maps name and path to file
                table.put(name, line);
                split = splitNameAndExtension(name);
                // add to correct list 
                if(split[1].equals("mp3")){
                    mp3List.add(split[0]); 
                } else {
                    nonMp3List.add(name);
                }
            }
        }
        in.close();
    }
    
    /**
     * Extract filename from filepath
     * @param line
     * @return
     */
    public static String retrieveName(String line){
        int index = line.indexOf("/");
        String name = line;
        debug(name);
        debug(""+index);
        while (index !=-1){
            name=name.substring((index+1));
            index = name.indexOf("/");
            debug(name);
            debug(""+index);
        }
        return name;
    }
    
    /**
     * for example with an argument "toto.mp3", will return {"toto","mp3"}
     * @param line
     * @return
     */
    public static String [] splitNameAndExtension(String line){
        int index = line.lastIndexOf('.');
        String [] split = new String [2];
        split[0] = line.substring(0, index-1);
        split[1] = line.substring(index+1);

        return split;
    }
    
    /**
     * Print debug info if required
     * @param s
     */
    public static void debug(String s){
        debug(s,DEBUG);
    }
    public static void debug(String s, boolean debug){
        if(debug)
            System.out.println(s);
    }
    
    /**
     * retrieve files from nonMp3List that are not in mp3List
     */
    public static void fillNonConvertedList(){
        nonConvertedList = new ArrayList<String>();
        String fullname = null, truncatedName = null;
                
        for(int i=0;i<nonMp3List.size();i++){
            fullname = nonMp3List.get(i);
            truncatedName = splitNameAndExtension(fullname)[0];
            if(!mp3List.contains(truncatedName)){
                nonConvertedList.add(fullname);
            }
            
        }
        
    }
    
    /**
     * generate the final list and sort it by file path and alphabetic order
     */
    public static void generateResult(){
        result =  new ArrayList<String>();
        for(int i=0;i<nonConvertedList.size();i++){
            result.add(table.get(nonConvertedList.get(i)));
        }
        Collections.sort(result);
    }
    
    /**
     * prints the final list to file
     * @throws Exception
     */
    public static void outputResult() throws Exception{
        BufferedWriter out= new BufferedWriter(new FileWriter("non_converted_flv_and_mp4.txt"));
        String nonConvertedFileName = null;
        
        for(int i=0;i<result.size();i++){
            nonConvertedFileName = result.get(i);
            System.out.println(nonConvertedFileName);
            out.write(nonConvertedFileName+"\n");
        }
        out.close();
    }
    
    public static void printLicense(){
        System.out.println("FindNonConvertedFiles  Copyright (C) 2009 - 2012  Aldian");
        System.out.println("This program comes with ABSOLUTELY NO WARRANTY; for details visit site http://www.gnu.org/licenses/gpl.html.");
        System.out.println("This is free software, and you are welcome to redistribute it");
        System.out.println("under certain conditions; visit site http://www.gnu.org/licenses/gpl.html for details.");
        System.out.println("");
    }

}

II - Utilisation :

1) compilation du fichier java. Vous avez besoin du paquet gcj.
javac FindNonConvertedFiles.java

2) copiez le fichier FindNonConvertedFiles.class et le script bash dans le dossier racine qui contient tous vos dossiers musicaux.

3) exécutez le script bash

4) exécutez le programme java par la commande
java FindNonConvertedFiles

Dernière modification par Aldian (Le 04/10/2012, à 11:32)

Hors ligne

#2 Le 14/09/2009, à 12:30

Hawkmoon

Re : lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

du java pour ça ?
Quelle lourdeur... perl, bash, ou python seront plus légers.


Tagazok à toi, mon frère !

Hors ligne

#3 Le 27/09/2009, à 13:53

Aldian

Re : lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

Oui, sauf que je suis spécialiste du java... Pas du perl, du bash ou du python. En outre, le problème n'est pas si simple qu'il y parait, ça nécessite pas mal de tris de tableaux et autres joyeusetés afférentes wink.

Hors ligne

#4 Le 27/09/2009, à 14:39

k-o-x

Re : lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

Houlalala mais tu vas bien chercher loin...

#!/bin/bash

EXTS="flv mp4"
echo "" > /tmp/liste

for ext in $EXTS; do
    find -name "*.$ext" | while read fname; do
        MP3=`basename $fname | sed -r 's:$ext$:mp3:'`
        [ `find -name "$MP3" | wc -l` -eq 0 ] && basename $fname >> /tmp/liste
    done
done

cat /tmp/liste | uniq | sort > pas_converti.txt

Dernière modification par k-o-x (Le 27/09/2009, à 15:06)

Hors ligne

#5 Le 01/10/2009, à 21:06

Aldian

Re : lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

Ca m'a l'air intéressant, mais je n'aurais jamais trouvé tout seul. Je vais tester à l'occasion. Merci en tout cas smile

Hors ligne

#6 Le 02/10/2009, à 01:09

twocats

Re : lister les fichiers multimédia dont vous n'avez pas d'équivalent mp3

MP3="${fname%.*}".mp3

C'est plus simple et plus besoin ni de basename ni de sed.
Les guillemets sont indispensable pour gérer les noms avec des espaces.


La réponse est 42

Hors ligne