[英]iOS offline HLS file size
在 iOS 10 中,Apple 添加了離線 HLS。 在文檔中,他們提到:
重要提示:下載的 HLS 資產以私有包格式存儲在磁盤上。 此捆綁包格式可能會隨着時間的推移而改變,開發人員不應嘗試直接訪問或存儲捆綁包中的文件,而應使用 AVFoundation 和其他 iOS API 與下載的資產進行交互。
似乎對有關這些文件的信息的訪問受到限制。 我正在嘗試查找存儲文件的大小。 這就是我所做的。 下載完成后,我保存相對路徑
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
//Save path
video?.downloadPath = location.relativePath
}
后來我重建文件路徑如下
if let assetPath = workout.downloadPath {
let baseURL = URL(fileURLWithPath: NSHomeDirectory())
let assetURL = baseURL.appendingPathComponent(assetPath)
這有效:
try FileManager.default.removeItem(at: assetURL)
這不會並返回文件不存在的錯誤:
let att = try FileManager.default.attributesOfItem(atPath: assetURL.absoluteString)
我可以按如下方式加載視頻資產並使用以下命令離線播放:
let avAsset = AVURLAsset(url: assetURL)
但這會返回一個空數組:
let tracks = avAsset.tracks(withMediaType: AVMediaTypeVideo)
再一次,我只是想獲取離線 HLS 資產的文件大小。 看來 SO 上使用 FileManager 獲取文件大小的其他答案不適用於這些,也不適用於獲取加載的 AVAsset 大小的答案。 提前致謝。
唯一的方法是將所有文件大小匯總到存儲下載內容的文件夾中。
- (NSUInteger)hlsFileSize:(NSURL *)fileURL {
NSUInteger size = 0;
let enumerator = [NSFileManager.defaultManager enumeratorAtURL:fileURL includingPropertiesForKeys:nil options:0 errorHandler:nil];
for (NSURL *url in enumerator) {
NSError *error = nil;
// Get values
let resourceValues = [url resourceValuesForKeys:@[NSURLIsRegularFileKey, NSURLFileAllocatedSizeKey, NSURLNameKey] error:&error];
// Skip unregular files
let isRegularFile = [resourceValues[NSURLIsRegularFileKey] boolValue];
if (!isRegularFile) {
continue;
}
let fileAllocatedSize = [resourceValues[NSURLFileAllocatedSizeKey] unsignedLongLongValue];
size += fileAllocatedSize;
}
return size;
}
以下是計算離線 HLS (.movpkg) 文件大小的方法:
/// Calculates HLS File Size.
/// - Parameter directoryPath: file directory path.
/// - Returns: Human Redable File Size.
func getHLSFileSize(at directoryPath: String) -> String? {
var result: String? = nil
let properties: [URLResourceKey] = [.isRegularFileKey,
.totalFileAllocatedSizeKey,
/*.fileAllocatedSizeKey*/]
guard let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: directoryPath),
includingPropertiesForKeys: properties,
options: .skipsHiddenFiles,
errorHandler: nil) else {
return nil
}
let urls: [URL] = enumerator
.compactMap { $0 as? URL }
.filter { $0.absoluteString.contains(".frag") }
let regularFileResources: [URLResourceValues] = urls
.compactMap { try? $0.resourceValues(forKeys: Set(properties)) }
.filter { $0.isRegularFile == true }
let sizes: [Int64] = regularFileResources
.compactMap { $0.totalFileAllocatedSize! /* ?? $0.fileAllocatedSize */ }
.compactMap { Int64($0) }
let size = sizes.reduce(0, +)
result = ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: .file)
return result
}
if let url = URL(string: localFileLocation),
let size = self.getHLSFileSize(at: url.path) {
result = String(size)
}
試試這個:
let att = try FileManager.default.attributesOfItem(atPath: assetURL.path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.