使用AVMutableComposition拼接(合并)视频时固定方向

Ang*_*uck 16 video ios avmutablecomposition swift

TLDR - SEE EDIT

我正在Swift中创建一个测试应用程序,我想在我的应用程序文档目录中拼接多个视频AVMutableComposition.

我在某种程度上成功地做到了这一点,我的所有视频都拼​​凑在一起,一切都显示出正确尺寸的肖像和风景.

但问题是,所有视频都显示在编辑中最后一个视频的方向上.

我知道要解决这个问题,我需要为我添加的每个曲目添加图层说明,但是我似乎无法做到这一点,我发现整个编辑的答案似乎是以纵向方式出现的风景视频只需按比例缩放以适应纵向视图,因此当我将手机侧面转动以查看风景视频时,它们仍然很小,因为它们已缩放为纵向尺寸.

这不是我想要的结果,我想要预期的功能,即如果视频是横向的,它在纵向模式下显示缩放,但如果手机旋转,我希望该横向视频填充屏幕(就像在查看时一样)照片中的风景视频)和肖像相同,因此在纵向观看时它是全屏的,当侧向转动时,视频会缩放到横向尺寸(就像在照片中查看肖像视频时一样).

总之,我想要的结果是,当查看具有横向和纵向视频的编辑时,我可以使用我的手机侧面查看整个编辑,并且横向视频是全屏的,纵向是缩放的,或者在查看相同的视频时肖像肖像视频全屏,风景视频按比例缩放.

通过所有答案,我发现情况并非如此,当从照片导入视频以添加到编辑时,它们似乎都有非常意外的行为,并且在添加使用前置摄像头拍摄的视频时具有相同的随机行为(至明确我目前的实施视频从图书馆导入和"自拍"视频出现正确的大小没有这些问题).

我正在寻找一种旋转/缩放这些视频的方法,以便它们始终以正确的方向和比例显示,具体取决于用户握住手机的方向.

编辑:我现在知道我不能在一个视频中同时拥有横向和纵向方向,所以我正在寻找的预期结果是以横向方向制作最终视频.我已经想出如何切换所有的方向和比例,以获得一切相同的方式,但我的输出是一个肖像视频,如果有人可以帮助我改变这,所以我的输出是景观,将不胜感激.

以下是我获取每个视频说明的功能:

func videoTransformForTrack(asset: AVAsset) -> CGAffineTransform
{
    var return_value:CGAffineTransform?

    let assetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]

    let transform = assetTrack.preferredTransform
    let assetInfo = orientationFromTransform(transform)

    var scaleToFitRatio = UIScreen.mainScreen().bounds.width / assetTrack.naturalSize.width
    if assetInfo.isPortrait
    {
        scaleToFitRatio = UIScreen.mainScreen().bounds.width / assetTrack.naturalSize.height
        let scaleFactor = CGAffineTransformMakeScale(scaleToFitRatio, scaleToFitRatio)
        return_value = CGAffineTransformConcat(assetTrack.preferredTransform, scaleFactor)
    }
    else
    {
        let scaleFactor = CGAffineTransformMakeScale(scaleToFitRatio, scaleToFitRatio)
        var concat = CGAffineTransformConcat(CGAffineTransformConcat(assetTrack.preferredTransform, scaleFactor), CGAffineTransformMakeTranslation(0, UIScreen.mainScreen().bounds.width / 2))
        if assetInfo.orientation == .Down
        {
            let fixUpsideDown = CGAffineTransformMakeRotation(CGFloat(M_PI))
            let windowBounds = UIScreen.mainScreen().bounds
            let yFix = assetTrack.naturalSize.height + windowBounds.height
            let centerFix = CGAffineTransformMakeTranslation(assetTrack.naturalSize.width, yFix)
            concat = CGAffineTransformConcat(CGAffineTransformConcat(fixUpsideDown, centerFix), scaleFactor)
        }
        return_value = concat
    }
    return return_value!
}
Run Code Online (Sandbox Code Playgroud)

和出口商:

    // Create AVMutableComposition to contain all AVMutableComposition tracks
    let mix_composition = AVMutableComposition()
    var total_time = kCMTimeZero

    // Loop over videos and create tracks, keep incrementing total duration
    let video_track = mix_composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    var instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: video_track)
    for video in videos
    {
        let shortened_duration = CMTimeSubtract(video.duration, CMTimeMake(1,10));
        let videoAssetTrack = video.tracksWithMediaType(AVMediaTypeVideo)[0]

        do
        {
            try video_track.insertTimeRange(CMTimeRangeMake(kCMTimeZero, shortened_duration),
                ofTrack: videoAssetTrack ,
                atTime: total_time)

            video_track.preferredTransform = videoAssetTrack.preferredTransform

        }
        catch _
        {
        }

        instruction.setTransform(videoTransformForTrack(video), atTime: total_time)

        // Add video duration to total time
        total_time = CMTimeAdd(total_time, shortened_duration)
    }

    // Create main instrcution for video composition
    let main_instruction = AVMutableVideoCompositionInstruction()
    main_instruction.timeRange = CMTimeRangeMake(kCMTimeZero, total_time)
    main_instruction.layerInstructions = [instruction]
    main_composition.instructions = [main_instruction]
    main_composition.frameDuration = CMTimeMake(1, 30)
    main_composition.renderSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)

    let exporter = AVAssetExportSession(asset: mix_composition, presetName: AVAssetExportPreset640x480)
    exporter!.outputURL = final_url
    exporter!.outputFileType = AVFileTypeMPEG4
    exporter!.shouldOptimizeForNetworkUse = true
    exporter!.videoComposition = main_composition

    // 6 - Perform the Export
    exporter!.exportAsynchronouslyWithCompletionHandler()
    {
        // Assign return values based on success of export
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.exportDidFinish(exporter!)
        })
    }
Run Code Online (Sandbox Code Playgroud)

很抱歉,我只是想确保我对所要求的内容非常清楚,因为其他答案对我没有用.

Ale*_*ano 2

我不确定你orientationFromTransform()给了你正确的方向。

我认为你尝试修改它或尝试类似的操作:

extension AVAsset {

    func videoOrientation() -> (orientation: UIInterfaceOrientation, device: AVCaptureDevicePosition) {
        var orientation: UIInterfaceOrientation = .Unknown
        var device: AVCaptureDevicePosition = .Unspecified

        let tracks :[AVAssetTrack] = self.tracksWithMediaType(AVMediaTypeVideo)
        if let videoTrack = tracks.first {

            let t = videoTrack.preferredTransform

            if (t.a == 0 && t.b == 1.0 && t.d == 0) {
                orientation = .Portrait

                if t.c == 1.0 {
                    device = .Front
                } else if t.c == -1.0 {
                    device = .Back
                }
            }
            else if (t.a == 0 && t.b == -1.0 && t.d == 0) {
                orientation = .PortraitUpsideDown

                if t.c == -1.0 {
                    device = .Front
                } else if t.c == 1.0 {
                    device = .Back
                }
            }
            else if (t.a == 1.0 && t.b == 0 && t.c == 0) {
                orientation = .LandscapeRight

                if t.d == -1.0 {
                    device = .Front
                } else if t.d == 1.0 {
                    device = .Back
                }
            }
            else if (t.a == -1.0 && t.b == 0 && t.c == 0) {
                orientation = .LandscapeLeft

                if t.d == 1.0 {
                    device = .Front
                } else if t.d == -1.0 {
                    device = .Back
                }
            }
        }

        return (orientation, device)
    }
}
Run Code Online (Sandbox Code Playgroud)