Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attributes #1

Open
iDevelopper opened this issue Feb 21, 2024 · 5 comments
Open

Custom attributes #1

iDevelopper opened this issue Feb 21, 2024 · 5 comments

Comments

@iDevelopper
Copy link

Thanks for this great decoder!

However, I'm looking for the best way to decode an m3u8 file like the one below, to recover all the attributes of EXTINF if they exist.

Thank you for your help.

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:171115544
#EXTINF:10,title="The Ben Maller Show",artist="zc4732",url="song_spot=\"T\" spotInstanceId=\"-1\" length=\"04:00:00\" MediaBaseId=\"\" TAID=\"0\" TPID=\"0\" cartcutId=\"\" amgArtworkURL=\"https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/cff92185-5e92-11ec-9478-8bbc72f158cb/logo\" spEventID=\"01f47968-ccac-11ee-a9cf-f50937f44113\" "
https://n0ab-e2.revma.ihrhls.com/zc4732/10_sz0pexjzvq8g02/main/171115542.aac
#EXTINF:10,title="The Ben Maller Show",artist="zc4732",url="song_spot=\"T\" spotInstanceId=\"-1\" length=\"04:00:00\" MediaBaseId=\"\" TAID=\"0\" TPID=\"0\" cartcutId=\"\" amgArtworkURL=\"https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/cff92185-5e92-11ec-9478-8bbc72f158cb/logo\" spEventID=\"01f47968-ccac-11ee-a9cf-f50937f44113\" "
https://n0ab-e2.revma.ihrhls.com/zc4732/10_sz0pexjzvq8g02/main/171115543.aac
#EXTINF:10,title="The Ben Maller Show",artist="zc4732",url="song_spot=\"T\" spotInstanceId=\"-1\" length=\"04:00:00\" MediaBaseId=\"\" TAID=\"0\" TPID=\"0\" cartcutId=\"\" amgArtworkURL=\"https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/cff92185-5e92-11ec-9478-8bbc72f158cb/logo\" spEventID=\"01f47968-ccac-11ee-a9cf-f50937f44113\" "
https://n0ab-e2.revma.ihrhls.com/zc4732/10_sz0pexjzvq8g02/main/171115544.aac
@ikhvorost
Copy link
Owner

Hi @iDevelopper !

Yes, there is a bug with #EXTINF attribute e.g.:

struct EXTINF2: Decodable {
  public let duration: Double
  public let title: String
  public let artist: String
  public let url: String
}

struct Playlist: Decodable {
  let extinf: [EXTINF2]
}

let m3u8 = """
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:171115544
#EXTINF:10,title="The Ben Maller Show",artist="zc4732",url="song_spot=\"T\" spotInstanceId=\"-1\" length=\"04:00:00\" MediaBaseId=\"\" TAID=\"0\" TPID=\"0\" cartcutId=\"\" amgArtworkURL=\"https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/cff92185-5e92-11ec-9478-8bbc72f158cb/logo\" spEventID=\"01f47968-ccac-11ee-a9cf-f50937f44113\" "
https://n0ab-e2.revma.ihrhls.com/zc4732/10_sz0pexjzvq8g02/main/171115542.aac
"""

let playlist = try! M3U8Decoder().decode(Playlist.self, from: m3u8)
print(playlist.extinf[0]) 

// Prints: EXTINF2(duration: 10.0, title: "The Ben Maller Show", artist: "zc4732", url: "song_spot=")

But url must be:

song_spot=\"T\" spotInstanceId=\"-1\" length=\"04:00:00\" MediaBaseId=\"\" TAID=\"0\" TPID=\"0\" cartcutId=\"\" amgArtworkURL=\"https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/cff92185-5e92-11ec-9478-8bbc72f158cb/logo\" spEventID=\"01f47968-ccac-11ee-a9cf-f50937f44113\"

And then you are able to parse this string value manually because the library parses coma-separated attributes on the top level only.

Thanks for bug reporting.

@iDevelopper
Copy link
Author

Okay, that's exactly what I tested and found. I'm not getting the full text for the url key.

Thank you for your response.

So are you going to fix the bug?

@ikhvorost
Copy link
Owner

Yes, the fix will be available in the next release version.

@ikhvorost
Copy link
Owner

Fixed in 1.2.0. Thanks.

@iDevelopper
Copy link
Author

Hi @ikhvorost,

Awesome! Thanks!

Could you modify the regex attributes so that it also detects quotes (char code 39 "'"), perhaps like this:

    private static let regexAttributes = try! NSRegularExpression(pattern: "([^=,\\s]+)=((\'([^\']+)\')|(\"([^\"]+)\")|([^,]+))")

Add it to the charSetQuotes:

 private static let charSetQuotes = CharacterSet(charactersIn: "\', \"")

Take this into account in the func convertType(text: String) -> Any

        guard text.hasPrefix("\'") == false, text.hasPrefix("\"") == false, text.hasPrefix("0x") == false, text.hasPrefix("0X") == false else {
            return text.trimmingCharacters(in: Self.charSetQuotes)
        }

As I receive the url with some quotes (') (instead of ""):
// song_spot='T' spotInstanceId='-1' length='03:00:00' MediaBaseId='' TAID='0' TPID='0' cartcutId='' amgArtworkURL='https://storage.googleapis.com/portal-content.zettacloud.appspot.com/shows/c2c3b528-59f9-11ec-b000-cb602abe1056/logo\' spEventID='c550c194-daff-11ee-acde-05a5249c06b1'

And expose the function parse(attributes: String, keyValues: inout [String : Any]) to be public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants