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

标签 ios swift video avmutablecomposition

TLDR - 查看编辑

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

我在这方面取得了一定程度的成功,我所有的视频都拼接在一起,并且所有内容都显示正确的纵向和横向尺寸。

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

我知道要解决这个问题,我需要为我添加的每条轨道添加图层说明,但我似乎无法做到这一点,根据我发现的答案,整个合辑似乎以纵向显示横向视频只是简单地缩放以适合纵向 View ,所以当我将手机侧放以查看横向视频时,它们仍然很小,因为它们已被缩放为纵向尺寸。

这不是我想要的结果,我想要预期的功能,即如果视频是横向的,它会在纵向模式下显示缩放,但如果手机旋转,我希望横向视频填满屏幕(因为它会当简单地查看照片中的横向视频时)和纵向相同,因此当以纵向方式查看时,它是全屏的,而当横向查看时,视频会缩放到横向大小(就像在查看照片中的纵向视频时一样)。

总而言之,我想要的期望结果是,在观看包含横向和纵向视频的合辑时,我可以将手机放在一边查看整个合辑,并且横向视频是全屏的,纵向视频是缩放的,或者当查看纵向相同的视频,纵向视频全屏显示,横向视频按比例缩放。

根据所有答案,我发现情况并非如此,当从照片中导入视频以添加到编辑中时,它们似乎都有非常意外的行为,并且在添加正面拍摄的视频时也有相同的随机行为相机(要清楚我当前从库中导入的实现视频和“自拍”视频以正确的尺寸显示而没有这些问题)。

我正在寻找一种旋转/缩放这些视频的方法,以便它们始终以正确的方向显示,并根据用户手持手机的方向进行缩放。

编辑:我现在知道我不能在一个视频中同时拥有横向和纵向方向,所以我正在寻找的预期结果是最终视频是横向的方向。我已经想出如何切换所有方向和比例以使所有内容都以相同的方式向上但我的输出是纵向视频如果有人可以帮助我改变它所以我的输出是横向它会很感激。

下面是我获取每个视频指令的函数:

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!
}

导出商:

    // 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!)
        })
    }

对于冗长的解释,我深表歉意,我只是想确保我对自己的问题非常清楚,因为其他答案对我没有用。

最佳答案

我不确定您的 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)
    }
}

关于ios - 使用 AVMutableComposition 拼接(合并)视频时固定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663942/

相关文章:

ios - 使用 segues 恢复到已经打开的 View

ios - 获取总数包括 2 个日期之间的月份

ios - 如何更好地发送完整的闭包

swift - Alamofire 超时 url

Android intent share 在 facebook 上分享视频 url

php - 上传从同一服务器下载的视频时不生成缩略图

iphone - 如何使用 UIScrollView 创建顶部淡入淡出效果?

ios 权限/授权是如何工作的?

ios - 对 iOS 7 应用程序使用 iOS 6 主题

html - 无法播放HTML嵌入式Youtube视频