Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AV Foundation

nakajijapan
February 01, 2014

AV Foundation

心霊動画アプリ「もう一度ご覧いただこう」を作成してみて

Conference With Developers 2014

nakajijapan

February 01, 2014
Tweet

More Decks by nakajijapan

Other Decks in Technology

Transcript

  1. // Assetੜ੒ AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil]; !

    // CompositionTrackੜ੒ AVMutableCompositionTrack* compositionVideoTrack; compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; ! // Ξηοτ͔ΒτϥοΫΛऔಘ AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; ! // ઃఆ [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoTrack atTime:self.currentTimeDuration error:nil]; [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // Assetੜ੒ [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:self.currentTimeDuration error:nil]; self.currentTimeDuration = [mixComposition duration]; ! // Layer Instructionͷ௥Ճ AVMutableVideoCompositionLayerInstruction *layerInstruction; layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; ! [self.layerInstructions addObject:layerInstruction]; ϝσΟΞ৘ใΛอ࣋͠ɺͦͷ৘ใΛऔಘͰ͖Δ
  2. // Assetੜ੒ AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil]; !

    // CompositionTrackੜ੒ AVMutableCompositionTrack* compositionVideoTrack; compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; ! // Ξηοτ͔ΒτϥοΫΛऔಘ AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; ! // ઃఆ [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoTrack atTime:self.currentTimeDuration error:nil]; [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // Assetੜ੒ [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:self.currentTimeDuration error:nil]; self.currentTimeDuration = [mixComposition duration]; ! // Layer Instructionͷ௥Ճ AVMutableVideoCompositionLayerInstruction *layerInstruction; layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; ! [self.layerInstructions addObject:layerInstruction]; ө૾ΛऔΓग़͢ Ի੠ΛऔΓग़͢
  3. // Assetੜ੒ AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil]; !

    // CompositionTrackੜ੒ AVMutableCompositionTrack* compositionVideoTrack; compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; ! // Ξηοτ͔ΒτϥοΫΛऔಘ AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; ! // ઃఆ [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoTrack atTime:self.currentTimeDuration error:nil]; [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // Assetੜ੒ [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:self.currentTimeDuration error:nil]; self.currentTimeDuration = [mixComposition duration]; ! // Layer Instructionͷ௥Ճ AVMutableVideoCompositionLayerInstruction *layerInstruction; layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; ! [self.layerInstructions addObject:layerInstruction]; ίϯϙδγϣϯΛ૊ΈཱͯΔ
  4. // generate parent layer CALayer* layerRoot = [CALayer layer]; CALayer*

    layerVideo = [CALayer layer]; . . . . videoComp = [AVMutableVideoComposition videoComposition]; videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:layerVideo inLayer:layerRoot]; $"-BZFSʹ$"#BTJD"OJNBUJPOͰ ઃఆͨ͠ΞχϝʔγϣϯΛ௥Ճ͢Δ
  5. // Assetੜ੒ AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil]; //

    CompositionTrackੜ੒ AVMutableCompositionTrack* compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; // Trackऔಘ AVAssetTrack* videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; ! // ઃఆ CMTime timeDuration; timeDuration = videoAsset.duration; ! [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, timeDuration) ofTrack:videoTrack atTime:kCMTimeZero error:nil]; [compositionVideoTrack setPreferredTransform:[videoTrack preferredTransform]]; AVMutableVideoCompositionLayerInstruction* layerInstruction; layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; [layerInstruction setTransform:CGAffineTransformConcat(scale, trnsration) atTime:kCMTimeZero]; ! [self.layerInstructions addObject:layerInstruction]; ΞϑΟϯม׵
  6. // શମʹରͯ͠εϩʔϞʔγϣϯॲཧΛ͔͚Δ CMTime durationSlow = CMTimeSubtract(videoAsset.duration, CMTimeMake(9, 1)); [mixComposition scaleTimeRange:CMTimeRangeMake(CMTimeMake(19,

    1), durationSlow) toDuration:CMTimeMake(durationSlow.value * 2.0, durationSlow.timescale)]; ࣮ࡍͷө૾Λ̎ഒͷ࣌ؒͰग़ྗͤ͞Δ