Du bist nicht angemeldet.

Lieber Besucher, herzlich willkommen bei: OS X Entwicklerforum. Falls dies dein erster Besuch auf dieser Seite ist, lies bitte die Hilfe durch. Dort wird dir die Bedienung dieser Seite näher erläutert. Darüber hinaus solltest du dich registrieren, um alle Funktionen dieser Seite nutzen zu können. Benutze das Registrierungsformular, um dich zu registrieren oder informiere dich ausführlich über den Registrierungsvorgang. Falls du dich bereits zu einem früheren Zeitpunkt registriert hast, kannst du dich hier anmelden.

1

Mittwoch, 25. April 2012, 15:02

CALayer Export

Hallo,

ich habe folgende Aufgabe:

Ich will eine CALayer Animation (mit Bildern) mit einem bereits vorhandenem Video kombinieren, sprich die CALayer Animation auf das andere Video drauflegen.
Das CALayer wird zwar exportiert, allerdings wird die Animation nicht abgespielt...

Hier mal der Code:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
CALayer *aLayer = [CALayer layer];
    UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png" inDirectory:@"Pack_1"]];
    [aLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
   
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    NSMutableArray *images = [NSMutableArray array];
    NSArray *imagePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Pack_1"];
    
    for (int i = 1; i <= [imagePaths count]; i++) {
        
        [images addObject:(id)[[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", i] ofType:@"png" inDirectory:@"Pack_1"]] CGImage]];
    }
    
    [anim setDuration:10.0];
    [anim setValues:images];
    
    [aLayer addAnimation:anim forKey:@"contents"];

    AVURLAsset *url = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"mov"]] options:nil];
    AVMutableComposition *videoComposition = [AVMutableComposition composition];
    
    NSError *error = nil;
    
    //VIDEO TRACK
    AVMutableCompositionTrack *compositionVideoTrack = [videoComposition  addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVAssetTrack *clipVideoTrack = [[url tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];
    
    //AUDIO TRACK
    AVMutableCompositionTrack *compositionAudioTrack = [videoComposition  addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    AVAssetTrack *clipAudioTrack = [[url tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration])  ofTrack:clipAudioTrack atTime:kCMTimeZero error:&error];
    
    AVMutableVideoComposition *videoComp = [AVMutableVideoComposition videoComposition];
    videoComp.renderSize = clipVideoTrack.naturalSize;
    videoComp.frameDuration = CMTimeMake(1, 25);
    
    //COMBINE
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, videoComp.renderSize.width, videoComp.renderSize.height);
    videoLayer.frame = CGRectMake(0, 0, videoComp.renderSize.width, videoComp.renderSize.height);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:aLayer];
    
    videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
    
    ///Instruction
    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoComposition.duration);
    
    AVMutableVideoCompositionLayerInstruction *layerInstruction1 = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
    
    instruction.layerInstructions = [NSArray arrayWithObjects:layerInstruction1, nil];
    videoComp.instructions = [NSArray arrayWithObject:instruction];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy_MM_dd_HHmmss_zzz"];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destinationPathHighQ = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Merged_Video_High_%@.mov", [dateFormatter stringFromDate:[NSDate date]]]];
    
    AVAssetExportSession *exporterHighQ = [[AVAssetExportSession alloc] initWithAsset:videoComposition presetName:AVAssetExportPresetHighestQuality];
    [exporterHighQ setVideoComposition:videoComp];
    exporterHighQ.outputFileType = AVFileTypeQuickTimeMovie;
    exporterHighQ.outputURL = [NSURL fileURLWithPath:destinationPathHighQ];
        
	[exporterHighQ exportAsynchronouslyWithCompletionHandler:^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            switch (exporterHighQ.status) {
                case AVAssetExportSessionStatusCompleted:
                    [self exportDidFinish:exporterHighQ];
                    break;
                default:
                    break;
            }
        });
    }];


Hoffe ihr könnt mir weiterhelfen...

LG,
Lukas
-Carpe Diem-

Verwendete Tags

CALayer, Export

Social Bookmarks