Matlab: Sorting Songs/Files

(German) This post describes a Matlab function which load out of a folder all songs, process them to a standarized name and save them in a folder in the different interpret folders.

Mode of operation:

First of all the path is chosen where the songs are, which should be sorted and then the folder is chosen, where the songs are copied to. Because in many folders are files like "..", ".", "desktop.ini", these are filtered. So now are all names of the file loaded.
Next the name (interpret and songname) is devided and also further informations in the name like "(Official Video)" or "[pleer.net]" are cutted out. 
Now the first song is taken and if there is a folder existing in the folder where it should be saved, then the song is saved, if it isn't already in the folder.
If the interpret folder is not existing right now a new folder is generated with the name of the interpret and the song is copied into it. This is done for all songs out of the first chosen folder.
At the end all copied songs are printed out.
At the begin one can decide if the song should be saved as "interpret - songname.mp3" or as "songname.mp3". Furthermore one can decide if the songs should be saved, sorted in folders, or not.

Source code:

% 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: 3D Coordinate System Rotations with Vectors

Matlab: Cone/Arrow in 3D

Matlab: Points and vectors in 3D-plots