swift-如何从m3u播放列表解析徽标图像

时间:2018-09-04 23:02:47

标签: swift parsing m3u iptv

我有一个解析m3u播放列表网址,但我也无法解析图片网址?

来自GitHub的所有源代码:https://github.com/ArtemStepuk/zombobox

extension IPTVM3U8PareserService: M3U8ParserService {
func extractChannelsFromRawString(_ string: String) -> [Channel] {
    var channels = [Channel]()
    string.enumerateLines { line, shouldStop in
        if line.hasPrefix("#EXTINF:") {
            let infoLine = line.replacingOccurrences(of: "#EXTINF:", with: "")
            let infoItems = infoLine.components(separatedBy: ",")
            if let title = infoItems.last {
                let channel = Channel(title: title, url: nil, logo: nil)
                channels.append(channel)
            }
        } else {
            if var channel = channels.popLast() {
                channel.url = URL(string: line)
                channels.append(channel)
            }
        }
    }
    return channels
}

tvg-logo提供了解析频道徽标的任何建议。

示例:

#EXTINF:-1 tvg-logo="http://i.cdn.turner.com/adultswim/big/video/mainstream/liveStream.jpg" group-title="USA",[AS] Live Stream
http://adultswimhls-i.akamaihd.net/hls/live/238460/adultswim/main/1/master_Layer5.m3u8

1 个答案:

答案 0 :(得分:0)

//You can do this, and linkIm3 gives you the url for the image    
let val2 = line.components(separatedBy: " ")
for sep in val2{
    if sep.hasPrefix("tvg-logo="){
        let sep2 = sep.components(separatedBy: ",")
        let sep1 = sep2[0]
        let indexStartOfText = sep1.index(sep1.startIndex, offsetBy: 10)
        linkIm2 = String(sep1[indexStartOfText...])
        let indexEndOfText = linkIm2.index(linkIm2.endIndex, offsetBy: -1)
        linkIm3 = String(linkIm2[..<indexEndOfText])
     }
 }