2011-03-01 48 views
8

我有一個簡單的使用AVPlayer的視頻編輯視圖。我使用一個或多個AVURLAsset創建一個AVMutableComposition。所有工作正常,但視頻始終旋轉90度。即如果源視頻是以縱向模式拍攝的。 AVPlayer以橫向模式顯示,反之亦然。我已經閱讀了文檔,並「死死」地找到了解決辦法。可能我正在尋找錯誤的地方。iPhone SDK。是否有可能使用AVURLlAsset/AVMutableComposition在AVplayer中旋轉視頻?

任何人都可以幫忙嗎?

在此先感謝;

讓 - 皮埃爾·

+0

觀看AVFoundation一個WWDC10會議上,我相信這是什麼地方的設置,但我需要再看一遍之後。我肯定會發布答案 – 2011-03-06 17:54:12

+1

你有沒有解決過這個問題?我有同樣的問題。 – 2012-02-02 06:49:06

+0

你解決了這個問題?我有同樣的問題,我不知道我必須做什麼 – 2015-08-18 08:16:13

回答

1

而無需代碼檢查,也很難知道是怎麼回事......但最有可能的,因爲你正在失去你的preferredTransform屬性的值,您的問題引起的。

全部AVAssetTrack對象具有preferredTransform屬性,用於指定是否應旋轉視頻。如果您正在創建新的AVMutableAssetTrack對象,則可能需要設置該屬性的值,以便視頻保持預期方向。

3

根據我的理解,您有一些方向問題,例如縱向視頻處於橫向模式,有時視頻會顛倒。這是由於默認的AVAsset方向。使用默認iPhone攝像頭應用程序錄制的所有影片和圖像文件都將視頻幀設置爲橫向,因此媒體將以橫向模式保存。 AVAsset具有preferredTransform屬性,其中包含媒體方向信息,並且只要您使用照片應用程序或QuickTime查看媒體文件,就會將其應用於媒體文件。 您可以通過將必要的變換應用於AVAsset對象,輕鬆地進行糾正。但是,由於您的兩個視頻文件可能有不同的方向,因此您需要使用兩個獨立的AVMutableCompositionTrack實例,而不是原來的一個(假設)。 創建兩個AVMutableCompositionTrack視頻軌跡 由於您現在有兩個單獨的AVMutableCompositionTrack實例,因此需要將AVMutableVideoCompositionLayerInstruction應用於每個軌道以便修復方向。因此,添加以下代碼

// Create AVMutableVideoCompositionInstruction 
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration)); 
// Create an AVMutableVideoCompositionLayerInstruction for the first track 
AVMutableVideoCompositionLayerInstruction *firstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack]; 
AVAssetTrack *firstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
UIImageOrientation firstAssetOrientation_ = UIImageOrientationUp; 
BOOL isFirstAssetPortrait_ = NO; 
CGAffineTransform firstTransform = firstAssetTrack.preferredTransform; 
if (firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0) { 
    firstAssetOrientation_ = UIImageOrientationRight; 
    isFirstAssetPortrait_ = YES; 
} 
if (firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) { 
    firstAssetOrientation_ = UIImageOrientationLeft; 
    isFirstAssetPortrait_ = YES; 
} 
if (firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) { 
    firstAssetOrientation_ = UIImageOrientationUp; 
} 
if (firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) { 
    firstAssetOrientation_ = UIImageOrientationDown; 
} 
[firstlayerInstruction setTransform:firstAsset.preferredTransform atTime:kCMTimeZero]; 
[firstlayerInstruction setOpacity:0.0 atTime:firstAsset.duration]; 
// Create an AVMutableVideoCompositionLayerInstruction for the second track 
AVMutableVideoCompositionLayerInstruction *secondlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:secondTrack]; 
AVAssetTrack *secondAssetTrack = [[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
UIImageOrientation secondAssetOrientation_ = UIImageOrientationUp; 
BOOL isSecondAssetPortrait_ = NO; 
CGAffineTransform secondTransform = secondAssetTrack.preferredTransform; 
if (secondTransform.a == 0 && secondTransform.b == 1.0 && secondTransform.c == -1.0 && secondTransform.d == 0) { 
    secondAssetOrientation_= UIImageOrientationRight; 
    isSecondAssetPortrait_ = YES; 
} 
if (secondTransform.a == 0 && secondTransform.b == -1.0 && secondTransform.c == 1.0 && secondTransform.d == 0) { 
    secondAssetOrientation_ = UIImageOrientationLeft; 
    isSecondAssetPortrait_ = YES; 
} 
if (secondTransform.a == 1.0 && secondTransform.b == 0 && secondTransform.c == 0 && secondTransform.d == 1.0) { 
    secondAssetOrientation_ = UIImageOrientationUp; 
} 
if (secondTransform.a == -1.0 && secondTransform.b == 0 && secondTransform.c == 0 && secondTransform.d == -1.0) { 
    secondAssetOrientation_ = UIImageOrientationDown; 
} 
[secondlayerInstruction setTransform:secondAsset.preferredTransform atTime:firstAsset.duration]; 
} 

最後添加說明,它只是應用於第二個軌道的方向修正。

mainInstruction.layerInstructions = [NSArray arrayWithObjects:firstlayerInstruction, secondlayerInstruction,nil]; 
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition]; 
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction]; 
mainCompositionInst.frameDuration = CMTimeMake(1, 30); 

CGSize naturalSizeFirst, naturalSizeSecond; 
if(isFirstAssetPortrait_){ 
    naturalSizeFirst = CGSizeMake(FirstAssetTrack.naturalSize.height, FirstAssetTrack.naturalSize.width); 
} else { 
    naturalSizeFirst = FirstAssetTrack.naturalSize; 
} 
if(isSecondAssetPortrait_){ 
    naturalSizeSecond = CGSizeMake(SecondAssetTrack.naturalSize.height, SecondAssetTrack.naturalSize.width); 
} else { 
    naturalSizeSecond = SecondAssetTrack.naturalSize; 
} 

float renderWidth, renderHeight; 
if(naturalSizeFirst.width > naturalSizeSecond.width) { 
    renderWidth = naturalSizeFirst.width; 
} else { 
    renderWidth = naturalSizeSecond.width; 
} 
if(naturalSizeFirst.height > naturalSizeSecond.height) { 
    renderHeight = naturalSizeFirst.height; 
} else { 
    renderHeight = naturalSizeSecond.height; 
} 
MainCompositionInst.renderSize = CGSizeMake(renderWidth, renderHeight); 

希望這有助於