diff options
| author | Brian Jordan | 2012-03-28 22:16:10 -0400 |
|---|---|---|
| committer | Brian Jordan | 2012-03-28 22:16:10 -0400 |
| commit | bbe39772c3a7a45a2b70f780e103e8ce0302088b (patch) | |
| tree | ba987792639ee9171a519fa293a96831986476c7 | |
| parent | b90d45effc82e8f481220ba204db52b02e043022 (diff) | |
| download | Video-Tuneup-bbe39772c3a7a45a2b70f780e103e8ce0302088b.tar.bz2 | |
muxing working. Resolves #8
| -rw-r--r-- | Video Tuneup/SimpleEditor.h | 4 | ||||
| -rw-r--r-- | Video Tuneup/SimpleEditor.m | 25 | ||||
| -rw-r--r-- | Video Tuneup/ViewController.h | 1 | ||||
| -rw-r--r-- | Video Tuneup/ViewController.m | 14 |
4 files changed, 34 insertions, 10 deletions
diff --git a/Video Tuneup/SimpleEditor.h b/Video Tuneup/SimpleEditor.h index f505beb..dc9d8d3 100644 --- a/Video Tuneup/SimpleEditor.h +++ b/Video Tuneup/SimpleEditor.h @@ -56,6 +56,7 @@ // Assets AVURLAsset *_video; + CMTime _videoStartTime; AVURLAsset *_song; @@ -64,6 +65,7 @@ // Composition objects AVComposition *_composition; + AVComposition *_videoComposition; AVAudioMix *_audioMix; AVPlayerItem *_playerItem; // Reference to player of work-in-progress @@ -84,7 +86,7 @@ - (void)buildCompositionObjectsForPlayback:(BOOL)forPlayback; @property (nonatomic, retain) AVComposition *composition; -//@property (nonatomic, readonly, retain) AVVideoComposition *videoComposition; +@property (nonatomic, readwrite, retain) AVVideoComposition *videoComposition; @property (nonatomic, readwrite, retain) AVAudioMix *audioMix; @property (nonatomic, readwrite, retain) AVPlayerItem *playerItem; diff --git a/Video Tuneup/SimpleEditor.m b/Video Tuneup/SimpleEditor.m index 40e07f2..25daa4f 100644 --- a/Video Tuneup/SimpleEditor.m +++ b/Video Tuneup/SimpleEditor.m @@ -102,10 +102,25 @@ - (void)addVideoTrackToComposition:(AVMutableComposition *)composition { + AVAssetTrack *videoTrack = nil; + if ([[self.video tracksWithMediaType:AVMediaTypeVideo] count] > 0) + videoTrack = [[self.video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; + AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; CMTimeRange videoTimeRange = CMTimeRangeMake(self.videoStartTime, self.video.duration); -// [compositionVideoTrack insertTimeRange:videoTimeRange ofTrack: self.video atTime:videoTimeRange.start error:nil]; + [compositionVideoTrack insertTimeRange:videoTimeRange ofTrack:videoTrack atTime:videoTimeRange.start error:nil]; +} +- (void)addAudioTrackToComposition:(AVMutableComposition *)composition +{ + AVAssetTrack *audioTrack = nil; + if ([[self.video tracksWithMediaType:AVMediaTypeAudio] count] > 0) + audioTrack = [[self.video tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; + + AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; + CMTimeRange audioTimeRange = CMTimeRangeMake(self.videoStartTime, self.video.duration); + + [compositionAudioTrack insertTimeRange:audioTimeRange ofTrack:audioTrack atTime:audioTimeRange.start error:nil]; } - (void)buildCompositionObjectsForPlayback:(BOOL)forPlayback @@ -118,6 +133,7 @@ if (self.video) { [self addVideoTrackToComposition:composition]; + [self addAudioTrackToComposition:composition]; } if (self.song) { @@ -151,9 +167,10 @@ - (AVAssetExportSession*)assetExportSessionWithPreset:(NSString*)presetName { - AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:self.video presetName:presetName]; -// session.videoComposition = self.composition; - session.audioMix = self.audioMix; + AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:self.composition presetName:presetName]; + +// session.videoComposition = self.videoComposition; +// session.audioMix = self.audioMix; return session; } diff --git a/Video Tuneup/ViewController.h b/Video Tuneup/ViewController.h index 0f8279c..3eb2eb2 100644 --- a/Video Tuneup/ViewController.h +++ b/Video Tuneup/ViewController.h @@ -15,6 +15,7 @@ @interface ViewController : UIViewController { AVURLAsset *asset; + AVURLAsset *songAsset; } @property (nonatomic, retain) AVPlayer *player; diff --git a/Video Tuneup/ViewController.m b/Video Tuneup/ViewController.m index 4e01e93..7715baa 100644 --- a/Video Tuneup/ViewController.m +++ b/Video Tuneup/ViewController.m @@ -32,19 +32,22 @@ static const NSString *ItemStatusContext; playButton.enabled = NO; NSLog(@"Play button disabled"); } - } - (IBAction)loadAssetFromFile:sender { - NSLog(@"Loading asset."); + NSLog(@"Loading asset."); NSURL *fileURL = [[NSBundle mainBundle] - URLForResource:@"airplane" withExtension:@"m4v"]; - + URLForResource:@"sample_iPod" withExtension:@"m4v"]; + NSURL *songFileURL = [[NSBundle mainBundle] + URLForResource:@"song" withExtension:@"mp3"]; + asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; + songAsset = [AVURLAsset URLAssetWithURL:songFileURL options:nil]; NSLog(@"Asset duration is %f", CMTimeGetSeconds([asset duration])); - + NSLog(@"Song asset duration is %f", CMTimeGetSeconds([songAsset duration])); + NSString *tracksKey = @"tracks"; [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler: @@ -120,6 +123,7 @@ static const NSString *ItemStatusContext; // Initialize video editor self.editor = [[SimpleEditor alloc] init]; self.editor.video = asset; + self.editor.song = songAsset; // Begin export [self.editor buildCompositionObjectsForPlayback:NO]; |
