WLM3U 是一个用 Swift 编写的 M3U 工具。
示例
要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install
。
要求
iOS | Swift |
---|---|
9.0 + | 5.0 + |
安装
WLM3U 通过 CocoaPods 提供使用。要安装它,只需将以下一行添加到您的 Podfile 中
pod 'WLM3U'
用法
M3U 文件解析
let url = URL(string:"http://xxx.com/yyy.m3u8")! // URL of the M3U file
let size: Int = <#fileSize#> // The total size of all ts files
WLM3U
.attach(url: url,
size: size,
tsURL: { (path, url) -> URL? in
if path.hasSuffix(".ts") {
return url.appendingPathComponent(path)
} else {
return nil
}
},
completion: { (result) in
switch result {
case .success(let model):
print("[Attach Success] " + model.name!)
case .failure(let error):
print("[Attach Failure] " + error. localizedDescription)
}
})
下载由 M3U 文件描述的 ts 文件
let url = URL(string:"http://xxx.com/yyy.m3u8")! // URL of the M3U file
let size: Int = <#fileSize#> // The total size of all ts files
WLM3U
.attach(url: url, size: size)
.download(progress: { (progress, completedCount) in
progress // Current download progress
completedCount // Download speed (B/S)
}, completion: { (result) in
switch result {
case .success(let url):
url // The directory where the ts file is located
case .failure(let error):
print("[Download Failure] " + error.localizedDescription)
}
})
将下载的 ts 文件合并成一个文件
let url = URL(string:"http://xxx.com/yyy.m3u8")! // URL of the M3U file
let size: Int = <#fileSize#> // The total size of all ts files
WLM3U
.attach(url: url, size: size)
.download()
.combine(completion: { (result) in
switch result {
case .success(let url):
url // The directory where the files are located after the combine is completed
case .failure(let error):
print("[Combine Failure] " + error.localizedDescription)
}
})
自动获取 ts 文件的总大小
WLM3U 支持在调用 WLM3U.attach()
函数而不传入 size
参数时,自动获取所有文件的总大小。获取大小的过程是异步的,您可以通过接收 TaskGetFileSizeProgressNotification
和 TaskGetFileSizeCompletionNotification
来获取大小数据。
暂停和恢复任务
为了简化接口,WLM3U 没有提供 pause
和 resume
概念。它们和 cancel
以及 attach
是一样的,所以
您需要暂停一个任务时,调用 cancel(url: URL)
。
您需要取消任务时,调用 cancel(url: URL)
并通过 folder(for url: URL)
获取任务缓存目录并删除它。
您需要添加任务时,调用 attach(url: URL)
。
您需要恢复任务时,调用 attach(url: URL)
,如果之前的缓存已经存在于本地,它将自动继续下载剩余的文件。
监听状态
WLM3U 具有几种内部通知,您可以通过这些通知来处理数据
/// A notification that will be sent when the progress of the task changes.
public let TaskProgressNotification: Notification.Name
/// A notification that will be sent when the progress of getting file size changes.
public let TaskGetFileSizeProgressNotification: Notification.Name
/// A notification that will be sent when size of all files has got.
public let TaskGetFileSizeCompletionNotification: Notification.Name
/// A notification that will be sent when the task ends.
public let TaskCompletionNotification: Notification.Name
/// A notification that will be sent when a task has an error.
public let TaskErrorNotification: Notification.Name
播放下载的文件
目前,AVPlayer 和 WLM3U 不支持播放本地 ts 文件。这里有两种简单可行的替代方案。
使用 GCDWebServer 构建本地服务
注意:在以这种方式播放时,不要调用 WLM3U.combine()
函数。
使用 GCDWebServer 库
pod "GCDWebServer"
创建一个本地 HTTP 服务,以提供下载的 ts 文件
let server = GCDWebServer()
let path = <#folderPath#> // The local directory where the ts file is located
server.addGETHandler(forBasePath: "/",
directoryPath: path,
indexFilename: "file.m3u8",
cacheAge: 3600,
allowRangeRequests: true)
server.start()
然后,使用 AVPlayer 播放本地服务提供的 ts 文件
let url = URL(string: "https://:\(server.port)/file.m3u8")
let player = AVPlayer(url: url)
使用 FFmpeg 将 ts 文件转换为 mp4 文件
使用 mobile-ffmpeg-full 库:
pod "mobile-ffmpeg-full"
执行转码命令
let command = "-i 'The path where the ts file is located' 'The path to which the mp4 file is saved'"
let result = MobileFFmpeg.execute(command)
if result == RETURN_CODE_SUCCESS {
// Transcode completion
}
作者
Willie, [邮箱地址隐藏]
WLM3U 是一个用 Swift 实现的 M3U 工具。
示例
克隆此仓库,然后执行 pod install
命令,运行示例项目。
需求
iOS | Swift |
---|---|
9.0 + | 5.0 + |
安装
WLM3U 可通过 CocoaPods 安装,只需将以下行添加到 Podfile 即可
pod 'WLM3U'
使用
解析 M3U 文件
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U 文件的 URL
let size: Int = <#fileSize#> // 所有 ts 文件的总大小
WLM3U
.attach(url: url,
size: size,
tsURL: { (path, url) -> URL? in
if path.hasSuffix(".ts") {
return url.appendingPathComponent(path)
} else {
return nil
}
},
completion: { (result) in
switch result {
case .success(let model):
print("[Attach Success] " + model.name!)
case .failure(let error):
print("[Attach Failure] " + error. localizedDescription)
}
})
下载 M3U 文件描述的 ts 文件
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U 文件的 URL
let size: Int = <#fileSize#> // 所有 ts 文件的总大小
WLM3U
.attach(url: url, size: size)
.download(progress: { (progress, completedCount) in
progress // 当前下载的进度
completedCount // 下载速度( B/S )
}, completion: { (result) in
switch result {
case .success(let url):
url // ts 文件所在的目录
case .failure(let error):
print("[Download Failure] " + error.localizedDescription)
}
})
将下载的 ts 文件合并成一个文件
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U 文件的 URL
let size: Int = <#fileSize#> // 所有 ts 文件的总大小
WLM3U
.attach(url: url, size: size)
.download()
.combine(completion: { (result) in
switch result {
case .success(let url):
url // 合并完成后文件所在的目录
case .failure(let error):
print("[Combine Failure] " + error.localizedDescription)
}
})
自动获取 ts 文件总大小
当调用 WLM3U.attach()
函数未传递 size
参数时,WLM3U 会自动获取所有文件的总大小。这个过程是异步的,可以通过接收 TaskGetFileSizeProgressNotification
和 TaskGetFileSizeCompletionNotification
来获取大小数据。
暂停与恢复任务
为了简化接口,WLM3U 没有暂停与恢复的概念,它们和取消与添加是相同的,所以:
需要暂停一个任务时,调用 cancel(url: URL)
。
需要取消一个任务时,调用 cancel(url: URL)
,并通过 folder(for url: URL)
获取到此任务缓存目录,并删除它即可。
需要添加一个任务时,调用 attach(url: URL)
。
需要恢复一个任务时,调用 attach(url: URL)
,如果本地存在之前的缓存,会自动继续下载剩余的文件。
监听状态
WLM3U 内置了几个状态的通知,你可以接收这些通知来处理数据:
/// 下载进度发生变化时会发出的通知。
public let TaskProgressNotification: Notification.Name
/// 获取文件总大小的进度发生变化时会发出的通知。
public let TaskGetFileSizeProgressNotification: Notification.Name
/// 获取文件总大小完成时会发出的通知。
public let TaskGetFileSizeCompletionNotification: Notification.Name
/// 任务完成时会发出的通知。
public let TaskCompletionNotification: Notification.Name
/// 任务发生错误时会发出的通知。
public let TaskErrorNotification: Notification.Name
播放下载的文件
AVPlayer 与 WLM3U 暂不支持播放本地 ts 文件,这里提供两个简单可行的替代方案。
使用 GCDWebServer 搭建本地服务
注意:使用此方式播放时,不要调用 WLM3U.combine()
函数。
引入 GCDWebServer 库:
pod "GCDWebServer"
创建本地 HTTP 服务来提供下载好的 ts 文件:
let server = GCDWebServer()
let path = <#folderPath#> // ts 文件所在的本地目录
server.addGETHandler(forBasePath: "/",
directoryPath: path,
indexFilename: "file.m3u8",
cacheAge: 3600,
allowRangeRequests: true)
server.start()
使用 AVPlayer 来播放本地服务提供的 ts 文件:
let url = URL(string: "https://:\(server.port)/file.m3u8")
let player = AVPlayer(url: url)
使用 FFmpeg 将 ts 文件转码成 mp4 文件
引入 mobile-ffmpeg-full 库:
pod "mobile-ffmpeg-full"
执行转码命令:
let command = "-i 'ts文件所在的路径' 'mp4文件要保存到的路径'"
let result = MobileFFmpeg.execute(command)
if result == RETURN_CODE_SUCCESS {
// 转码完成
}
接下来直接播放转码得到的 mp4 文件即可。
作者
Willie, [email protected]
许可协议
WLM3U 可在 MIT 许可协议下访问。更多信息请参阅 LICENSE 文件。