我正在尝试缩放,居中并填充分辨率为1920x1080的标准16:9视频。这些问题似乎不是编码问题,但更多的是我无法解决的数学问题,尤其是对于旋转的视频。
在缩放视频时,我需要做什么以使视频居中,但通常会在视频上留下黑框。我知道这只是和 CGAffineTransformMakeTranslation
一起玩的一种情况-(CGAffineTransform)exportTransform:(AVAsset *)asset {
AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
UIImageOrientation orientation = UIImageOrientationUp;
BOOL portrait = false;
CGAffineTransform firstTransform = track.preferredTransform;
if(firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0) {
orientation= UIImageOrientationRight;
portrait = true;
}
if(firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) {
orientation = UIImageOrientationLeft;
portrait = true;
}
if(firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) {
orientation = UIImageOrientationUp;
}
if(firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) {
orientation = UIImageOrientationDown;
}
CGFloat FirstAssetScaleToFitRatio = self.videoresize.height / track.naturalSize.height;
if (portrait) {
FirstAssetScaleToFitRatio = self.videoresize.width / track.naturalSize.height;
CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);
return CGAffineTransformConcat(track.preferredTransform, FirstAssetScaleFactor);
}
else {
CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);
return CGAffineTransformConcat(CGAffineTransformConcat(track.preferredTransform, FirstAssetScaleFactor),CGAffineTransformMakeTranslation(0.0, -0.0));
}
}