fork(7) download
  1. var selectedPhotosArray = [UIImage]()
  2.  
  3. var imageArrayToVideoURL = URL()
  4.  
  5. imageArrayToVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/outputVideo.mp4")
  6.  
  7. guard let videoWriter = try? AVAssetWriter(outputURL: imageArrayToVideoURL as URL, fileType: AVFileTypeMPEG4) else {
  8. fatalError("AVAssetWriter error")
  9. }
  10. let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
  11. guard videoWriter.canApply(outputSettings: outputSettings, forMediaType: AVMediaTypeVideo) else {
  12. fatalError("Negative : Can't apply the Output settings...")
  13. }
  14. let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)
  15. let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
  16. let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoWriterInput, sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary)
  17. if videoWriter.canAdd(videoWriterInput) {
  18. videoWriter.add(videoWriterInput)
  19. }
  20. if videoWriter.startWriting() {
  21. let zeroTime = CMTimeMake(Int64(1),Int32(1))
  22. videoWriter.startSession(atSourceTime: zeroTime)
  23.  
  24. assert(pixelBufferAdaptor.pixelBufferPool != nil)
  25. let media_queue = DispatchQueue(label: "mediaInputQueue")
  26. videoWriterInput.requestMediaDataWhenReady(on: media_queue, using: { () -> Void in
  27. let fps: Int32 = 1
  28. let framePerSecond: Int64 = Int64(1)
  29. let frameDuration = CMTimeMake(framePerSecond, fps)
  30. var frameCount: Int64 = 0
  31. var appendSucceeded = true
  32. while (!selectedPhotosArray.isEmpty) {
  33. if (videoWriterInput.isReadyForMoreMediaData) {
  34. let nextPhoto = selectedPhotosArray.remove(at: 0)
  35. let lastFrameTime = CMTimeMake(frameCount * framePerSecond, fps)
  36. let presentationTime = frameCount == 0 ? lastFrameTime : CMTimeAdd(lastFrameTime, frameDuration)
  37. var pixelBuffer: CVPixelBuffer? = nil
  38. let status: CVReturn = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pixelBufferAdaptor.pixelBufferPool!, &pixelBuffer)
  39. if let pixelBuffer = pixelBuffer, status == 0 {
  40. let managedPixelBuffer = pixelBuffer
  41. CVPixelBufferLockBaseAddress(managedPixelBuffer, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
  42. let data = CVPixelBufferGetBaseAddress(managedPixelBuffer)
  43. let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
  44. let context = CGContext(data: data, width: Int(outputSize.width), height: Int(outputSize.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(managedPixelBuffer), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
  45. context!.clear(CGRect(x: 0, y: 0, width: CGFloat(outputSize.width), height: CGFloat(outputSize.height)))
  46. let horizontalRatio = CGFloat(outputSize.width) / nextPhoto.size.width
  47. let verticalRatio = CGFloat(outputSize.height) / nextPhoto.size.height
  48. //aspectRatio = max(horizontalRatio, verticalRatio) // ScaleAspectFill
  49. let aspectRatio = min(horizontalRatio, verticalRatio) // ScaleAspectFit
  50. let newSize: CGSize = CGSize(width: nextPhoto.size.width * aspectRatio, height: nextPhoto.size.height * aspectRatio)
  51. let x = newSize.width < outputSize.width ? (outputSize.width - newSize.width) / 2 : 0
  52. let y = newSize.height < outputSize.height ? (outputSize.height - newSize.height) / 2 : 0
  53. context?.draw(nextPhoto.cgImage!, in: CGRect(x: x, y: y, width: newSize.width, height: newSize.height))
  54. CVPixelBufferUnlockBaseAddress(managedPixelBuffer, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
  55. appendSucceeded = pixelBufferAdaptor.append(pixelBuffer, withPresentationTime: presentationTime)
  56. } else {
  57. print("Failed to allocate pixel buffer")
  58. appendSucceeded = false
  59. }
  60. }
  61. if !appendSucceeded {
  62. break
  63. }
  64. frameCount += 1
  65. }
  66. videoWriterInput.markAsFinished()
  67. videoWriter.finishWriting { () -> Void in
  68. print(“complete”)
  69. }
  70. })
  71. }
  72.  
  73.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.swift:1:30: error: use of unresolved identifier 'UIImage'
                var selectedPhotosArray = [UIImage]()
                                           ^~~~~~~
prog.swift:3:30: error: use of unresolved identifier 'URL'
                var imageArrayToVideoURL = URL()
                                           ^~~
prog.swift:5:32: error: use of unresolved identifier 'NSURL'
        imageArrayToVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/outputVideo.mp4")
                               ^~~~~
prog.swift:5:55: error: use of unresolved identifier 'NSHomeDirectory'
        imageArrayToVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/outputVideo.mp4")
                                                      ^~~~~~~~~~~~~~~
prog.swift:7:38: error: use of unresolved identifier 'AVAssetWriter'
        guard let videoWriter = try? AVAssetWriter(outputURL: imageArrayToVideoURL as URL, fileType: AVFileTypeMPEG4) else {
                                     ^~~~~~~~~~~~~
prog.swift:7:102: error: use of unresolved identifier 'AVFileTypeMPEG4'
        guard let videoWriter = try? AVAssetWriter(outputURL: imageArrayToVideoURL as URL, fileType: AVFileTypeMPEG4) else {
                                                                                                     ^~~~~~~~~~~~~~~
prog.swift:10:31: error: use of unresolved identifier 'AVVideoCodecKey'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                              ^~~~~~~~~~~~~~~
prog.swift:10:49: error: use of unresolved identifier 'AVVideoCodecH264'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                ^~~~~~~~~~~~~~~~
prog.swift:10:67: error: use of unresolved identifier 'AVVideoWidthKey'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                  ^~~~~~~~~~~~~~~
prog.swift:10:85: error: use of unresolved identifier 'NSNumber'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                                    ^~~~~~~~
prog.swift:10:107: error: use of unresolved identifier 'outputSize'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                                                          ^~~~~~~~~~
prog.swift:10:127: error: use of unresolved identifier 'AVVideoHeightKey'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                                                                              ^~~~~~~~~~~~~~~~
prog.swift:10:146: error: use of unresolved identifier 'NSNumber'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                                                                                                 ^~~~~~~~
prog.swift:10:168: error: use of unresolved identifier 'outputSize'
        let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height))] as [String : Any]
                                                                                                                                                                       ^~~~~~~~~~
prog.swift:11:82: error: use of unresolved identifier 'AVMediaTypeVideo'
        guard videoWriter.canApply(outputSettings: outputSettings, forMediaType: AVMediaTypeVideo) else {
                                                                                 ^~~~~~~~~~~~~~~~
prog.swift:14:32: error: use of unresolved identifier 'AVAssetWriterInput'
        let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)
                               ^~~~~~~~~~~~~~~~~~
prog.swift:14:62: error: use of unresolved identifier 'AVMediaTypeVideo'
        let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)
                                                             ^~~~~~~~~~~~~~~~
prog.swift:15:54: error: use of unresolved identifier 'kCVPixelBufferPixelFormatTypeKey'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.swift:15:99: error: use of unresolved identifier 'NSNumber'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                  ^~~~~~~~
prog.swift:15:115: error: use of unresolved identifier 'kCVPixelFormatType_32ARGB'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~
prog.swift:15:143: error: use of unresolved identifier 'kCVPixelBufferWidthKey'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                              ^~~~~~~~~~~~~~~~~~~~~~
prog.swift:15:177: error: use of unresolved identifier 'NSNumber'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                                                                ^~~~~~~~
prog.swift:15:199: error: use of unresolved identifier 'outputSize'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                                                                                      ^~~~~~~~~~
prog.swift:15:219: error: use of unresolved identifier 'kCVPixelBufferHeightKey'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                                                                                                          ^~~~~~~~~~~~~~~~~~~~~~~
prog.swift:15:254: error: use of unresolved identifier 'NSNumber'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                                                                                                                                             ^~~~~~~~
prog.swift:15:276: error: use of unresolved identifier 'outputSize'
        let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(value: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(value: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(value: Float(outputSize.height))]
                                                                                                                                                                                                                                                                                   ^~~~~~~~~~
prog.swift:16:34: error: use of unresolved identifier 'AVAssetWriterInputPixelBufferAdaptor'
        let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoWriterInput, sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary)
                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.swift:68:38: error: unicode curly quote found, replace with '"'
                    print(“complete”)
prog.swift:68:27: error: unicode curly quote found, replace with '"'
                    print(“complete”)
prog.swift:21:28: error: use of unresolved identifier 'CMTimeMake'
            let zeroTime = CMTimeMake(Int64(1),Int32(1))
                           ^~~~~~~~~~
prog.swift:25:31: error: use of unresolved identifier 'DispatchQueue'
            let media_queue = DispatchQueue(label: "mediaInputQueue")
                              ^~~~~~~~~~~~~
<unknown>:0: error: error opening input file 'prog.o' (No such file or directory
)
clang: error: no such file or directory: 'prog.o'
clang: error: no such file or directory: '@prog.autolink'
stdout
Standard output is empty