What is the best way to transcode video to objective-c (iOS)?

I would like, depending on the device and settings in my application, transcode the video into a specific video format. For example, if a user has an iPhone 4S and you choose the average settings in my application, I would like to convert the video to 540p before starting processing. If he chooses high, then I would like to transcode to 720p.

I could read the video frame by frame, resize and save to disk, but this does not seem very efficient. What would be the easiest and fastest way to transcode a video that I can submit to my video processing libraries?

I tried using the videoQuality settings on my UIImagePickerController, but it doesn't seem to work even when I set it to UIImagePickerControllerQualityTypeIFrame960x540, my video comes out as 720p (640x480 works, but I need to be more granular).

+6
source share
1 answer

You might want to look at AVAssetExportSession, which makes it simple enough to re-encode the video. I think this is also hardware supported when possible, like the rest of AVFoundation:

https://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/01_UsingAssets.html

Please note that it will never make the video larger than it is, so you are not guaranteed to get the required output size. The following code may be the beginning for what you want if you have an ALAsset instance:

- (void)transcodeLibraryVideo:(ALAsset *)libraryAsset toURL:(NSURL *)fileURL withQuality:(NSString *quality) { // get a video asset for the original video file AVAsset *asset = [AVAsset assetWithURL: [NSURL URLWithString: [NSString stringWithFormat:@"%@", [[libraryAsset defaultRepresentation] url]]]]; // see if it possible to export at the requested quality NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; if ([compatiblePresets containsObject:quality]) { // set up the export AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:quality]; exportSession.outputURL = fileURL; exportSession.outputFileType = AVFileTypeQuickTimeMovie; // run the export [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch ([exportSession status]) { case AVAssetExportSessionStatusFailed: //TODO: warn of failure break; case AVAssetExportSessionStatusCancelled: //TODO: warn of cancellation break; default: //TODO: do whatever is next break; } [exportSession release]; }]; } else { //TODO: warn that the requested quality is not available } } 

You would like to transfer the quality of AVAssetExportPreset960x540 for 540p and AVAssetExportPreset1280x720 for 720p, for example.

+3
source

Source: https://habr.com/ru/post/908824/


All Articles