gpt4 book ai didi

ios - 尝试获取视频文件的大小 (kb) 时出错

转载 作者:行者123 更新时间:2023-11-30 11:11:22 27 4
gpt4 key购买 nike

在我的应用程序中,显示照片中的所有视频。选择视频后,将使用 avplayer 播放该视频。但是当我尝试获取所选视频文件的大小(kb)时,它显示错误。当我尝试复制视频文件时出现同样的错误。

我已获取这些权限:

<key>NSCameraUsageDescription</key>
<string>App needs to access camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>App need to access microphone</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App needs to access photos and videos</string>

获取大小的代码:

func fileSize(forURL url: Any) -> Double {

var fileURL: URL?
var fileSize: Double = 0.0

if url is URL || url is String {

if url is URL {
fileURL = url as? URL
}
else {
fileURL = URL(fileURLWithPath: url as! String)
}
var fileSizeValue = 0.0

do{
try fileSizeValue = (fileURL?.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! Double?)!
if fileSizeValue > 0.0 {
fileSize = (Double(fileSizeValue) / (1024 * 1024))
}

} catch {

print("file size error: \(error)")
}
}
return fileSize
}

let fileSize = fileSize(forURL: selectedVideos[0] as Any)

video file url: "file:///var/mobile/Media/DCIM/107APPLE/IMG_7728.MOV"

Error: Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_7728.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///var/mobile/Media/DCIM/107APPLE/IMG_7728.MOV, NSFilePath=/var/mobile/Media/DCIM/107APPLE/IMG_7728.MOV, NSUnderlyingError=0x1c445ef90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

编辑: 我已将 url 更改为路径。但仍然显示错误:

   let selectedFilePath = (selectedVideos[0] as! URL).path
print("video path: \(selectedFilePath)")
do{
let attributes = try FileManager.default.attributesOfItem(atPath: selectedFilePath)
let fileSize = attributes[.size] as! NSNumber
print("file size: \(fileSize)")
} catch {
print("file size error: \(error)")
}

let filePath = get_directory().appendingPathComponent("video\(CACurrentMediaTime()).MOV")
print("copy to path: \(filePath.path)")
do {
try FileManager.default.copyItem(atPath: selectedFilePath, toPath: filePath.path)

} catch {

print("copy error: \(error)")
}

错误:

video path: /var/mobile/Media/DCIM/107APPLE/IMG_7789.MOV

file size error: Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_7789.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/107APPLE/IMG_7789.MOV, NSUnderlyingError=0x1c0259530 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

copy to path: /var/mobile/Containers/Data/Application/5AAD29BD-489D-4737-AA98-E6131EFBA94D/Documents/addMusicToVideos/voices/video1171047.69338271.MOV

copy error: Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_7789.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/107APPLE/IMG_7789.MOV, NSUnderlyingError=0x1c4859830 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

最佳答案

使用此 AVURLAsset 扩展程序,您可以轻松地从 URL 获取视频文件的大小。确保导入 AVFoundation 框架。代码如下:

extension AVURLAsset {
var fileSize: Int? {
let keys: Set<URLResourceKey> = [.totalFileSizeKey, .fileSizeKey]
let resourceValues = try? url.resourceValues(forKeys: keys)

return resourceValues?.fileSize ?? resourceValues?.totalFileSize
}
}

此扩展以 Int(字节)形式返回视频大小。您可以根据需要修改单位。要调用它,请使用以下方法:

let asset = AVURLAsset(url: urlOfYourAsset)
print(asset.fileSize ?? 0)

您没有提供足够的信息。问题出在你的文件系统上。在 iOS 中将视频文件放在哪里很重要,以及如何获取 URL,这就是问题所在。也许您的 URL 在沙箱中是不允许的。请进一步学习并阅读Apple Documentation .

关于ios - 尝试获取视频文件的大小 (kb) 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52153775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com