Streaming Music Web-server pt8

Streaming Music Web-server pt8

So last time we are able to get some information from a mp3 file and output it to a screen as well as a text file. So now we are going to work on what we would do with that data with our music server.

So if you have been following my examples and download from my repository you notice I have a file call (Will Smith) – Gettin’ Jiggy Wit It.mp3 here is a partial output from the file:


Input #0, wav, from '(Will Smith) - Gettin' Jiggy Wit It.mp3':
  Metadata:
    title           : Gettin' Jiggy Wit It
    artist          : Will Smith
    publisher       : Elektra/Asylum
    genre           : Rap/R&B
    album           : 1999 Grammy Nominees: Rap [Clean]
    track           : 4
    album_artist    : Various Artists
    composer        : Bernard Edwards/Joe Robinson/Nile Rodgers/Samuel Barnes/Will Smith
    date            : 1999
  Duration: 00:15:11.73, bitrate: 31 kb/s
    Stream #0:0: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, fltp, 128 kb/s
At least one output file must be specified

So what I want to do is collect all that date to be stored for retrieval at a later date. There are many ways to parse date for this example I will be using regular expression also known as regex. Wikipedia defines regular expression a sequence of characters that specifies a search pattern. Usually such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation.

So for the next part I recommend paying a visit to regex101.com this site allows you play around online with regular expressions. So in the large bottom box on the page you want to put your test string. In the top box you want to add your regular expression.

So in the top box add the following code:

Metadata:|\s+((?=title)title\s+:\s(?<TITLE>.+$))
|\s+((?=artist)artist\s+:\s(?<ARTIST>.+$))
|\s+((?=publisher)publisher\s+:\s(?<PUBLISHER>.+$))
|\s+((?=genre)genre\s+:\s(?<GENRE>.+$))
|\s+((?=album)album\s+:\s(?<ALBUM>.+$))
|\s+((?=track)track\s+:\s(?<TRACK>.+$))
|\s+((?=album_artist)album_artist\s+:\s(?<ALBUM_ARTISTS>.+$))
|\s+((?=composer)composer\s+:\s(?<COMPOSER>.+$))
|\s+((?=date)date\s+:\s(?<DATE>.+$))
|\s+((?=Duration)Duration:\s(?<DURATION>\d{2}:\d{2}:\d{2}.\d{2}),)|\s+((?=bitrate)bitrate:\s(?<BITRATE>\d{1,5}\s.+$))

In the bottom box add the partial snippet from the Gettin’ Jiggy Wit It.mp3. Now take notice on the screen as a lot of information is being displayed. Let me say this I am not an expert of regular expression and it can be overwhelming and challenging but it is a necessary evil when dealing with data.

So I will give a brief explanation of what is going on but I highly recommend you study the site to get a more depth understanding. First the code starts out by searching for a string called Metadata:. The reason we search for this string is that it will always be present in our output so it will be are starting point.

If you highlight over the top code the site does a good job explaining what it does. I will continue to explain the first line. The | symbol is a conditional operator because there will be some instance where the value may not be present. The \s+ means gobble up multiple spaces. ((?=title) here we tell it to open a code block and search for text name title if it is found we continue on otherwise we go to the second line.

We then have title\s+:\s which gobbles up the word title any number of spaces and a colon followed by one space. The final code (?<TITLE>.+$)) creates a key name title with the value of whatever is in the string until we reach the end of the line. This final code also close the code block.

And there you go from here you can review the other lines and gleam information from them. For homework study regular expression and play around with it to get a better understanding. Also try different test strings in the test.

This has been a heavy session so please take some time to absorb the information. If you have any questions, comments, and/or suggestions please place them in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *