Question

Im using the code found [here][1] to attach image on video taken using UIImagePickerController.

the video is in portrait and playing fine but once I use AVURLASSet it turning landscape orientation instead of portrait and I cant find why?

Can any one point me to the right direction ?

My Code:

-(IBAction)addWaterMark:(id)sender {
 AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:tempPath] options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo  preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                               ofTrack:clipVideoTrack
                                atTime:kCMTimeZero error:nil];

[compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 

CGSize videoSize = [videoAsset naturalSize]; 
NSLog(@"%f %f",videoSize.width,videoSize.height);
}

at this point I get 480,360 instead of the correct size for temppath

Was it helpful?

Solution

found detailed tutorial describing how to edit and manipulate videos with AVfoundation introduction here

If they are landscape, we can use the naturalSize property we are supplied with, but if they are portrait, we must flip the the naturalSize so that the width is now the height and vice-versa

OTHER TIPS

I left the correct code here in case others need although you've chosen the answer of yourself as the correct one.

//readout the size of video
AVAssetTrack *vT = nil;
if ([[asset tracksWithMediaType:AVMediaTypeVideo] count] != 0)
{
    vT = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
}
if (vT != nil)
{
    orgWidth = vT.naturalSize.width;
    orgHeight = vT.naturalSize.height;
}

//check the orientation
CGAffineTransform txf = [vT preferredTransform];
if ((width == txf.tx && height == txf.ty)|(txf.tx == 0 && txf.ty == 0))
{
    //Landscape
}
else
{ 
    //Portrait
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top