Any way to get the resource name of an existing AVAsset?

。_饼干妹妹 提交于 2019-12-24 05:45:17

问题


I am interested in obtaining the resource name (aka filename) of an AVAsset, specifically an mp4 video. The asset was created with the resource name, I simply want to obtain that same string resource in the future when I have only the AVAsset instance. In this case how can I obtain "my-video" from the asset?

AVAsset(URL: NSBundle.mainBundle().URLForResource("my-video", withExtension: "mp4")!)

I am able to obtain the asset's title, but this is not always the same as the resource name. And the filename is not in the asset's common metadata.

var title = ""
for metadataItem in asset.commonMetadata {
    if metadataItem.commonKey == "title" {
        title = metadataItem.value as! String
    }
}

回答1:


You can use AVURLAsset.

NSURL *url = [[NSBundle mainBundle] URLForResource:@"my-video" withExtension:@"mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSLog(@"filename: %@", asset.URL.lastPathComponent);

Sorry I don't known much about Swift, it may be:

let url:NSURL = NSBundle.mainBundle().URLForResource("my-video", withExtension: "mp4")!
let asset:AVURLAsset = AVURLAsset.init(URL: url, options: nil)
let filename:NSString = asset.URL.lastPathComponent!
print(filename)



回答2:


In Swift 4

let assetURL = avAsset as! AVURLAsset
let name = assetURL.url.lastPathComponent


来源:https://stackoverflow.com/questions/36380679/any-way-to-get-the-resource-name-of-an-existing-avasset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!