如何从 swift / AVKIT 中的 HLS 流中读取 id3 标签/其他元数据
how to read id3 tags / other metadata from an HLS stream in swift / AVKIT
我正在尝试收集一些有关如何从 iOS 应用程序内的 HLS 流中读取元数据的知识。
下面的 HLS 流有一些我想读取的 ID3 标签:
HLS test stream
在 Safari 的网络检查器中,我可以在控制台中看到很多数据对象,每个对象都有元数据:
在网络检查器的网络选项卡中,我可以读取播放列表文件:
#EXTM3U
#EXT-X-VERSION:5
#EXT-X-MEDIA-SEQUENCE:89147
#EXT-X-TARGETDURATION:20
#EXT-X-PROGRAM-DATE-TIME:2019-09-25T11:35:23.401Z
#EXTINF:19.970,
05-20190925T113523Z.aac
#EXTINF:19.970,
05-20190925T113543Z.aac
#EXTINF:19.970,
05-20190925T113603Z.aac
#EXTINF:19.970,
05-20190925T113623Z.aac
#EXTINF:19.970,
05-20190925T113643Z.aac
#EXTINF:19.970,
05-20190925T113703Z.aac
到目前为止,我已经实现了 class,它使用 AVPlayer
实例来播放此流。它工作正常。
我将 AVPlayer
和 AVPlayerItem
的各种属性打印到 Xcode 控制台。
但是,我唯一可以解释的 属性 是 AVPlayerItem.currentTime
,它为我提供了播放列表文件中 EXT-X-PROGRAM-DATE-TIME
的值。
所有其他属性似乎与我在播放列表和 id3 标签中看到的信息无关。
有什么方法可以读取每个 id3 标签中包含的元数据?
如何从播放列表中读取 EXT-X-TARGETDURATION
?
我读到了 AVPlayerItemMetadataCollector
,但我不明白它应该做什么以及这是否有助于我读取 HLS 流中的元数据。
我是这样实现的:
import UIKit
import AVKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController{
let player = AVPlayer()
var playerItem: AVPlayerItem!
let asset = AVAsset(url: URL(string: "https://db2.indexcom.com/bucket/ram/00/05/05.m3u8")!)
override func viewDidLoad() {
prepareToPlay()
player.play()
}
func prepareToPlay() {
playerItem = AVPlayerItem(asset: asset)
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: [], context: nil)
player.replaceCurrentItem(with: playerItem)
printTimeStamp()
}
func printTimeStamp() {
print("▼⎺▼⎺▼⎺▼⎺▼⎺▼⎺▼⎺▼")
print("PROGRAM-DATE-TIME: ")
print(playerItem.currentDate() ?? "No timeStamp")
print("▲_▲_▲_▲_▲_▲_▲_▲\n\n")
}
override func observeValue(forKeyPath: String?, of: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if forKeyPath != "timedMetadata" { return }
printTimeStamp()
let data: AVPlayerItem = of as! AVPlayerItem
guard let timedMetadata = data.timedMetadata else { return }
for item in timedMetadata {
switch item.commonKey {
case .commonKeyAlbumName?:
print("AlbumName: \(item.value!)")
case .commonKeyArtist?:
print("Artist: \(item.value!)")
case .commonKeyArtwork?:
print("Artwork: \(item.value!)")
case .commonKeyAuthor?:
print("Author: \(item.value!)")
case .commonKeyContributor?:
print("Contributor: \(item.value!)")
case .commonKeyCopyrights?:
print("Copyrights: \(item.value!)")
case .commonKeyCreationDate?:
print("CreationDate: \(item.value!)")
case .commonKeyCreator?:
print("creator: \(item.value!)")
case .commonKeyDescription?:
print("Description: \(item.value!)")
case .commonKeyFormat?:
print("Format: \(item.value!)")
case .commonKeyIdentifier?:
print("Identifier: \(item.value!)")
case .commonKeyLanguage?:
print("Language: \(item.value!)")
case .commonKeyMake?:
print("Make: \(item.value!)")
case .commonKeyModel?:
print("Model: \(item.value!)")
case .commonKeyPublisher?:
print("Publisher: \(item.value!)")
case .commonKeyRelation?:
print("Relation: \(item.value!)")
case .commonKeySoftware?:
print("Software: \(item.value!)")
case .commonKeySubject?:
print("Subject: \(item.value!)")
case .commonKeyTitle?:
print("Title: \(item.value!)")
case .commonKeyType?:
print("Type: \(item.value!)")
case .id3MetadataKeyAlbumTitle?:
print("id3MetadataKeyAlbumTitle: \(item.value!)")
default:
print("other data: \(item.value!)")
}
}
}
}
我正在尝试收集一些有关如何从 iOS 应用程序内的 HLS 流中读取元数据的知识。 下面的 HLS 流有一些我想读取的 ID3 标签: HLS test stream
在 Safari 的网络检查器中,我可以在控制台中看到很多数据对象,每个对象都有元数据:
在网络检查器的网络选项卡中,我可以读取播放列表文件:
#EXTM3U
#EXT-X-VERSION:5
#EXT-X-MEDIA-SEQUENCE:89147
#EXT-X-TARGETDURATION:20
#EXT-X-PROGRAM-DATE-TIME:2019-09-25T11:35:23.401Z
#EXTINF:19.970,
05-20190925T113523Z.aac
#EXTINF:19.970,
05-20190925T113543Z.aac
#EXTINF:19.970,
05-20190925T113603Z.aac
#EXTINF:19.970,
05-20190925T113623Z.aac
#EXTINF:19.970,
05-20190925T113643Z.aac
#EXTINF:19.970,
05-20190925T113703Z.aac
到目前为止,我已经实现了 class,它使用 AVPlayer
实例来播放此流。它工作正常。
我将 AVPlayer
和 AVPlayerItem
的各种属性打印到 Xcode 控制台。
但是,我唯一可以解释的 属性 是 AVPlayerItem.currentTime
,它为我提供了播放列表文件中 EXT-X-PROGRAM-DATE-TIME
的值。
所有其他属性似乎与我在播放列表和 id3 标签中看到的信息无关。
有什么方法可以读取每个 id3 标签中包含的元数据?
如何从播放列表中读取 EXT-X-TARGETDURATION
?
我读到了 AVPlayerItemMetadataCollector
,但我不明白它应该做什么以及这是否有助于我读取 HLS 流中的元数据。
我是这样实现的:
import UIKit
import AVKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController{
let player = AVPlayer()
var playerItem: AVPlayerItem!
let asset = AVAsset(url: URL(string: "https://db2.indexcom.com/bucket/ram/00/05/05.m3u8")!)
override func viewDidLoad() {
prepareToPlay()
player.play()
}
func prepareToPlay() {
playerItem = AVPlayerItem(asset: asset)
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: [], context: nil)
player.replaceCurrentItem(with: playerItem)
printTimeStamp()
}
func printTimeStamp() {
print("▼⎺▼⎺▼⎺▼⎺▼⎺▼⎺▼⎺▼")
print("PROGRAM-DATE-TIME: ")
print(playerItem.currentDate() ?? "No timeStamp")
print("▲_▲_▲_▲_▲_▲_▲_▲\n\n")
}
override func observeValue(forKeyPath: String?, of: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if forKeyPath != "timedMetadata" { return }
printTimeStamp()
let data: AVPlayerItem = of as! AVPlayerItem
guard let timedMetadata = data.timedMetadata else { return }
for item in timedMetadata {
switch item.commonKey {
case .commonKeyAlbumName?:
print("AlbumName: \(item.value!)")
case .commonKeyArtist?:
print("Artist: \(item.value!)")
case .commonKeyArtwork?:
print("Artwork: \(item.value!)")
case .commonKeyAuthor?:
print("Author: \(item.value!)")
case .commonKeyContributor?:
print("Contributor: \(item.value!)")
case .commonKeyCopyrights?:
print("Copyrights: \(item.value!)")
case .commonKeyCreationDate?:
print("CreationDate: \(item.value!)")
case .commonKeyCreator?:
print("creator: \(item.value!)")
case .commonKeyDescription?:
print("Description: \(item.value!)")
case .commonKeyFormat?:
print("Format: \(item.value!)")
case .commonKeyIdentifier?:
print("Identifier: \(item.value!)")
case .commonKeyLanguage?:
print("Language: \(item.value!)")
case .commonKeyMake?:
print("Make: \(item.value!)")
case .commonKeyModel?:
print("Model: \(item.value!)")
case .commonKeyPublisher?:
print("Publisher: \(item.value!)")
case .commonKeyRelation?:
print("Relation: \(item.value!)")
case .commonKeySoftware?:
print("Software: \(item.value!)")
case .commonKeySubject?:
print("Subject: \(item.value!)")
case .commonKeyTitle?:
print("Title: \(item.value!)")
case .commonKeyType?:
print("Type: \(item.value!)")
case .id3MetadataKeyAlbumTitle?:
print("id3MetadataKeyAlbumTitle: \(item.value!)")
default:
print("other data: \(item.value!)")
}
}
}
}