質問編集履歴
1
aa
test
CHANGED
File without changes
|
test
CHANGED
@@ -45,3 +45,123 @@
|
|
45
45
|
|
46
46
|
|
47
47
|
下記のような実装でアプリを実行すると、ドキュメントフォルダに、動画ファイルが生成されません。exportを消してみれば、、ドキュメントフォルダに、動画ファイルが生成され正常に動きます。
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
# 実装
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
```swift
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
//動画トリミング
|
62
|
+
|
63
|
+
func createAudioFileFromAsset(_ asset: AVAsset){
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// ロゴのCALayerの作成
|
72
|
+
|
73
|
+
let logoImage: UIImage = UIImage(named: "iwk.jpg")!
|
74
|
+
|
75
|
+
let logoLayer: CALayer = CALayer()
|
76
|
+
|
77
|
+
logoLayer.contents = logoImage.cgImage
|
78
|
+
|
79
|
+
logoLayer.opacity = 0.9
|
80
|
+
|
81
|
+
// 親レイヤーを作成
|
82
|
+
|
83
|
+
let parentLayer: CALayer = CALayer()
|
84
|
+
|
85
|
+
parentLayer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
|
86
|
+
|
87
|
+
parentLayer.addSublayer(logoLayer)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
// 合成用コンポジション作成
|
94
|
+
|
95
|
+
let videoComp: AVMutableVideoComposition = AVMutableVideoComposition()
|
96
|
+
|
97
|
+
let videoSize: CGSize = videoAssetSourceTrack.naturalSize
|
98
|
+
|
99
|
+
videoComp.renderSize = videoSize
|
100
|
+
|
101
|
+
videoComp.frameDuration = CMTimeMake(1, 30)
|
102
|
+
|
103
|
+
videoComp.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: parentLayer, in: parentLayer)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// インストラクション作成
|
110
|
+
|
111
|
+
let instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
|
112
|
+
|
113
|
+
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, comp.duration)
|
114
|
+
|
115
|
+
let layerInstruction: AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction.init(assetTrack: videoAssetSourceTrack)
|
116
|
+
|
117
|
+
instruction.layerInstructions = [layerInstruction]
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// インストラクションを合成用コンポジションに設定
|
122
|
+
|
123
|
+
videoComp.instructions = [instruction]
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
|
132
|
+
|
133
|
+
let filePath = documentsDirectory.appendingPathComponent("rendered-audio.m4v")
|
134
|
+
|
135
|
+
//filePath.videoAssetTrack.naturalSize = 320.0
|
136
|
+
|
137
|
+
if let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset640x480){
|
138
|
+
|
139
|
+
//exportSession.naturalSize.preferredTransform = 320.0
|
140
|
+
|
141
|
+
exportSession.canPerformMultiplePassesOverSourceMediaData = true
|
142
|
+
|
143
|
+
exportSession.outputURL = filePath
|
144
|
+
|
145
|
+
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration)
|
146
|
+
|
147
|
+
exportSession.outputFileType = AVFileTypeQuickTimeMovie
|
148
|
+
|
149
|
+
//exportSession.videoComposition = videoComp
|
150
|
+
|
151
|
+
exportSession.exportAsynchronously {
|
152
|
+
|
153
|
+
_ in
|
154
|
+
|
155
|
+
print("finished: \(filePath) : \(exportSession.status.rawValue) ")
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```
|