Search code examples
ios8alassetslibraryphasset

Re-render video using the new Photos Framework in iOS8


I need to be able take a video from Photos and re-rendering, both clipping it in time, changing the width and height, and frame rate. Certainly I need to start with:

    PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];

    [self.asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {

        // Get full image

        NSURL *url = [contentEditingInput fullSizeImageURL];

    }];

And I should be able to adjust width, height and duration. Grab an NSData from that, write that out to the file syset.m

But the url is nil, which implies to me that I can't edit videos with the new Photos framework. (ALAsset didn't have a problem with this using AVAssetExportSession.) This makes sense since the Apple Dev sample code can't edit videos either.

Now, to make life easier I could just pass that url to an AVAssetExportSession but I can't, because it is nil. If I just modified width, height and duration I'd still need to grab an NSData from it, write that out to the file system.

I do not need to write the modified video back to Photos, I actually need the video on the file system since I'll be uploading it to our servers.


Solution

  • fullSizeImageURL is for working with Photo assets. You want the avAsset property when working with a video. Modify the actual video, not the metadata, by writing a new video file.

    To do that, you could use that avAsset in an AVMutableComposition:

    Insert the appropriate time range of the avAsset's video track (AVAssetTrack) into an AVMutableCompositionTrack. That'll do your trimming.

    Place/size it appropriately using layer instructions. (AVMutableVideoCompositionLayerInstruction) to do your cropping and scaling.