WLM3U is a M3U tool written in Swift.
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS | Swift |
---|---|
10.0 + | 5.1 + |
pod 'WLM3U'
dependencies: [
.package(url: "https://github.com/WillieWangWei/WLM3U.git", .upToNextMajor(from: "0.1.5"))
]
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)
}
})
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)
}
})
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)
}
})
WLM3U supports automatic acquisition of the total size of all files when calling the WLM3U.attach()
function without passing the size
parameter. The process of getting the size is asynchronous, you can get the size data by receiving TaskGetFileSizeProgressNotification
and TaskGetFileSizeCompletionNotification
.
To simplify the interface, WLM3U does not have the concepts of pause
and resume
. They are the same as cancel
and attach
, so:
When you need to pause a task, call cancel(url: URL)
when you need to pause a task.
When you need to cancel a task, call cancel(url: URL)
and get the task cache directory through folder(for url: URL)
and delete it.
When you need to add a task, call attach(url: URL)
.
When you need to resume a task, call attach(url: URL)
, if the previous cache exists locally, it will automatically continue to download the remaining files.
The WLM3U has built-in notifications for several states that you can receive to process data:
/// 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 and WLM3U do not support playing local ts files at this time. Here are two simple and feasible alternatives.
Note: Do not call the WLM3U.combine()
function when playing in this way.
Using the GCDWebServer library:
pod "GCDWebServer"
Create a local HTTP service to provide the downloaded ts file:
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()
Then, use AVPlayer to play the ts file provided by the local service:
let url = URL(string: "http://localhost:\(server.port)/file.m3u8")
let player = AVPlayer(url: url)
Using the mobile-ffmpeg-full library:
pod "mobile-ffmpeg-full"
Execute the transcoding command:
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, willie.wangwei@gmail.com
WLM3U 是一个用 Swift 实现的 M3U 工具。
clone 这个仓库,接着执行 pod install
命令,然后运行示例项目。
iOS | Swift |
---|---|
9.0 + | 5.0 + |
WLM3U 可通过 CocoaPods 安装,只需将以下行添加到 Podfile 即可
pod 'WLM3U'
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)
}
})
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)
}
})
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)
}
})
当调用 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 文件,这里提供两个简单可行的替代方案。
注意:使用此方式播放时,不要调用 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: "http://localhost:\(server.port)/file.m3u8")
let player = AVPlayer(url: url)
引入 mobile-ffmpeg-full 库:
pod "mobile-ffmpeg-full"
执行转码命令:
let command = "-i 'ts文件所在的路径' 'mp4文件要保存到的路径'"
let result = MobileFFmpeg.execute(command)
if result == RETURN_CODE_SUCCESS {
// 转码完成
}
接下来直接播放转码得到的 mp4 文件即可。
Willie, willie.wangwei@gmail.com
WLM3U is available under the MIT license. See the LICENSE file for more info.