Android Question M3U Parser

aidymp

Well-Known Member
Licensed User
Longtime User
Hi im looking read M3U playlists? I was thinking it would be quite easy at first, however, there seems to be a variation on them, im also confused about M3U8 are the the same??

The ones I have seen look like the examples below.

#EXTM3U
#EXTINF:0,Title1
video1.m3u8
#EXTINF:1,Title2
video2.m3u8
...

but they can also look like this :

#EXTM3U
#EXT-X-STREAM-INF:pROGRAM-ID=1,BANDWIDTH=1217000,RESOLUTION=1280x720
2013girlwithipad-1200k.m3u8
#EXT-X-STREAM-INF:pROGRAM-ID=1,BANDWIDTH=824000,RESOLUTION=896x504
2013girlwithipad-800k.m3u8
...

But I have seen some M3U's with lots more tags!

Google shows 3 github M3U8 Parsers? and im not sure if they are what I need, but they do look promising (if someone was able to wrap them? ;)

So has anyone any experience of reading M3U's and can you help with reading them please, or maybe someone could try wrapping one of the libraries???

Thanks

Aidy
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
It is a plain-text format, for which there is no "official" RFC specification: https://en.wikipedia.org/wiki/M3U

It would not be too hard to write your own parsing method for the file; at the simplest level you could just ignore any line beginning with a "#" symbol, and assume that everything else is a filename or URI.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
It is a plain-text format, for which there is no "official" RFC specification: https://en.wikipedia.org/wiki/M3U

It would not be too hard to write your own parsing method for the file; at the simplest level you could just ignore any line beginning with a "#" symbol, and assume that everything else is a filename or URI.

That's part of the problem solved then! lol I was searching for some specification, but the ones I saw all read different!

I think I will make a start in just code then!

Thanks

Aidy
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I wonder whether a regular expression could locate all the ".m3u8" characters and bring with it all the characters before the next white space? I'm not that clues up with regular expressions, although I have used them before and so think that this is possible.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I wonder whether a regular expression could locate all the ".m3u8" characters and bring with it all the characters before the next white space? I'm not that clues up with regular expressions, although I have used them before and so think that this is possible.

Most of the files I work with look similar to this:

#EXTM3U
#EXTINF:0,Title1
video1.m3u8
#EXTINF:1,Title2
video2.m3u8

But the m3u8 as in video2.m3u8 was just an example, most end in something else. .ts .mp4 .strm so regex may not work for that. But Im just testing some code, as for myself i'm only interested in Title, and link (shown in the list example above).

Ideally I can add those to a map and do what i want with it.

Thanks

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User

I tried that but regex is pretty alien to me, and trying the code in regex-tool and an m3u did nothing! I will investigate more... I did however have good results in code..

Thanks

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
The code below gives the title, and file / url of the item from a simple M3U list like:

#EXTM3U
#EXTINF:0,Title1
video1.m3u8
#EXTINF:1,Title2
video2.m3u8

B4X:
    ...
    txt=File.ReadString(File.DirAssets,"ap.m3u")
    Txt=Txt.Replace("#EXTM3U"&CRLF,"")
    Txt=Txt.Replace("#EXTINF:-1","")
    Txt=Txt.Replace(",","")
    myans = SplitText(Txt)
    Log(Txt)
    For i = 0 To myans.Length -1
        Log(myans(i)&" - "&myans(i+1))
        i=i+1
    Next
    ...

    
   Sub SplitText(Text As String) As String()
        Dim sText() As String
        sText = Regex.Split(CRLF, Text)
        Return sText
   End Sub

gives output I can use in a map like

Title 1 - video1.m3u8
Title 2 - video2.m3u8

BUT I have a more complex list! and that is presenting problems! as it also contains the title twice!

sample!

#EXTM3U
#EXTINF:-1 tvg-ID="" tvg-name="USA: Title1" tvg-logo="" group-title="USA",USA: Title1
http://nothingtoseehere1.m3u8
#EXTINF:-1 tvg-ID="" tvg-name="USA: Title2" tvg-logo="" group-title="USA",USA: Title2
http://nothingtoseehere2.m3u8

I managed to get

USA: Title1"USA: Title1 - http://nothingtoseehere1.m3u8
USA: Title2"USA: Title2 - http://nothingtoseehere2.m3u8

just by adding more:

B4X:
Txt=Txt.Replace($" tvg-ID="" tvg-name=""$,"")
Txt=Txt.Replace($"USA: "$,"")
Txt=Txt.Replace($"" tvg-logo="""$,"")
Txt=Txt.Replace($" group-title=""$,"")
Txt=Txt.Replace($" group-title="USA"$,"")

I presume adding to a list I could search the " and remove anything upto and including the "

As its becoming clear there is no standard, I presume a library would have the same trouble!?

Thanks

Aidy
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Yes, I would assume the same issues would apply to any library. Lacking any "official" standard, ultimately you, or the library writer, will have decide what you want/need to support in terms of parsing the file entries.

RegEx is very powerful and flexible (there are entire courses devoted to nothing but RexEx programming) and quite worth the time invested in understanding how it works (at least on a basic level). I'm barely functionally literate with RegEx but I'm sure you can come up with one to parse TAG="VALUE" into groups that you can then parse into a map of (Tag, Value) entries. I also recommend http://www.regxlib.com as a decent source for RegEx patterns that people have already created (although I did not find any M3U ones there).
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Yes, I would assume the same issues would apply to any library. Lacking any "official" standard, ultimately you, or the library writer, will have decide what you want/need to support in terms of parsing the file entries.

RegEx is very powerful and flexible (there are entire courses devoted to nothing but RexEx programming) and quite worth the time invested in understanding how it works (at least on a basic level). I'm barely functionally literate with RegEx but I'm sure you can come up with one to parse TAG="VALUE" into groups that you can then parse into a map of (Tag, Value) entries. I also recommend http://www.regxlib.com as a decent source for RegEx patterns that people have already created (although I did not find any M3U ones there).

Thanks, I sort of cracked the main aim for 2 types of m3u8 the latter one I was stuck with above, all of the data was in a string, so i loaded it to a map, then found out you cant rename keys in the map, so extracted them, and removed the duplicate name before and including the " and added them to a second map! may be long winded but trying that in a string I kept deleting 99% of the string and ending up with one correct entry! lol

I will try and study regex, as I can see it is very powerful.

I would also like to say thanks to everyone who answered, once again you have proven this community is very helpful! and not belittling like some others!

Thanks

Aidy
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Using the three samples you have provided above, then this regex expression seems to extract all of the links and it will find links with extensions of .mu3 .m3u8 .mp4 .ts and .strm
.*\.(m3u[8]{0,1}|mp4|ts|strm)
I've only ever played with simple regex expressions and so some one with far more experience may say that this is totally wrong and could produce bad results under certain circumstances, I just don't know. But it works on the regex tester that @Jeffrey Cameron provided in post #6.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Using the three samples you have provided above, then this regex expression seems to extract all of the links and it will find links with extensions of .mu3 .m3u8 .mp4 .ts and .strm
.*\.(m3u[8]{0,1}|mp4|ts|strm)
I've only ever played with simple regex expressions and so some one with far more experience may say that this is totally wrong and could produce bad results under certain circumstances, I just don't know. But it works on the regex tester that @Jeffrey Cameron provided in post #6.

Thanks, That certainly seems to extract the file, but not the title! but it is a step in the right direction, so thank you for that

Thanks

Aidy
 
Upvote 0
Top