Is it possible to make AVURLAsset work without a file extension?

zoul picture zoul · Feb 15, 2012 · Viewed 12k times · Source

I’m trying to create thumbnails for video files:

- (UIImage*) thumbnailForVideoAtURL: (NSURL*) videoURL
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    CGImageRef imageHandle = [generator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:NULL];
    if (imageHandle) {
        UIImage *frameImage = [UIImage imageWithCGImage:imageHandle];
        CFRelease(imageHandle);
        return frameImage;
    } else {
        return nil;
    }
}

The catch is that the video files are stored in a content-addressable store and have no extensions. This appears to throw AVURLAsset off, as the asset gets created, but upon reading the frame image I get the following error:

Error Domain=AVFoundationErrorDomain Code=-11828 "Cannot Open" UserInfo=0x167170 {NSLocalizedFailureReason=This media format is not supported., NSUnderlyingError=0x163a00 "The operation couldn’t be completed. (OSStatus error -12847.)", NSLocalizedDescription=Cannot Open}

Is this documented or mentioned somewhere? I can’t believe I’m really forced to pass the file format information through the file name. The options argument to the AVURLAsset initializer looks like a good place to supply the filetype, but according to the documentation there does not seem to be any support for that.

PS. I have tested the code, the same file with the correct extension produces the thumbnails just fine.

Answer

zoul picture zoul · Feb 19, 2012

In the end I temporarily create a hardlink to the file and give the correct extension to the hardlink. It’s a hack though, I’d like to see a better solution. I’ve submitted a bug report against AVURLAsset, hopefully Apple could add the feature to read the file format information from the options dictionary.