rs / SDAVAssetExportSession

AVAssetExportSession drop-in replacement with customizable audio&video settings
MIT License
805 stars 212 forks source link

Hyperlapse videos #22

Closed olivej closed 7 years ago

olivej commented 9 years ago

i am using the latest - 0.0.2. i am trying to transcode videos to a smaller size to upload to a server. working great with videos taken from the built-in camera, regardless of orientation - i swap my width and height based on a check i do to see whether video was taken in portrait/landscape.

the only use case this doesn't work with is when i use source videos that were filmed with Instagram's Hyperlapse app. for some reason that i can't figure out, the transforms for those videos are slightly different than the ones from the built-in camera. the a,b,c,d values match up when filmed in the different orientations, but the tx and ty translation values are not the same as when filmed using the built-in camera app. for example, the built-in camera app, when filming in landscape with the home button on the left, produces a transform that looks like this : transform a = -1.0 , b = 0.0 , c = 0.0 , d = -1.0 , tx = 640.0 , ty = 360.0 but, when filmed using Hyperlapse, the same orientation, the transform looks like this : transform a = -1.0 , b = 0.0 , c = 0.0 , d = -1.0 , tx = 0.0 , ty = 0.0 the translations are always set to 0, and this produces a black video when i transcode.

if i manually set the tx and ty values in the "buildDefaultVideoComposition" when assigning the transform to the passThroughLayer, then it works fine.

any ideas? could this be a bug in the transform for the Hyperlapse videos or am i missing something? thanks.

officiallyrad commented 9 years ago

Seeing the same thing, did you find a solution @olivej?

olivej commented 9 years ago

Hi @officiallyrad , sorry for late reply. I still think there is an error in how Hyperlapse is creating their transforms. So, the only solution I came up with was to check for incorrect transform numbers and correct them. I do this in the "buildDefaultVideoComposition" method of SDAVAssetExportSession.m. Remove the line that sets the transform on the pass through layer and then replace with this -

    CGAffineTransform transform = videoTrack.preferredTransform;

    // HELPME - fix Hyperlapse videos....geez
    if(transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0) {
        // portrait, home button at bottom (upright)
        DLog(@"Portrait, home button at bottom");
        if( (transform.tx == 0) && (transform.ty == 0) ) {
            transform.tx = videoTrack.naturalSize.height;
            transform.ty = 0;
        }
    }
    else if(transform.a == 0 && transform.b == -1.0 && transform.c == 1.0 && transform.d == 0) {
        // portrait, home button at top (upside down)
        DLog(@"Portrait, home button at top");
        if( (transform.tx == 0) && (transform.ty == 0) ) {
            transform.tx = 0;
            transform.ty = videoTrack.naturalSize.width;
        }
    }
    else if(transform.a == 1.0 && transform.b == 0 && transform.c == 0 && transform.d == 1.0) {
        // landscape, home button on right
        // do nothing
        DLog(@"Landscape, home button at right");
    }
    else if(transform.a == -1.0 && transform.b == 0 && transform.c == 0 && transform.d == -1.0) {
        // landscape, home button on left
        DLog(@"Landscape, home button at left");
        if( (transform.tx == 0) && (transform.ty == 0) ) {
            transform.tx = videoTrack.naturalSize.width;
            transform.ty = videoTrack.naturalSize.height;
        }
    }

    [passThroughLayer setTransform:transform atTime:kCMTimeZero];
ghost commented 9 years ago

@olivej I also had that issue, thanks for sharing your solution!

shahen94 commented 7 years ago

@olivej thank you ! your solution helped me