Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Resizing/Cropping recorded video

What would be the best way to resize a user recorded video on the iphone?

I currently have the ability to get video data in two ways:

1) Get the file URL from UIImagePickerController after the video has been recorded

2) Get the frames of the video as its being sent by a AVCaptureSession

For 1) I would need something which can read .mov h.264 files and spit out individual frames. Is there such a thing?

For 2) I thought about getting a UIImage for each frame, resizing the image and then recompiling a video with something like AVAssetWriter. But this seems like a very intensive operation and I'm wondering if there's a better way to approach this problem.

Is the general idea of resizing videos to resize each individual frame and then recompile a video? Or is there a way to directly resize an entire video?

I just basically need the user to record a video, and then to resize that video to 320x320, no fancy editing.

Thanks for any help you can provide.

Edit: Maybe 'resize' is the wrong word. I really just need to crop the video so that it goes from 480x360 to 320x320. Also this cropping doesn't need to happen in real time, I can do it once the video has been recorded.

like image 617
nebs Avatar asked Feb 14 '11 22:02

nebs


People also ask

Can you change iPhone video aspect ratio?

Crop the iPhone video. Click the Crop button and enter into the editing window. Click Enable Crop. In the Preset options, you can select a specific aspect ratio like 16:9, 4:3, 1:1 and start cropping, or select Free to manually crop iPhone videos to any customized aspect ratio of any displays.


2 Answers

The perfect way is to use AVAssetReader to read the video file and use AVAssetWriter to write this video file to a new video file again. you can set a outputSetting for writer input:

NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  AVVideoCodecH264, AVVideoCodecKey,
                                  [NSNumber numberWithInt: 480], AVVideoWidthKey,
                                  [NSNumber numberWithInt: 480], AVVideoHeightKey,
                                      AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey,
                                  nil];
        NSLog(@"%@",[assetVideoTrack mediaType]);
assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType: [assetVideoTrack mediaType]
                                                                   outputSettings:settings];
        [assetWriter addInput:assetWriterVideoInput];

you search apple developer documentation to find guid line about AVAssetReader

like image 35
Jason JX Avatar answered Oct 13 '22 00:10

Jason JX


In case anyone runs across this I have found you can do this with AVFoundation if you us an AVMutableVideoComposition as well as an AVMutableComposition. You then set renderSize on the video composition and that will effect the output size of the video. You then use AVMutableVideoCompositionLayerInstruction to position the video where you want it. There is a good article here on getting started with all these classes and his example project has commented out code at the end for exporting it.

like image 99
pixelrevision Avatar answered Oct 13 '22 00:10

pixelrevision