Thursday, September 30, 2010

Create Movie Using AVFoundation

Steps to Create Movie Using AVFoundation:-
1. Create a AVMutableComposition
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent(); //Debug purpose - used to calculate the total time taken
NSError *error = nil;
AVMutableComposition *saveComposition = [AVMutableComposition composition];
2. Get the video and audio file path
NSString *tempPath = NSTemporaryDirectory();
NSString *videoPath = nil ;//<Video file path>;
NSString *audioPath = nil ;//<Audio file path>;;
3. Create the video asset 
NSURL * url1 = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *video = [AVURLAsset URLAssetWithURL:url1 options:nil];
[url1 release];
4. Get the AVMutableCompositionTrack for video and add the video track to it.
The method insertTimeRange: ofTrack: atTime: decides the what portion of the video to be added and also where the video track should appear in the final video created.
AVMutableCompositionTrack *compositionVideoTrack = [ addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [video duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
NSLog(@"%f %@",CMTimeGetSeconds([video duration]),error);

5. Create the Audio asset 
NSURL * url2 = [[NSURL alloc] initFileURLWithPath:audioPath];
AVURLAsset *audio = [AVURLAsset URLAssetWithURL:url2 options:nil];
[url2 release];

6. Get the AVMutableCompositionTrack for audio and add the audio track to it.
AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration])  ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
NSLog(@"%f %@",CMTimeGetSeconds([audio duration]),error);
7. Get file path for of the final video.
NSString *path = [tempPath stringByAppendingPathComponent:@"mergedvideo.mov"];
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

8. Create the AVAssetExportSession and set the preset to it.
The completion handler will be called upon the completion of the export.
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] autorelease];
exporter.outputURL=url;
exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"export completed : %lf",CFAbsoluteTimeGetCurrent()-currentTime);
}];

The above technique can be used to merge N number of video and audio tracks and create a movie. I hope this tutorial helps to create movie using AVFoundation.


Saturday, August 28, 2010

Integrating google analytics into Blogger

It's very important to keep track of the traffic that your site receives. So, Google Analytics the way to go.It's very simple to integrate Google analytics into Blogger.Follow these steps
  1. Sign into Google Analytics
  2. create your profile.
  3. fill the details like your blogger URL, time zone etc
  4. hit continue and Copy the code
  5. Sign into blogger
  6. Click on the 'Layout' Tab, then click on 'Edit HTML'
  7. Paste the code between the header tag as shown . <head>........ paste your code here </head>
  8. Press 'Save Template' to save the changes.
  9. Back in Google Analytics, click on 'Check Status' or 'Verify Tracking Code' under the Status column. Once your tracking code has been verified, the status will change to: 'Receiving Data.'