在预览和导出时将肖像视频转换为aspectFit

时间:2016-06-02 19:54:59

标签: swift avfoundation orientation avasset avvideocomposition

我一直试图让肖像视频转换成一个可以在播放器中心适应的方式,同时保持屏幕的其余部分为空。

Example of How the video should look like

这是代码: 几件事:  self.videoRenderSize = CGSize(宽度:1920.0,高度:1080.0)  videoTrackSize可能是(960,720),(1280,720),(1920,1080)(所有肖像虽然,所以宽度是高度,反之亦然)

        let instruction = AVMutableVideoCompositionInstruction()
        instruction.timeRange = passThroughTimeRanges[i]

        let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: currentTrack)

        let assetObject = self.assetsList[i]["assetVideo"] as! Asset
        let asset = assetObject.movieAsset as AVAsset
        let videoTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
        let videoTrackSize = videoTrack.naturalSize
        let preferredTransform = videoTrack.preferredTransform

        let videoOrientationMode = self.isVideoPortrait(asset)

        print(preferredTransform)
        print(videoTrackSize)

        switch videoOrientationMode {
        case "Portrait" :

            if self.videoRenderSize.height > videoTrackSize.height {

            }else if self.videoRenderSize.height == videoTrackSize.height {

            }else {

            }

            break

        case "PortraitUpsideDown":


            break

        case "LandscapeRight", "LandscapeLeft":

            let heightScale = CGFloat(self.videoRenderSize.height / videoTrackSize.height)
            let widthScale = CGFloat(self.videoRenderSize.width / videoTrackSize.width)

            layerInstruction.setTransform(CGAffineTransformMakeScale(widthScale,heightScale), atTime: instruction.timeRange.start)

            break
        default:
            print("Unknown Orientation")
        }

        instruction.layerInstructions = [layerInstruction]
        compositionInstructions.append(instruction)
    }

    videoComposition.instructions = compositionInstructions
    videoComposition.renderSize = self.videoRenderSize
    videoComposition.frameDuration = CMTimeMake(1, 30)
    videoComposition.renderScale = 1.0 // This is a iPhone only option.

isVideoPortrait函数的代码

    func isVideoPortrait(asset : AVAsset) -> String{

    var videoOrientation : String = ""

    let tracks : NSArray  = asset.tracksWithMediaType(AVMediaTypeVideo)

    if(tracks.count > 0) {

        let videoTrack : AVAssetTrack = tracks[0] as! AVAssetTrack

        let t = videoTrack.preferredTransform

        // Portrait
        if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0 || t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)
        {
            videoOrientation = "Portrait"
        }
        // PortraitUpsideDown
        if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)  {
            videoOrientation = "PortraitUpsideDown"
        }
        // LandscapeRight
        if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
        {
            videoOrientation = "LandscapeRight"
        }
        // LandscapeLeft
        if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)
        {
            videoOrientation = "LandscapeLeft"
        }
    }

    return videoOrientation
}

什么是转换图层说明,以确保视频将在中心转换,并且可以放置在上面附带的图像中。

0 个答案:

没有答案