Jordan H
Jordan H

Reputation: 55885

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

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
    }
}

Upvotes: 2

Views: 7683

Answers (2)

makkhokher
makkhokher

Reputation: 238

In Swift 4

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

Upvotes: 4

Elf Sundae
Elf Sundae

Reputation: 1615

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)

Upvotes: 4

Related Questions