forked from swiftyfinch/VideoMerger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoMergeManager.swift
More file actions
516 lines (439 loc) · 22.4 KB
/
VideoMergeManager.swift
File metadata and controls
516 lines (439 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//
// VideoMergeManager.swift
// Video merger manager
//
// Created by Vyacheslav Khorkov on 23/08/16.
// Copyright © 2016 Vyacheslav Khorkov. All rights reserved.
//
import Foundation
import AVFoundation
public let VideoMergeManagerVerbose: Bool = false // TODO: implement
public let VideoMergeManagerPrintPts: Bool = false
public let VideoMergeManagerDomain: String = "videoMergeManager"
public enum VideoMergeManagerErrorCode: Int {
case WrongInputParameters = -10000
case CannotFindVideoDescriptionInSourceFile = -10001
case CannotCreateAssetVideoWriter = -10002
case CannotCreateVideoInput = -10003
}
class VideoMergeManager
{
static func mergeMultipleVideos(destinationPath destinationPath: String, filePaths: [String], finished: ((NSError?, NSURL?) -> Void))
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
// Check input parametes
if filePaths.count < 1 {
let error = NSError(domain: VideoMergeManagerDomain,
code: VideoMergeManagerErrorCode.WrongInputParameters.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Please, check [filePaths]."])
dispatch_async(dispatch_get_main_queue(), {
finished(error, nil)
})
return
}
// Get audio and video format description
let formatDescriptionTuple: (videoFormatHint: CMFormatDescriptionRef?,
audioFormatHint: CMFormatDescriptionRef?) = findFormatDescription(filePaths.first!)
if formatDescriptionTuple.videoFormatHint == nil {
let error = NSError(domain: VideoMergeManagerDomain,
code: VideoMergeManagerErrorCode.CannotFindVideoDescriptionInSourceFile.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Can't find video format description in source file."])
dispatch_async(dispatch_get_main_queue(), {
finished(error, nil)
})
return
}
let videoFormatHint: CMFormatDescriptionRef = formatDescriptionTuple.videoFormatHint!
let audioFormatHint: CMFormatDescriptionRef? = formatDescriptionTuple.audioFormatHint
// Prepare asset writer
var assetWriter: AVAssetWriter
var videoInput: AVAssetWriterInput
var audioInput: AVAssetWriterInput? // optional
let assetWriterTuple: (writer: AVAssetWriter?,
videoInput: AVAssetWriterInput?,
audioInput:AVAssetWriterInput?) = prepareAssetWriter(destinationPath: destinationPath,
videoFormatHint: videoFormatHint,
audioFormatHint: audioFormatHint)
if let test = assetWriterTuple.writer {
assetWriter = test
}
else {
let error = NSError(domain: VideoMergeManagerDomain,
code: VideoMergeManagerErrorCode.CannotCreateAssetVideoWriter.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Can't create asset video writer"])
dispatch_async(dispatch_get_main_queue(), {
finished(error, nil)
})
return
}
if let test = assetWriterTuple.videoInput {
videoInput = test
}
else {
let error = NSError(domain: VideoMergeManagerDomain,
code: VideoMergeManagerErrorCode.CannotCreateVideoInput.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Can't create video input"])
dispatch_async(dispatch_get_main_queue(), {
finished(error, nil)
})
return
}
if let test = assetWriterTuple.audioInput {
audioInput = test
}
else {
print("Can't create audio input")
}
// Start writing
assetWriter.startWriting()
assetWriter.startSessionAtSourceTime(kCMTimeZero)
var index = 0
var isLastFilePath = false
var audioSampleBuffer: CMSampleBuffer
var videoSampleBuffer: CMSampleBuffer
var audioBasePts: CMTime = kCMTimeZero
var videoBasePts: CMTime = kCMTimeZero
var lastAudioPts: CMTime = kCMTimeZero
var lastVideoPts: CMTime = kCMTimeZero
var lastAudioDuration: CMTime = kCMTimeZero
//
for filePath in filePaths
{
isLastFilePath = index == (filePaths.count - 1)
// Prepare asset reader
var assetReader: AVAssetReader
var videoOutput: AVAssetReaderTrackOutput
var audioOutput: AVAssetReaderTrackOutput? // optional
let assetReaderTuple: (reader: AVAssetReader?,
videoOutput: AVAssetReaderTrackOutput?,
audioOutput: AVAssetReaderTrackOutput?) = prepareAssetReader(filePath: filePath)
if let test = assetReaderTuple.reader {
assetReader = test
}
else {
print("Can't create asset reader for this path: \(filePath)")
continue;
}
if let test = assetReaderTuple.videoOutput {
videoOutput = test
}
else {
print("Can't create video output for this path: \(filePath)")
continue;
}
if let test = assetReaderTuple.audioOutput {
audioOutput = test
}
else {
print("Can't create audio output for this path: \(filePath)")
}
// Start reading
assetReader.startReading()
var audioBuffers = 0
var videoBuffers = 0
while assetReader.status == .Reading
{
// Video
if let test = videoOutput.copyNextSampleBuffer() {
videoSampleBuffer = test
while !videoInput.readyForMoreMediaData {
usleep(100) // TODO: add limit
}
if videoBasePts.value != 0 {
let newPts: CMTime = CMTimeAdd(CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer), videoBasePts)
let changedSampleBuffer:CMSampleBuffer? = copySampleBufferWithPresentationTime(sampleBuffer: videoSampleBuffer,
pts: newPts)
if let test = changedSampleBuffer {
videoSampleBuffer = test
}
}
videoInput.appendSampleBuffer(videoSampleBuffer)
videoBuffers += 1
lastVideoPts = CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer)
if VideoMergeManagerPrintPts {
print("v: \(String(format: "%.4f", CMTimeGetSeconds(lastVideoPts)))")
}
}
else {
assetReader.cancelReading()
break
}
// Audio
if audioInput == nil || audioOutput == nil {
continue
}
if let test = audioOutput!.copyNextSampleBuffer() {
audioSampleBuffer = test
while !audioInput!.readyForMoreMediaData {
usleep(100) // TODO: add limit
}
if audioBasePts.value != 0 {
let newPts: CMTime = CMTimeAdd(CMSampleBufferGetPresentationTimeStamp(audioSampleBuffer), audioBasePts)
let changedSampleBuffer:CMSampleBuffer? = copySampleBufferWithPresentationTime(sampleBuffer: audioSampleBuffer,
pts: newPts)
if let test = changedSampleBuffer {
audioSampleBuffer = test
}
}
let currentPts = CMSampleBufferGetPresentationTimeStamp(audioSampleBuffer)
let duration = CMSampleBufferGetDuration(audioSampleBuffer)
if CMTIME_IS_VALID(currentPts) && duration.value > 0 {
if index > 0 {
// TODO: Probably in different audio tracks need to select attachments for deletion
CMRemoveAllAttachments(audioSampleBuffer)
}
audioInput!.appendSampleBuffer(audioSampleBuffer)
audioBuffers += 1
lastAudioPts = currentPts
lastAudioDuration = duration
if VideoMergeManagerPrintPts {
print("a: \(String(format: "%.4f", CMTimeGetSeconds(lastAudioPts)))")
}
}
}
if VideoMergeManagerPrintPts {
print("")
}
}
if !isLastFilePath {
if VideoMergeManagerPrintPts {
print("--")
}
let url = NSURL.fileURLWithPath(filePath)
let asset = AVAsset(URL: url)
let videoTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first!
let fps = videoTrack.nominalFrameRate
audioBasePts = CMTimeAdd(lastAudioPts, lastAudioDuration)
videoBasePts = CMTimeAdd(lastVideoPts, CMTimeMake(100, Int32(fps * 100))) // If fps like a 29.xx
let compareResult = CMTimeCompare(audioBasePts, videoBasePts)
if compareResult >= 0 {
videoBasePts = audioBasePts
}
else {
if let test = audioFormatHint {
let diff = CMTimeSubtract(videoBasePts, audioBasePts)
let sampleRate = CMAudioFormatDescriptionGetStreamBasicDescription(test).memory.mSampleRate
let scaledDiff = CMTimeConvertScale(diff, Int32(sampleRate), .RoundHalfAwayFromZero)
let silentAudioSampleBuffer = self.createSilentAudioSampleBufferWithFormatDescription(test,
numSamples: Int(scaledDiff.value),
presentationTime: audioBasePts)
audioInput!.appendSampleBuffer(silentAudioSampleBuffer!)
}
audioBasePts = videoBasePts
}
if VideoMergeManagerPrintPts {
print("vc: \(videoBuffers)");
print("ac: \(audioBuffers)");
print("vb: \(String(format: "%.4f", CMTimeGetSeconds(videoBasePts)))")
print("ab: \(String(format: "%.4f", CMTimeGetSeconds(audioBasePts)))")
print("")
}
}
index += 1
}
assetWriter.finishWritingWithCompletionHandler {
switch assetWriter.status {
case .Cancelled:
print("Cancelled")
break
case .Completed:
print("Completed")
break
case .Failed:
print("Failed")
break
case .Unknown:
print("Unknown")
break
case .Writing:
print("Writing")
break
}
print(destinationPath)
dispatch_async(dispatch_get_main_queue(), {
finished(nil, NSURL(fileURLWithPath: destinationPath))
})
}
}
}
static func copySampleBufferWithPresentationTime(sampleBuffer sampleBuffer: CMSampleBuffer, pts: CMTime) -> CMSampleBuffer?
{
var count: CMItemCount = 0
CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, 0, nil, &count)
let pInfo = UnsafeMutablePointer<CMSampleTimingInfo>(malloc(sizeof(CMSampleTimingInfo) * count))
CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, count, pInfo, &count)
for i in 0..<count {
pInfo[i].decodeTimeStamp = kCMTimeInvalid;
pInfo[i].presentationTimeStamp = pts;
}
var newSampleBuffer: CMSampleBufferRef?
CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, sampleBuffer, count, pInfo, &newSampleBuffer);
free(pInfo);
return newSampleBuffer
}
static func prepareAssetWriter(destinationPath destinationPath: String,
videoFormatHint: CMAudioFormatDescription,
audioFormatHint: CMAudioFormatDescription?) ->
(writer: AVAssetWriter?,
videoInput: AVAssetWriterInput?,
audioInput:AVAssetWriterInput?)
{
var assetWriter: AVAssetWriter?
do {
assetWriter = try AVAssetWriter(URL: NSURL(fileURLWithPath: destinationPath), fileType: AVFileTypeMPEG4)
}
catch {
print("Can't create asset writer. Check destinationPath: \(destinationPath).")
return (nil, nil, nil)
}
assetWriter?.shouldOptimizeForNetworkUse = true
// Video input
let videoDimensions: CMVideoDimensions = CMVideoFormatDescriptionGetDimensions(videoFormatHint as CMVideoFormatDescription)
let videoSettings: [String : AnyObject] = [AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: Int(videoDimensions.width),
AVVideoHeightKey: Int(videoDimensions.height)]
let videoInput: AVAssetWriterInput? = AVAssetWriterInput(mediaType: AVMediaTypeVideo,
outputSettings: videoSettings)
videoInput?.expectsMediaDataInRealTime = true
if assetWriter!.canAddInput(videoInput!) {
assetWriter!.addInput(videoInput!)
}
else {
print("Can't add video input")
return (nil, nil, nil)
}
let audioSettings: [String : AnyObject] = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: 2]
let audioInput: AVAssetWriterInput? = AVAssetWriterInput(mediaType: AVMediaTypeAudio,
outputSettings: audioSettings,
sourceFormatHint: audioFormatHint)
audioInput?.expectsMediaDataInRealTime = true
if assetWriter!.canAddInput(audioInput!) {
assetWriter!.addInput(audioInput!)
}
else {
print("Can't add audio input.")
return (assetWriter, videoInput, nil)
}
return (assetWriter, videoInput, audioInput)
}
static func prepareAssetReader(filePath filePath: String) ->
(reader: AVAssetReader?,
videoOutput: AVAssetReaderTrackOutput?,
audioOutput: AVAssetReaderTrackOutput?)
{
var assetReader: AVAssetReader?
var videoOutput: AVAssetReaderTrackOutput?
var audioOutput: AVAssetReaderTrackOutput?
let url = NSURL.fileURLWithPath(filePath)
let asset = AVAsset(URL: url)
do {
assetReader = try AVAssetReader(asset: asset)
}
catch {
print("Can't create asset reader.")
return (nil, nil, nil)
}
// Video output
let videoTracks = asset.tracksWithMediaType(AVMediaTypeVideo)
if videoTracks.count > 0 {
let outputVideoSetting: [String: AnyObject] = [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(unsignedInt: kCVPixelFormatType_32ARGB),
String(kCVPixelBufferIOSurfacePropertiesKey): [:]]
videoOutput = AVAssetReaderTrackOutput(track: videoTracks.first!, outputSettings: outputVideoSetting)
if assetReader!.canAddOutput(videoOutput!) {
assetReader!.addOutput(videoOutput!)
}
else {
print("Can't add video ouput.")
return (nil, nil, nil)
}
}
// Audio output
let audioTracks = asset.tracksWithMediaType(AVMediaTypeAudio)
if audioTracks.count > 0 {
let audioSettings: [String : AnyObject] = [AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVNumberOfChannelsKey: 2]
audioOutput = AVAssetReaderTrackOutput(track: audioTracks.first!, outputSettings: audioSettings)
if assetReader!.canAddOutput(audioOutput!) {
assetReader!.addOutput(audioOutput!)
}
else {
print("Can't add audio ouput.")
return (assetReader, videoOutput, nil)
}
}
return (assetReader, videoOutput, audioOutput)
}
static func findFormatDescription(filePath: String) ->
(videoFormatHint: CMFormatDescriptionRef?,
audioFormatHint: CMFormatDescriptionRef?)
{
let tuple: (reader: AVAssetReader?,
videoOutput: AVAssetReaderTrackOutput?,
audioOutput: AVAssetReaderTrackOutput?) = prepareAssetReader(filePath: filePath)
var assetReader: AVAssetReader
if let test = tuple.reader {
assetReader = test
}
else {
print("Can't create asset reader.")
return (nil, nil)
}
var videoOutput: AVAssetReaderTrackOutput
if let test = tuple.videoOutput {
videoOutput = test
}
else {
print("Can't create video output")
return (nil, nil)
}
let audioOutput: AVAssetReaderTrackOutput? = tuple.audioOutput
if !assetReader.startReading() {
print("Can't start reading.")
return (nil, nil)
}
var videoFormatHint: CMFormatDescriptionRef?
var audioFormatHint: CMFormatDescriptionRef?
while assetReader.status == .Reading
{
if let sampleBuffer = videoOutput.copyNextSampleBuffer() {
videoFormatHint = CMSampleBufferGetFormatDescription(sampleBuffer)
}
if let sampleBuffer = audioOutput?.copyNextSampleBuffer() {
audioFormatHint = CMSampleBufferGetFormatDescription(sampleBuffer)
}
return (videoFormatHint, audioFormatHint)
}
return (nil, nil)
}
static func createSilentAudioSampleBufferWithFormatDescription(formatDescription:CMAudioFormatDescription,
numSamples: Int,
presentationTime: CMTime) -> CMSampleBuffer?
{
let audioStreamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription).memory
let blockSize: size_t = numSamples * Int(audioStreamBasicDescription.mBytesPerFrame); // TODO: Probably need to calc in another way
var blockPointer: CMBlockBuffer? = nil
CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
nil,
blockSize,
nil,
nil,
0,
blockSize,
0,
&blockPointer);
guard let block = blockPointer else {
return nil
}
CMBlockBufferFillDataBytes(0, block, 0, blockSize);
var sampleBuffer: CMSampleBuffer? = nil;
CMAudioSampleBufferCreateReadyWithPacketDescriptions(kCFAllocatorDefault,
block,
formatDescription,
numSamples,
presentationTime,
nil,
&sampleBuffer);
return sampleBuffer;
}
}