Matlab: Lieder sortieren

(English) Dieser Beitrag beschreibt ein Matlab Skript in dem Lieder aus einem Ordner in einen anderen Ordner sortiert nach Interpreten gespeichert wird.

Funktion:

Zu erst wird der Pfad der zu sortierenden Dateien und der Pfad in welche diese sortiert werden ausgewählt. Da manchmal im Ordner versteckte Daten wie ".", ".." oder "Desktop.ini" sind werden diese herausgenommen. Jetzt wird der Dateien Name jeder Musikdatei aufgespalten in Interpret und in Liedname und unnötigt zusätze wie "(Official Video)", "[pleer.com]" weggeschnitten.
Jetzt wird für jedes Lied nachgeschaut, ob ein Ordner des Interpreten schon in dem zu speichernden Ordner ist. Wenn es keinen gibt wird einer erstellt mit dem Namen der Interpreten. Dann wird in diesem Ordner überprüft, ob das Lied bereits existiert und wenn dies nicht der Fall ist wird das Lied in den Ordner kopiert. All erfolgreich kopierten Liednamen werden in der Konsole ausgegeben.
Dabei kann am Anfang ausgewählt werden, ob der Name als "Interpret - Liedname.mp3" und anderenfalls als "Liedname.mp3" gespeichert werden soll und es kann ausgewählt werden, ob die Lieder in Ordner sortiert werden oder einfach hineinkopiert.

Quellcode:

% This function is copying all not already existing files(song) to the
% chosen folder from the chosen folder. It is also generating new
% directories if the folder does not already exist. Also you can adjust,
% which phrases the algorithm should delete out of the original name like
% "(original lyric)" or some kind of this not wanted informations in the
% title


clear;
clc;

path_mat = pwd;
start_path = pwd;
% set folders
path_new = uigetdir(start_path,'Set the folder with the new songs');
music_path = uigetdir(start_path,'Set the folder where the songs should be saved');

% save in different folders
choice = questdlg('Save in folders sorted by interpret?', ...
    'Option 1', ...
    'Yes','No','No');
switch choice
    case 'Yes'
        save_direct = 0;
    case 'No'
        save_direct = 1;
end
% save as "interpret - name" with save_long = 1
choice = questdlg('Save file as 1. "interpret - title.mp3" or 2. "title.mp3"', ...
    'Option 2','1','2', '');
switch choice
    case '1'
        save_long = 1;
    case '2'
        save_long = 0;
end

% load the folders and songs
list_new = dir(path_new);
list_save = dir(music_path);
% filter all not music files and right everything in list
k=1;
for i=1:1:length(list_new)
    if list_new(i,1).bytes > 1000
        list(k,1)=list_new(i,1);
        k = k+1;
    end
end
% get all folder names of music_path
for i=1:1:length(list_save)
    music_folder{i} = strtrim(list_save(i,1).name);
end

% get the names
for i=1:1:length(list)
    full_name{i}=list(i,1).name;
end
names = strjoin(full_name);

% split the names and write the names in list_name
split_name = strsplit(names,{'.mp3','.MP3','.wma','.WMA','[pleer.net]',...
    '[pleer.com]','(Official Video)','(Official Audio)','(Lyric Video)',...
    'HQ'});
empty_name = cellfun('isempty',split_name);
for i = 1:1:length(empty_name(empty_name==0))
    list_name{i} = strtrim(split_name{i});
end
clear list_new names;


%get the songs and interprets seperately
fprintf('Added songs are:\n');
for i=1:1:length(list_name)
    interpret_song = strsplit(list_name{i},{'-'});
    interpret{i} = strtrim(interpret_song{1});
    songtitle{i} = strtrim(interpret_song{2});
    % adjust how the name is saved
    if save_long == 1
        song_name = list_name(1,i);
    else
        song_name = songtitle{i};
    end
    % define the paths of everything
    folder_path = strcat(music_path, {'\'}, interpret(1,i));
    save_name = strcat(folder_path, {'\'}, song_name, {'.mp3'});
    copy_file = strcat(path_new, {'\'}, full_name(1,i));
    save_path = list(i,1).folder;
    if save_direct == 1
        music_folder{1} = 'empty';
        interpret{i} = music_folder{1};
        folder_path = music_path;
        save_name = strcat(folder_path, {'\'}, song_name, {'.mp3'});
    end
    %check if folder already exists
    x = strmatch(char(interpret{i}), char(music_folder), 'exact');
    if x>0
        % check if song already exists
        list_already = dir(char(folder_path));
        for j=1:1:length(list_already)
            path_already{j}=list_already(j,1).name;
        end      
        % compare strings if song already exists
        x = strmatch(char(strcat(song_name, {'.mp3'})), char(path_already),'exact');
        if isempty(x)
            copyfile(char(copy_file), char(save_name));
            fprintf(char(strcat(song_name, {'.mp3\n'})));
        end
        % create folder and save
    else
        mkdir(char(folder_path));      
        copyfile(char(copy_file), char(save_name));
        fprintf(char(strcat(song_name, {'.mp3\n'})));
        music_folder(1,end+1) = interpret(1,i);
    end
end

Kommentare

Beliebte Posts aus diesem Blog

Matlab: Fehlergeraden

Matlab: 3D Kartesisches Grid, Vektoren mit Transformation/Rotation

Matlab: Farbspektrum für Plots