问题
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