質問編集履歴

1

余計なソースコードの削除

2017/10/22 21:14

投稿

knitbow
knitbow

スコア11

test CHANGED
File without changes
test CHANGED
@@ -56,188 +56,6 @@
56
56
 
57
57
 
58
58
 
59
- // 動画合成用のAVMutableCompositionTrackを生成
60
-
61
- let compositionVideoTrack = mutableComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
62
-
63
-
64
-
65
- // 動画をトラックに追加
66
-
67
- do {
68
-
69
- try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoTrackFirst.timeRange.duration), of: videoTrackFirst, at: kCMTimeZero)
70
-
71
- } catch {
72
-
73
- fatalError("videoTrack error")
74
-
75
- }
76
-
77
- do {
78
-
79
- try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoTrackSecond.timeRange.duration), of: videoTrackSecond, at: kCMTimeZero)
80
-
81
- } catch {
82
-
83
- fatalError("videoTrack error")
84
-
85
- }
86
-
87
-
88
-
89
- // First合成命令用オブジェクトを生成
90
-
91
- let mutableVideoCompositionInstructionFirst = AVMutableVideoCompositionInstruction()
92
-
93
- mutableVideoCompositionInstructionFirst.timeRange = CMTimeRangeMake(kCMTimeZero, videoTrackFirst.timeRange.duration)
94
-
95
- mutableVideoCompositionInstructionFirst.backgroundColor = UIColor.red.cgColor
96
-
97
-
98
-
99
- // Second合成命令用オブジェクトを生成
100
-
101
- let mutableVideoCompositionInstructionSecond = AVMutableVideoCompositionInstruction()
102
-
103
- mutableVideoCompositionInstructionSecond.timeRange = CMTimeRangeMake(kCMTimeZero, videoTrackSecond.timeRange.duration)
104
-
105
- mutableVideoCompositionInstructionSecond.backgroundColor = UIColor.red.cgColor
106
-
107
-
108
-
109
- // AVMutableVideoCompositionを生成
110
-
111
- let mutableVideoComposition = AVMutableVideoComposition.init()
112
-
113
- mutableVideoComposition.instructions = [mutableVideoCompositionInstructionFirst, mutableVideoCompositionInstructionSecond]
114
-
115
-
116
-
117
- // 動画の回転情報を取得する
118
-
119
- let transform1 = videoTrackFirst.preferredTransform;
120
-
121
- let isVideoAssetPortrait = ( transform1.a == 0 &&
122
-
123
- transform1.d == 0 &&
124
-
125
- (transform1.b == 1.0 || transform1.b == -1.0) &&
126
-
127
- (transform1.c == 1.0 || transform1.c == -1.0));
128
-
129
-
130
-
131
- // 動画のサイズ設定
132
-
133
- var naturalSizeFirst = CGSize()
134
-
135
- var naturalSizeSecond = CGSize()
136
-
137
- if (isVideoAssetPortrait) {
138
-
139
- naturalSizeFirst = CGSize(width: videoTrackFirst.naturalSize.height, height: videoTrackFirst.naturalSize.width);
140
-
141
- naturalSizeSecond = CGSize(width: videoTrackSecond.naturalSize.height, height: videoTrackSecond.naturalSize.width);
142
-
143
-
144
-
145
- }else {
146
-
147
- naturalSizeFirst = videoTrackFirst.naturalSize;
148
-
149
- naturalSizeSecond = videoTrackSecond.naturalSize;
150
-
151
- }
152
-
153
- let renderWidth = max(naturalSizeFirst.width, naturalSizeSecond.width)
154
-
155
- let renderHeight = max(naturalSizeFirst.height, naturalSizeSecond.height)
156
-
157
-
158
-
159
- // 書き出す動画のサイズ設定
160
-
161
- mutableVideoComposition.renderSize = CGSize(width: renderWidth, height: renderHeight);
162
-
163
- // 書き出す動画のフレームレート(30FPS)
164
-
165
- mutableVideoComposition.frameDuration = CMTimeMake(1, 30);
166
-
167
-
168
-
169
- // AVMutableCompositionを元にExporterの生成
170
-
171
- let assetExportSession: AVAssetExportSession = AVAssetExportSession(asset: mutableComposition, presetName: AVAssetExportPreset1920x1080)!
172
-
173
- let composedMovieDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0];
174
-
175
- let composedMoviePath = NSString(format:"%@/%@", composedMovieDirectory, "test.mp4");
176
-
177
-
178
-
179
- // すでに合成動画が存在していたら消す
180
-
181
- let fileManager = FileManager.default;
182
-
183
- if (fileManager.fileExists(atPath: composedMoviePath as String)) {
184
-
185
- do {
186
-
187
- try fileManager.removeItem(atPath: composedMoviePath as String)
188
-
189
- } catch {
190
-
191
- fatalError("removeItem file")
192
-
193
- }
194
-
195
- }
196
-
197
-
198
-
199
- // 保存設定
200
-
201
- let composedMovieUrl = NSURL(fileURLWithPath:composedMoviePath as String);
202
-
203
- assetExportSession.outputFileType = AVFileTypeQuickTimeMovie;
204
-
205
- assetExportSession.outputURL = composedMovieUrl as URL;
206
-
207
- assetExportSession.shouldOptimizeForNetworkUse = true;
208
-
209
-
210
-
211
- // エクスポート
212
-
213
- assetExportSession.exportAsynchronously(completionHandler: {() -> Void in
214
-
215
- switch assetExportSession.status {
216
-
217
- case .completed:
218
-
219
- print("Crop Success! Url -> (composedMovieUrl)")
220
-
221
- case .failed, .cancelled:
222
-
223
- print("error = (String(describing: assetExportSession.error))")
224
-
225
- default:
226
-
227
- print("error = (String(describing: assetExportSession.error))")
228
-
229
- }
230
-
231
- })
232
-
233
-
234
-
235
- return composedMovieUrl as URL
236
-
237
-
238
-
239
- }
240
-
241
59
  ```
242
60
 
243
61