Сжатие видео размер файла - PullRequest
       55

Сжатие видео размер файла

0 голосов
/ 14 октября 2018

Я хочу сжать видеофайл и отправить его на сервер. Я пытаюсь записать видео на устройство iphone 7Plus с разрешением 1080p HD при 60 кадрах в секунду в течение 1 минуты, это видео размером 122,1 МБ, а после сжатия видео - 32 МГ, теперь я пытаюсьизмените размер битрейта и размер видео, но размер сжатия не изменится.Как можно сжать видео большего размера.

func compress(fileName:String,videoPath : String, exportVideoPath : String, renderSize : CGSize, completion : @escaping (Bool) -> ()) {

    let videoUrl = URL(fileURLWithPath: videoPath)
    let videoAssetUrl = AVURLAsset(url: videoUrl)
    let videoTrackArray = videoAssetUrl.tracks(withMediaType: AVMediaTypeVideo)

    if videoTrackArray.count < 1 {
      completion(false)
      return
    }
    let videoAssetTrack = videoTrackArray[0]
    let audioTrackArray = videoAssetUrl.tracks(withMediaType: AVMediaTypeAudio)

    if audioTrackArray.count < 1 {
      completion(false)
      return
    }
    let audioAssetTrack = audioTrackArray[0]
    let outputUrl = URL(fileURLWithPath: exportVideoPath)
    var videoWriter = try? AVAssetWriter(url: outputUrl, fileType: AVFileTypeQuickTimeMovie)
    videoWriter?.shouldOptimizeForNetworkUse = true

    let vSetting = videoSettings(size: renderSize)
    let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: vSetting)
    videoWriterInput.expectsMediaDataInRealTime = false
    videoWriterInput.transform = videoAssetTrack.preferredTransform
    videoWriter?.add(videoWriterInput)

    let aSettings = audioSettings()
    let audioWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: aSettings)
    audioWriterInput.expectsMediaDataInRealTime = false
    audioWriterInput.transform = audioAssetTrack.preferredTransform
    videoWriter?.add(audioWriterInput)

    let videoReaderSettings : [String : Int] = [kCVPixelBufferPixelFormatTypeKey as String : Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)]
    let videoReaderOutput = AVAssetReaderTrackOutput(track: videoAssetTrack, outputSettings: videoReaderSettings)
    let videoReader = try! AVAssetReader(asset: videoAssetUrl)
    videoReader.add(videoReaderOutput)
    var settings = [String : AnyObject]()
    settings[AVFormatIDKey] = Int(kAudioUnitRange) as AnyObject
    let audioReaderOutput = AVAssetReaderTrackOutput(track: videoAssetTrack, outputSettings: nil)
    let audioReader = try! AVAssetReader(asset: videoAssetUrl)
    audioReader.add(audioReaderOutput)

    videoWriter?.startWriting()
    videoReader.startReading()
    videoWriter?.startSession(atSourceTime: kCMTimeZero)

    let processingVideoQueue = DispatchQueue(label: "processingVideoCompressionQueue")
    videoWriterInput.requestMediaDataWhenReady(on: processingVideoQueue, using: {
      while(videoWriterInput.isReadyForMoreMediaData){
        let sampleVideoBuffer = videoReaderOutput.copyNextSampleBuffer()
        if (videoReader.status == .reading && sampleVideoBuffer != nil) {
          videoWriterInput.append(sampleVideoBuffer!)
        }else {
          videoWriterInput.markAsFinished()
          if (videoReader.status == .completed) {

                    videoWriter?.finishWriting(completionHandler: {
                      videoWriter = nil
                      self.export = true
                      completion(true)

                    })

          }
        }
        if self.export {
          break
        }
      }
    })
  }

  func videoSettings(size : CGSize) -> [String : AnyObject] {

    var compressionSettings = [String : AnyObject]()
    compressionSettings[AVVideoAverageBitRateKey] = 425000 as AnyObject

    var settings = [String : AnyObject]()
    settings[AVVideoCompressionPropertiesKey] = compressionSettings as AnyObject
    if #available(iOS 11.0, *) {
      settings[AVVideoCodecKey] = AVVideoCodecHEVC as AnyObject?
    } else {
      settings[AVVideoCodecKey] = AVVideoCodecH264 as AnyObject?
    }
    settings[AVVideoHeightKey] = size.height as AnyObject?
    settings[AVVideoWidthKey] = size.width as AnyObject?

    return settings
  }
...