我想修剪视频并进行压缩,然后将文件上传到服务器,在服务器端,我的大小限制为25 MB,时间限制为1分钟。我正在使用PryntTrimmerView窗格最多将视频修剪一分钟,然后将修剪的视频压缩到25 MB。 我有用于压缩视频的代码,我在该主题中进行了很多搜索,但我找不到压缩视频的地方。 我测试了两个视频文件,它们分别是磁盘上的173.9 MB和34.6 MB。 现在压缩后173.9的大小减小到50.71,34.6减小到54.44,我不知道为什么34.6减小到54.44!如何将视频压缩到磁盘上的25 MG? 有我的代码用于压缩视频并获取视频大小:
func compress(fileName:String,videoPath : String, exportVideoPath : String, 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 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 videoReaderSettings:[String : AnyObject] = [
kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) as AnyObject
]
let videoReaderOutput = AVAssetReaderTrackOutput(track: videoAssetTrack, outputSettings: videoReaderSettings)
let videoReader = try! AVAssetReader(asset: videoAssetUrl)
videoReader.add(videoReaderOutput)
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() -> [String : AnyObject] {
var compressionSettings = [String : AnyObject]()
// compressionSettings[AVVideoAverageBitRateKey] = 425000
compressionSettings[AVVideoAverageBitRateKey] = 1 as AnyObject
compressionSettings[AVVideoProfileLevelKey] = AVVideoProfileLevelH264Main30 as AnyObject;
compressionSettings[AVVideoMaxKeyFrameIntervalKey] = 1 as AnyObject
var settings = [String : AnyObject]()
settings[AVVideoCompressionPropertiesKey] = compressionSettings as AnyObject
settings[AVVideoCodecKey] = AVVideoCodecH264 as AnyObject?
settings[AVVideoHeightKey] = 500 as AnyObject?
settings[AVVideoWidthKey] = 720 as AnyObject?
return settings
}
func getFileSize() {
var fileSize : UInt64
do {
let attr:NSDictionary? = try FileManager.default.attributesOfItem(atPath: path) as NSDictionary
if let _attr = attr {
fileSize = _attr.fileSize();
print(fileSize)
var sizeMB = Double(fileSize / 1024)
sizeMB = Double(sizeMB / 1024)
print(String(format: "%.2f", sizeMB) + " MB")
if sizeMB > 25 {
showVideoWrongSizeAlert()
return
}
}
}catch {
print(error.localizedDescription)
}