7

Instagram 비디오 자르기 기능을 구현하려고합니다. 이를 위해 정확한 오리엔테이션 비디오를 올리면 내 MPMoviePlayerController보기가 올바른 종횡비로 표시되고 사용자는 비디오를 팬 할 수 있고 표시된 정사각형 비디오는 동작시 잘릴 것입니다.PhotoLibrary에서 동영상 오리엔테이션을 올바르게 얻는 방법 iOS

비디오의 올바른 방향을 잡는 데 문제가 있습니다. 나는 다음과 같은 코드를 몇 번 코드의 조각 위에, 세로 실제로 비디오 반대 가로로 분류되고 있다는 점이다 난 데

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil]; 
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo]; 
AVAssetTrack *track = [tracks objectAtIndex:0]; 
UIInterfaceOrientation orientation =[self orientationForTrack:track]; 

// method to get orientation 
- (UIInterfaceOrientation)orientationForTrack:(AVAssetTrack *)videoTrack 
{ 
    CGSize size = [videoTrack naturalSize]; 
    CGAffineTransform txf = [videoTrack preferredTransform]; 

    if (size.width == txf.tx && size.height == txf.ty) 
     return UIInterfaceOrientationLandscapeRight; 
    else if (txf.tx == 0 && txf.ty == 0) 
     return UIInterfaceOrientationLandscapeLeft; 
    else if (txf.tx == 0 && txf.ty == size.width) 
     return UIInterfaceOrientationPortraitUpsideDown; 
    else 
     return UIInterfaceOrientationUnknown; 
} 

문제를 사용하고 있습니다. 비디오의 방향이 두 가지 인 경우 (가로 또는 세로) 만 알면됩니다. MPMoviePlayerController가 해석하고있는 정확한 동일한 방향 값을 원합니다. 내 응용 프로그램은 세로 방향만 지원합니다. 어떤 도움을 주시면 감사하겠습니다.

PS : 위 코드는 아이폰 카메라에서 캡처 한 비디오, whatsapp를 통해 다운로드하거나 공유 한 비디오가 문제를 일으키는 경우 잘 작동합니다.

+0

이 링크는 도움이 될 것입니다. https://gist.github.com/lukabernardi/5020724 –

+0

아니요. 나는이 문제를 해결했고, 나는 약간의 시간을 얻을 때 해결책을 업로드 할 것이다. 당신의 도움을 주셔서 감사합니다. – Usama

+0

확인. 다른 사람이 도움을 얻을 수 있도록 넣어주세요 .... :) –

답변

5

유일한 방법 저를 위해이 문제를 해결하는 방법을 사용하여 비디오에서 썸네일 이미지를 캡처하는 것입니다

if (singleFrameImage.size.height > singleFrameImage.size.width) { 
    // potrait video 
} else { 
    // landscape video 
} 

: 같은 썸네일 이미지에 체크를 추가 한 후

- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option 

및 이 솔루션은 iPhone 카메라에서 캡처 한 비디오와 인터넷에서 무작위로 다운로드 한 비디오 세트 (8-10)에서 테스트됩니다.

4

AVAssetTrack은 속성이 preferredTransform입니다. 이를 사용하여 실제 비디오 방향을 찾을 수 있습니다.

 AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp; 
CGFloat videoAngle = RadiansToDegrees(atan2(videoAssetOrientation_.b, videoAssetOrientation_.a)); 


int orientation = 0; 
switch ((int)videoAngle) { 
    case 0: 
     orientation = UIImageOrientationRight; 
     break; 
    case 90: 
     orientation = UIImageOrientationUp; 
     break; 
    case 180: 
     orientation = UIImageOrientationLeft; 
     break; 
    case -90: 
     orientation = UIImageOrientationDown; 
     break; 
    default: 
     //Not found 
     break; 
} 
+0

이것은 iphone 카메라에서 캡처 한 비디오에서만 작동하며 인터넷에서 다운로드 한 임의의 비디오에서는 작동하지 않습니다. – Usama

+0

나는이 문제를 해결했다. 일단 자유 시간을 얻으면 해결책을 게시 할 것이다. 당신의 도움을 주셔서 감사합니다. – Usama

+0

제안 된 코드가 맞다고 생각하지만 동영상 애셋 크기에 해당 방향을 적용해야합니다. 나는 방향이 0 또는 180이면 isLandscape = width> height이지만 90 또는 -90이면 isLandscape = width markov

관련 문제