Thursday, May 21, 2009

m3u PlayList Copier

I Made this program as i really very lazy :D

Actually it is really hard to catch each song from a different partition and Folder to copy it .

So with this Program You can Select items from any playlist and copy them to any Destination Like My Mobile Or mp3 any device

ScreenShot as You can See it can read English and Non-English (like Arabic) song names from one playlist

Download Here
Note : You will Find the .exe file with another file with .cs extension (if you are NOT programer ,just Delete it) it just contain the Program Source Code
Code :

First we have a public array ,So we can use it in all program controls (to be accessed by open dialog and save dialog)

public string[] Final_Adresses=new string[0];

After That We need to treat with the playlist language if it is non-English language

//Read all bytes to an array To convert it to unicode ,so a Non-english characters will be shown properly 
byte[] all_bytes = File.ReadAllBytes(openFileDialog1.FileName);
 
// Perform the conversion from one encoding to the other.
byte[] new_encoded = Encoding.Convert(Encoding.Default, Encoding.Unicode, all_bytes);
 
//Convert the new encoded array (of bytes) to a normal string
string uniString = Encoding.Unicode.GetString(new_encoded);
 
//Split the string to array by lines
string[] final_encoded = uniString.Split('\n');

in the code above we read all playlist file as a byte array then we converted it to Unicode(utf-8) then we converted the Unicode byte array to a string the we spitted the new string to get the original names with a unicode characters so it will NOT be displayed like (??????) or any undefined characters

Then We will Make an array with the address ,but the problem with the m3u playlists is ,it depend on the song location from the playlist itself so if the song

For Example :
if the Songs in the Same Directory , the playlist will Contain the song name only
if the Songs in Subdirectory in the same directory of the playlist ,it will contain the “ directory name\song name “
else it will contain the full address
So we will make an array to contain all addresses in unified format

//String Array To Save all addresses as it vary with Some conditions in M3U PlayList
//We Will use an Array List as it doesn't has an initial length
System.Collections.ArrayList address = new System.Collections.ArrayList();
 
//Read the M3u File from this array
                    for (int x = 2; x < final_encoded.Length; x = x + 2)
                    {
                        //the song is in other Directory
                        if (final_encoded[x].Contains(":"))
                        {
                            //removing the last character "\r" to get the exact path
                            address.Add(final_encoded[x].Remove(final_encoded[x].Length - 1));
                            checkedListBox1.Items.Add(final_encoded[x].Remove(0, final_encoded[x].LastIndexOf('\\') + 1));
                        }
                        //if (final_encoded[x].Contains("\\"))
                        else
                        {
                            string tmpadrs = openFileDialog1.FileName;
                            tmpadrs = tmpadrs.Remove(tmpadrs.LastIndexOf('\\'));
                            tmpadrs = tmpadrs + "\\" + final_encoded[x];
                            address.Add(tmpadrs);
                            checkedListBox1.Items.Add(final_encoded[x].Remove(0, final_encoded[x].LastIndexOf('\\') + 1));
                        }
                    }
                    //Converting the array list to a normal array of strings to the public array
                    Final_Adresses = (string[])address.ToArray(typeof(string));

Above we make some checks to determine if the song in the same directory of the playlist or in another place

Save or Copy Button
we check if the user checked any songs or NOT then we copy them

if (checkedListBox1.CheckedItems.Count > 0)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    ProgressBar1.Maximum = checkedListBox1.CheckedItems.Count;
                    for (int i = 0; i < Final_Adresses.Length; i++)
                    {
                        if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
                        {
                            File.Copy(Final_Adresses[i], folderBrowserDialog1.SelectedPath + Final_Adresses[i].Remove(0, Final_Adresses[i].LastIndexOf('\\')));
                            ProgressBar1.PerformStep();
                        }
                    }
                    MessageBox.Show("Copying Selected Items Completed");
                }
            }
            else
            {
                MessageBox.Show("No Items Selected To Copy");
            }

Download the full code file from the link above or if you are lazy like me :D its Here again

You can Deal with any playlist with the same way science other formats is easier to get the song path as they store the full path with NO conditions for its location

1 التعليقات:

Unknown said...

I would add a "check/uncheck all" button!