Archive for ‘C#’

26/01/2014

Automatically generating playlists (.m3u) from directory structure

I have many audiobooks that I like to listen to. With my growing collection, I thought it would be a good idea to set up some streaming on my internal network via a browser.

To cut a long story short, streaming is easy one file at a time – open the appropriate folder on the server to allow a web browser to browse to it. When that file is finished, click back and go to the next. Obviously this is OK for mp3 files of 30 minute episodes, but not so hot on a folder containing 40 x 5 minute tracks.

A playlist enables all the mp3 files to be played in a particular folder without any intervention. The pertinent C# code I have as a working starting point is as follows:

public void ProcessDir(string sourceDir)
{
StringBuilder sb = new StringBuilder();
StringBuilder sbPlayList = new StringBuilder();
List ls = new List();

string title = "";
string playListTitle = "";
string oldPlayListTitle = "";
int ct = 0;

foreach (string fileName in Directory.EnumerateFiles(sourceDir, "*.mp3", SearchOption.AllDirectories).OrderBy(filename => filename))
{
title = Path.GetDirectoryName(fileName).Replace(sourceDir,"");

string[] words = title.Split('\\');
if (words.Count() > 2)
playListTitle = words[2]; // the title eg. \author\title
else
playListTitle = words[1]; // just the authors name - no title subfolders

title = title.Replace(" ","%20");
title = title.Replace("\\", "/");
if (!ls.Contains(title))
{
ls.Add(title);
if (ct > 0) // don't write an empty playlist on the first one
{
sb.AppendLine("Adding " + oldPlayListTitle + ".m3u");
StreamWriter playListFile = new StreamWriter(cbDestination.Text + oldPlayListTitle + ".m3u");
playListFile.Write(sbPlayList.ToString());
playListFile.Close();
//sb.AppendLine("Contents of sbPlayList are: " + sbPlayList.ToString());
sbPlayList.Clear();
}
sb.AppendLine(playListTitle.ToUpper());
playListTitle = playListTitle.Replace(" ", "_");
oldPlayListTitle = playListTitle.ToLower();
ct++;
}
sb.AppendLine(cbPrefix.Text
+ title + "/"
+ Path.GetFileName(fileName).Replace(" ", "%20"));

sbPlayList.AppendLine(cbPrefix.Text
+ title + "/"
+ Path.GetFileName(fileName).Replace(" ", "%20"));
}
tb.Text = sb.ToString();
}
}

I am using a VLC media plugin with Chrome to play the m3u playlists directly from a link.

With my Android phone it is slightly more complex:

  • In the Dolphin web browser click the link to the m3u.
  • Choose to download the file with ES File explorer
  • Finally open the playlist with the Just Playlists application.

I make no apologies for the brevity of the explanation – still busy testing the above. I will do a more thorough write up when I have finalised my solution.

Advertisements
22/02/2013

UK to MySQL Date in C#

A C# snippet that will allow a function to be written where a string is passed in, containing a date in the format dd/mm/yyyy, and have that replaced by yyyy-mm-dd:

// Matches 2 digits forward slash 2 digits forward slash 4 digits e.g. 22/02/2013
Match m = Regex.Match(input, "\\d{2}/\\d{2}/\\d{4}");
// could use @"\d{2}/\d{2}/\d{4}" instead of the double backslash escape
if (m.Index > 0)
{
    string dd = input.Substring(m.Index, 2);
    string mm = input.Substring(m.Index + 3, 2);
    string yyyy = input.Substring(m.Index + 6, 4);

    input = input.Replace(dd + "/" + mm + "/" + yyyy, yyyy + "-" + mm + "-" + dd);
}