参考
http://qiita.com/masapp/items/dd0589d2a894a2a32b82
説明
下記のコードを実行すると、下記のように、動画とロゴ画像が合成された動画が生成されるはずなのですが、なぜ、下記のディレクトリに動画が合成されて保存されていません。色々AVFoundationをの動きに関して調べているのですが、何が正しくて何が正しくないのか、未だに理解できていません。何かアドバイスをいただければ幸いです。
/Users/ユーザー名/Library/Developer/CoreSimulator/Devices/B169A746-A6DD-47E4-9C4D-04059F032885/data/Containers/Data/Application/1CD22F47-6C3D-47C1-863C-EFA5426F89C2/Documents/test.mov
実装
swift
1 2import UIKit 3import AVFoundation 4 5 6 7class ViewController: UIViewController { 8 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 14 //動画のURLを定義 15 let url: URL = { 16 let url = URL(string: "http://160.16.120.119/jj.m4v") 17 return url! 18 }() 19 20 // 動画URLからアセットを生成 21 let videoAsset: AVURLAsset = AVURLAsset(url: url, options: nil) 22 23 print("videoAsset\(videoAsset)") 24 25 // ベースとなる動画のコンポジション作成 26 let mixComposition : AVMutableComposition = AVMutableComposition() 27 let compositionVideoTrack: AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) 28 29 // アセットからトラックを取得 30 let videoTrack: AVAssetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] 31 32 // コンポジションの設定 33 try! compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoTrack, at: kCMTimeZero) 34 compositionVideoTrack.preferredTransform = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0].preferredTransform 35 36 // ロゴのCALayerの作成 37 let logoImage: UIImage = UIImage(named: "logo.png")! 38 let logoLayer: CALayer = CALayer() 39 logoLayer.contents = logoImage.cgImage 40 logoLayer.frame = CGRect(x: 5, y: 25, width: 57, height: 57) 41 logoLayer.opacity = 0.9 42 43 // 動画のサイズを取得 44 let videoSize: CGSize = videoTrack.naturalSize 45 46 // 親レイヤーを作成 47 let parentLayer: CALayer = CALayer() 48 let videoLayer: CALayer = CALayer() 49 parentLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height) 50 videoLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height) 51 parentLayer.addSublayer(videoLayer) 52 parentLayer.addSublayer(logoLayer) 53 54 // 合成用コンポジション作成 55 let videoComp: AVMutableVideoComposition = AVMutableVideoComposition() 56 videoComp.renderSize = videoSize 57 videoComp.frameDuration = CMTimeMake(1, 30) 58 videoComp.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, in: parentLayer) 59 60 // インストラクション作成 61 let instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction() 62 instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration) 63 let layerInstruction: AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction.init(assetTrack: videoTrack) 64 instruction.layerInstructions = [layerInstruction] 65 66 // インストラクションを合成用コンポジションに設定 67 videoComp.instructions = [instruction] 68 69 // 動画のコンポジションをベースにAVAssetExportを生成 70 let _assetExport = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPresetMediumQuality) 71 // 合成用コンポジションを設定 72 _assetExport?.videoComposition = videoComp 73 74 // エクスポートファイルの設定 75 let videoName: String = "test.mov" 76 let _exportPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 77 let exportPath: String = _exportPath + "/" + videoName 78 let exportUrl: URL = URL(fileURLWithPath: exportPath) 79 _assetExport?.outputFileType = AVFileTypeQuickTimeMovie 80 _assetExport?.outputURL = exportUrl 81 _assetExport?.shouldOptimizeForNetworkUse = true 82 83 print(exportPath) 84 print(exportUrl) 85 86 // ファイルが存在している場合は削除 87 if FileManager.default.fileExists(atPath: exportPath) { 88 try! FileManager.default.removeItem(atPath: exportPath) 89 } 90 91 // エクスポート実行 92// _assetExport?.exportAsynchronously(completionHandler: {() -> Void in 93// // 端末に保存 94// let library: ALAssetLibrary = ALAssetsLibrary() 95// if library.videoAtPathIs(compatibleWithSavedPhotosAlbum: exportUrl) { 96// library.writeVideoAtPath(toSavedPhotosAlbum: self.delegate.exportUrl, completionBlock: nil) 97// } 98// }) 99 100 } 101 102 override func didReceiveMemoryWarning() { 103 super.didReceiveMemoryWarning() 104 // Dispose of any resources that can be recreated. 105 } 106} 107
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。