Created
April 26, 2022 11:00
-
-
Save SoundBlaster/3bd4e164934d0816d9698017c03fc3cf to your computer and use it in GitHub Desktop.
Create empty video by code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)exportVideo | |
{ | |
CGSize renderingSize = CGSizeMake(640, 360); // The desired size of your video | |
float displayDuration = 2.0f; //The duration of the desired video, in seconds | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]]; | |
NSURL *url = [NSURL fileURLWithPath:myPathDocs]; | |
NSError *error; | |
[[NSFileManager defaultManager] removeItemAtURL:url error:&error]; | |
mutableComposition = [AVMutableComposition composition]; | |
mutableCompositionVideoTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; | |
videoComposition = [AVMutableVideoComposition videoComposition]; | |
videoComposition.renderSize = renderingSize; | |
videoComposition.frameDuration = CMTimeMake(1, 30); | |
CALayer *parentLayer = [CALayer layer]; | |
CALayer *videoLayer = [CALayer layer]; | |
parentLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height); | |
videoLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height); | |
[parentLayer addSublayer:videoLayer]; | |
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; | |
NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_1080p" ofType:@"mp4"]; | |
NSURL *trackUrl = [NSURL fileURLWithPath:path]; | |
AVAsset *asset = [AVAsset assetWithURL:trackUrl]; | |
AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
[mutableCompositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,CMTimeMakeWithSeconds(displayDuration, 600)) ofTrack:track atTime:kCMTimeZero error:nil]; | |
CALayer *imageLayer = [CALayer layer]; | |
imageLayer.bounds = parentLayer.frame; | |
imageLayer.anchorPoint = CGPointMake(0.5, 0.5); | |
imageLayer.position = CGPointMake(CGRectGetMidX(imageLayer.bounds), CGRectGetMidY(imageLayer.bounds)); | |
imageLayer.backgroundColor = [UIColor blueColor].CGColor; | |
imageLayer.contentsGravity = kCAGravityResizeAspectFill; | |
[parentLayer addSublayer:imageLayer]; | |
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; | |
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(displayDuration, 600)); | |
videoComposition.instructions = @[instruction]; | |
exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality]; | |
exporter.outputURL = url; | |
exporter.videoComposition = videoComposition; | |
exporter.outputFileType= AVFileTypeMPEG4; | |
exporter.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(displayDuration, 600)); | |
exporter.shouldOptimizeForNetworkUse = YES; | |
[exporter exportAsynchronouslyWithCompletionHandler:^(void){ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self exportDidFinish:exporter]; | |
}); | |
}]; | |
} | |
// https://stackoverflow.com/a/21001173/602249 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment