2016-11-30 213 views
0

我的目标是让用户从照片中选择视频,然后让他在其上添加标签。AVAssetExportSession导出需要很长时间

以下是我有:

let audioAsset = AVURLAsset(url: selectedVideoURL) 
let videoAsset = AVURLAsset(url: selectedVideoURL) 
let mixComposition = AVMutableComposition() 
let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) 
let compositionAudioTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) 
let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] 
let clipAudioTrack = audioAsset.tracks(withMediaType: AVMediaTypeAudio)[0] 
do { 
    try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: clipVideoTrack, at: kCMTimeZero) 
    try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioAsset.duration), of: clipAudioTrack, at: kCMTimeZero) 
    compositionVideoTrack.preferredTransform = clipVideoTrack.preferredTransform 
} catch { 
    print(error) 
} 
var videoSize = clipVideoTrack.naturalSize 
if isVideoPortrait(asset: videoAsset) { 
    videoSize = CGSize(width: videoSize.height, height: videoSize.width) 
} 
let parentLayer = CALayer() 
let videoLayer = CALayer() 
parentLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height) 
videoLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height) 
parentLayer.addSublayer(videoLayer) 

// adding label 
let helloLabelLayer = CATextLayer() 
helloLabelLayer.string = "Hello" 
helloLabelLayer.font = "Signika-Semibold" as CFTypeRef? 
helloLabelLayer.fontSize = 30.0 
helloLabelLayer.contentsScale = mainScreen.scale 
helloLabelLayer.alignmentMode = kCAAlignmentNatural 
helloLabelLayer.frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 50.0) 
parentLayer.addSublayer(helloLabelLayer) 

// creating composition 
let videoComp = AVMutableVideoComposition() 
videoComp.renderSize = videoSize 
videoComp.frameDuration = CMTimeMake(1, 30) 
videoComp.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer) 

let instruction = AVMutableVideoCompositionInstruction() 
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration) 
let layerInstruction = videoCompositionInstructionForTrack(track: compositionVideoTrack, asset: videoAsset) 
instruction.layerInstructions = [layerInstruction] 
videoComp.instructions = [instruction] 
if let assetExport = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPreset640x480) { 
    let filename = NSTemporaryDirectory().appending("video.mov") 

    if FileManager.default.fileExists(atPath: filename) { 
    do { 
     try FileManager.default.removeItem(atPath: filename) 
    } catch { 
     print(error) 
    } 
} 

let url = URL(fileURLWithPath: filename) 
assetExport.outputURL = url 
assetExport.outputFileType = AVFileTypeMPEG4 
assetExport.videoComposition = videoComp 
print(NSDate().timeIntervalSince1970) 
assetExport.exportAsynchronously { 
    print(NSDate().timeIntervalSince1970) 
    let library = ALAssetsLibrary() 
    library.writeVideoAtPath(toSavedPhotosAlbum: url, completionBlock: { 
     (url, error) in 
     switch assetExport.status { 
      case AVAssetExportSessionStatus.failed: 
       p("failed \(assetExport.error)") 
      case AVAssetExportSessionStatus.cancelled: 
       p("cancelled \(assetExport.error)") 
      default: 
       p("complete") 
       p(NSDate().timeIntervalSince1970) 
       if FileManager.default.fileExists(atPath: filename) { 
        do { 
         try FileManager.default.removeItem(atPath: filename) 
        } catch { 
         p(error) 
        } 
       } 
       print("Exported")          
     } 
    }) 
} 

实施isVideoPortrait功能:

func isVideoPortrait(asset: AVAsset) -> Bool { 
    var isPortrait = false 
    let tracks = asset.tracks(withMediaType: AVMediaTypeVideo) 
    if tracks.count > 0 { 
     let videoTrack = tracks[0] 
     let t = videoTrack.preferredTransform 
     if t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0 { 
      isPortrait = true 
     } 
     if t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0 { 
      isPortrait = true 
     } 
     if t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0 { 
      isPortrait = false 
     } 
     if t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0 { 
      isPortrait = false 
     } 
    } 
    return isPortrait 
} 

而对于video composition layer instruction的最后一个函数:

func videoCompositionInstructionForTrack(track: AVCompositionTrack, asset: AVAsset) -> AVMutableVideoCompositionLayerInstruction { 
    let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: track) 
    let assetTrack = asset.tracks(withMediaType: AVMediaTypeVideo)[0] 
    let transform = assetTrack.preferredTransform 
    instruction.setTransform(transform, at: kCMTimeZero) 
    return instruction 
} 

代码工作得很好,输出视频有标签,但如果我选择1分钟视频,请输出ta凯斯28秒。

我已经搜索了它,并试图删除layerInsctuction变换,但没有效果。

试图添加: assetExport.shouldOptimizeForNetworkUse = false 也没有效果。

此外,试图设置AVAssetExportPresetPassthroughAVAssetExportSession,在这种情况下视频出口1秒但标签已经消失。

任何帮助将不胜感激,因为我陷入困境。谢谢你的时间。

+0

您正在做两件耗时的事情:导出和复制到相册中。哪一个花了很长时间? – matt

+0

另外:当某些事情需要很长时间并且您想知道为什么时,请使用仪器。你尝试过吗?它告诉你什么? – matt

+0

@matt基本上,出口需要很多时间。复制到照片专辑需要1秒。我还没有使用过仪器,当我运行仪器时,是否应该使用“活动监视器”? – mikle94

回答

1

我能想到的唯一方法是通过比特率和分辨率来降低质量。

这是通过作用于AssetExporter的videoSettings字典完成的,这工作我不得不使用一个名为SDAVAssetExportSession

框架然后通过改变videoSettings我能与质量方面发挥获得最佳质量/速度。

let compression = [AVVideoAverageBitRateKey : 2097152(DESIRED_BITRATE),AVVideoProfileLevelKey : AVVideoProfileLevelH264BaselineAutoLevel] 

    let videoSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : maxWidth, AVVideoHeightKey : maxHeight, AVVideoCompressionPropertiesKey:compression] 

这是我能够加快速度的唯一方法。

+0

谢谢,它帮助我减少了一半的时间。 – mikle94

+0

我不相信你必须为此使用一个框架(除非我遗漏了一些东西),你可以从AVAssetExportSession.exportPresets(compatibleWith:yourVideoAsset)'''中获取预设,然后将它们传递给你的AVAssetExportSession实例。 – AdjunctProfessorFalcon

+0

我相信这些特定的键与AVAssetExportSession不兼容,您必须使用较低的AVAssetWriter,这是该文件在文件下面的功能。 – SeanLintern88

1

这不是你的问题直接相关的,但在这里,你的代码是倒退:

assetExport.exportAsynchronously { 
    let library = ALAssetsLibrary() 
    library.writeVideoAtPath(toSavedPhotosAlbum: url, completionBlock: { 
     switch assetExport.status { 

不不不。首先你完成资产出口。然后你可以复制到别的地方,如果这是你想要做的。因此,这需要这样的:

assetExport.exportAsynchronously { 
    switch assetExport.status { 
    case .completed: 
     let library = ALAssetsLibrary() 
     library.writeVideoAtPath... 

其他评论:

  • ALAssetsLibrary已经死了。这不是复制到用户照片库的方式。使用Photo框架。

  • 你原来的代码很奇怪,因为还有很多你没有测试的其他案例。你只是,假设default表示.completed。这很危险。

+0

Ty为您的回复,我会改变这一点。 – mikle94

+0

不用担心。对不起,这不是一个真正的答案。我可能不得不删除它。 :)但是没有办法在评论中说出所有这些。 – matt