Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resize Video Frame / Crop Video Frame ?Please tell me #18

Open
NeerajBhakuni1410 opened this issue Dec 2, 2013 · 6 comments
Open

Comments

@NeerajBhakuni1410
Copy link

I have issue regarding Final Output video Frame size. I just want that 320 x 320 video frame size. Rest of them will get removed.

@xissburg
Copy link
Owner

xissburg commented Dec 2, 2013

What are you doing exactly? Do you want to crop the video? You can do that using AVFoundation http://stackoverflow.com/questions/5198245/how-do-i-use-avfoundation-to-crop-a-video

@NeerajBhakuni1410
Copy link
Author

Actually that link does not crop the video, it just shrink the video frame.
I want to crop the Video , Just like Instagram and vine application did.

@xissburg
Copy link
Owner

xissburg commented Dec 2, 2013

You transform the video frame effectively cropping it. Just scale it up.

@NeerajBhakuni1410
Copy link
Author

I am Using that code for merge all Video Clips. Can u Please Tell me What i have to do for crop the video frame from center with frame size 320 x 320.

Please Help me.
I really appreciate you.

  • (AVMutableComposition* )mergeAllVideoClip:(NSArray* )videoFilePaths orientation:(NSNumber*)orientation
    {
    AVMutableComposition
    composition = [AVMutableComposition composition];

    AVMutableCompositionTrack* videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    AVMutableCompositionTrack* audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    // Make it square
    AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.renderSize = CGSizeMake(320, 320);
    videoComposition.frameDuration = CMTimeMake(1, 30);

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

    // Merge All Track.
    NSError* error = nil;
    NSMutableArray* timeVideoRanges = [NSMutableArray arrayWithCapacity:videoFilePaths.count];
    NSMutableArray* videoTracks = [NSMutableArray arrayWithCapacity:videoFilePaths.count];

    //audio files
    NSMutableArray* timeAudioRanges = [NSMutableArray arrayWithCapacity:videoFilePaths.count];
    NSMutableArray* audioTracks = [NSMutableArray arrayWithCapacity:videoFilePaths.count];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil];

    for (NSURL* videoPathUrl in videoFilePaths)
    {
    AVURLAsset *assetClip = [AVURLAsset URLAssetWithURL:videoPathUrl options:options];

    AVAssetTrack *clipVideoTrack = [[assetClip tracksWithMediaType:AVMediaTypeVideo] lastObject];
    AVAssetTrack *audioAssetTrack = [[assetClip tracksWithMediaType:AVMediaTypeAudio] lastObject];
    
    if (clipVideoTrack == nil || audioAssetTrack == nil)
        continue;
    
    // rotate to portrait
    AVMutableVideoCompositionLayerInstruction* compositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
    
    CGAffineTransform videoTransform = clipVideoTrack.preferredTransform;
    
    if(videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0)  {
        DebugLog(@"right");
    
        *orientation = [NSNumber numberWithInteger:UIImageOrientationRight];
    
        CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
        videoCompositionTrack.preferredTransform = rotationTransform;
    }
    if(videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0)  {
        DebugLog(@"left");
    
        *orientation = [NSNumber numberWithInteger:UIImageOrientationLeft];
    }
    if(videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0)   {
        DebugLog(@"up");
        *orientation = [NSNumber numberWithInteger:UIImageOrientationUp];
    }
    if(videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {
        DebugLog(@"down");
        *orientation = [NSNumber numberWithInteger:UIImageOrientationDown];
    }
    
    
    instruction.layerInstructions = [NSArray arrayWithObject:compositionLayerInstruction];
    videoComposition.instructions = [NSArray arrayWithObject: instruction];
    
    // Merge Video Files
    [timeVideoRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
    [videoTracks addObject:clipVideoTrack];
    
    // Merge Audio Files
    [timeAudioRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
    [audioTracks addObject:audioAssetTrack];
    

    }

    BOOL compositionTrack = [videoCompositionTrack insertTimeRanges:timeVideoRanges ofTracks:videoTracks atTime:kCMTimeZero error:&error];
    DebugLog(@"compositionTrack :%d == error :%@",compositionTrack,error);

    BOOL audiocompositionTrack = [audioCompositionTrack insertTimeRanges:timeAudioRanges ofTracks:audioTracks atTime:kCMTimeZero error:&error];
    DebugLog(@"audiocompositionTrack :%d == error :%@",audiocompositionTrack,error);

    return composition;
    }

@NeerajBhakuni1410
Copy link
Author

Can you tell me how to use CGAffineTransform for cropping the video from the center with frame size 320 x 320

CGAffineTransform finalTransform = // setup a transform that grows the video, effectively causing a crop
[transformer setTransform:finalTransform atTime:kCMTimeZero];

@xissburg Please Help.
Thanks

@antonyjoemathew
Copy link

@NeerajBhakuni1410 Hi I am also facing the same issue.Are you able to crop the image.? if so can you please share your code
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants