Creating a AVAsset with a HTTP NSURL

ε祈祈猫儿з 提交于 2019-12-03 20:40:28

@MichaelScaria, many thanks for posting what you figured out, i was on this for about 3 days. below is my solution in full when i was trying to get AVAssets from both local urls and remote urls

+ (AVAsset*)getAVAssetFromRemoteUrl:(NSURL*)url 
{   
    if (!NSTemporaryDirectory())
    {
       // no tmp dir for the app (need to create one)
    }

    NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
    NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"temp"] URLByAppendingPathExtension:@"mp4"];
    NSLog(@"fileURL: %@", [fileURL path]);

    NSData *urlData = [NSData dataWithContentsOfURL:url];
    [urlData writeToURL:fileURL options:NSAtomicWrite error:nil];

    AVAsset *asset = [AVAsset assetWithURL:fileURL];
    return asset;
}
+ (AVAsset*)getAVAssetFromLocalUrl:(NSURL*)url
{
    AVURLAsset *asset = [AVAsset assetWithURL:url];
    return asset;
}

I saved the online url to a temporary directory and used the temporary url to merge the video and it worked.

    NSData *urlData = [NSData dataWithContentsOfURL:initalURL];
    [urlData writeToFile:path options:NSAtomicWrite error:nil]

Maybe you need to use AVURLAsset or other subclasses instead? From the docs:

You often instantiate an asset using AVURLAsset—a concrete subclass of AVAsset—with NSURLs that refer to audiovisual media resources, such as streams (including HTTP live streams), QuickTime movie files, MP3 files, and files of other types. You can also instantiate an asset using other concrete subclasses that extend the basic model for audiovisual media in useful ways, as AVComposition does for temporal editing.

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