WLM3U is an M3U tool implemented with Swift.
Project address github.com/WillieWangW…
The sample
Clone the repository, execute the pod install command, and then run the sample project.
requirements
iOS | Swift |
---|---|
9.0 + | 5.0 + |
The installation
WLM3U can be installed through CocoaPods by simply adding the following line to your Podfile
pod 'WLM3U'
Copy the code
use
Parse the M3U file
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U file URL
let size: Int = <#fileSize#> // Total size of all ts files
WLM3U
.attach(url: url, size: size, completion: { (result) in
switch result {
case .success(let model):
model.name // yyy
model.tsArr // ts file array.case .failure(let error):
print("attach failure " + error.localizedDescription)
}
})
Copy the code
Download the TS file described in the M3U file
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U file URL
let size: Int = <#fileSize#> // 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 resides
case .failure(let error):
print("download failure " + error.localizedDescription)
}
})
Copy the code
Combine the downloaded TS files into a single file
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U file URL
let size: Int = <#fileSize#> // 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 file resides after the merge is complete
case .failure(let error):
print("combine failure " + error.localizedDescription)
}
})
Copy the code
Automatically get the total size of TS files
WLM3U supports automatic retrieval of the total size of all files by simply setting the calculateSize parameter:
let url = URL(string:"http://xxx.com/yyy.m3u8")! // M3U file URL
WLM3U
.attach(url: url, calculateSize: true)
.download()
.combine()
Copy the code
Get the size of the process is asynchronous, could be obtained by receiving TaskGetFileSizeProgressNotification and TaskGetFileSizeCompletionNotification size data.
Pause and resume a task
To simplify the interface, WLM3U does not have pause and resume concepts, they are the same as cancel and add, so:
When a task needs to be paused, cancel(URL: url) is called.
To cancel a task, call Cancel (URL: URL), obtain 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 restore a task, attach(URL: URL) is called. If the previous cache exists locally, the remaining files will automatically continue to be downloaded.
Listening state
WLM3U has several status notifications built in, which you can receive to process data:
/// A notification when the download progress changes.
public let TaskProgressNotification: Notification.Name
/// a notification when the progress of getting the total file size changes.
public let TaskGetFileSizeProgressNotification: Notification.Name
/// get a notification when the total file size is complete.
public let TaskGetFileSizeCompletionNotification: Notification.Name
/// a notification when the task is complete.
public let TaskCompletionNotification: Notification.Name
/// A notification when a task has an error.
public let TaskErrorNotification: Notification.Name
Copy the code
Play the downloaded file
AVPlayer and WLM3U do not currently support playing local TS files. Here are two simple and viable alternatives.
GCDWebServer is used to build local services
Introduce GCDWebServer library:
pod "GCDWebServer"
Copy the code
Create a local HTTP service to serve downloaded TS files:
let server = GCDWebServer(a)let path = <#folderPath#> // The local directory where the ts file resides
server.addGETHandler(forBasePath: "/",
directoryPath: path,
indexFilename: "file.m3u8",
cacheAge: 3600,
allowRangeRequests: true)
server.start()
Copy the code
Use AVPlayer to play ts files provided by the local service:
let url = URL(string: "http://localhost:\(server.port)/file.m3u8")
let player = AVPlayer(url: url)
Copy the code
Convert TS files into MP4 files using FFmpeg
Introducing the mobile-FFMPEG-Full library:
pod "mobile-ffmpeg-full"
Copy the code
Run the transcoding command:
let command = "-i 'ts file path ' 'path to save mp4 files '"
let result = MobileFFmpeg.execute(command)
if result == RETURN_CODE_SUCCESS {
// Transcoding is complete
}
Copy the code
Then play the transcoding MP4 files directly.
The author
Willie, [email protected]