gpt4 book ai didi

swift - 在 Swift 中降低视频帧率以获得 GIF 感觉

转载 作者:行者123 更新时间:2023-11-30 10:43:33 27 4
gpt4 key购买 nike

我一直在尝试将视频的帧速率降低到每秒 10 或 15 帧。有一些4/5年前的OBJC相关问题。我在 swift 中找不到任何东西..

这就是我在 Playground 上所做的工作:

import Cocoa
import AVKit


var assetWriter:AVAssetWriter?
var assetReader:AVAssetReader?
let bitrate:NSNumber = NSNumber(value:8000000)

func compressFile(urlToCompress: URL, outputURL: URL, completion:@escaping (URL)->Void){

var videoFinished = false

let asset = AVAsset(url: urlToCompress);

do{
assetReader = try AVAssetReader(asset: asset)
} catch{
assetReader = nil
}

guard let reader = assetReader else{
fatalError("Could not initalize asset reader probably failed its try catch")
}

let videoTrack = asset.tracks(withMediaType: AVMediaType.video).first!

let videoReaderSettings: [String:Any] = [(kCVPixelBufferPixelFormatTypeKey as String?)!:kCVPixelFormatType_32ARGB ]

let videoSettings:[String:Any] = [
AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey:bitrate],
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoHeightKey: videoTrack.naturalSize.height,
AVVideoWidthKey: videoTrack.naturalSize.width
]

let assetReaderVideoOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: videoReaderSettings)

if reader.canAdd(assetReaderVideoOutput){
reader.add(assetReaderVideoOutput)
}else{
fatalError("Couldn't add video output reader")
}

let videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoSettings)
videoInput.transform = videoTrack.preferredTransform

let videoInputQueue = DispatchQueue(label: "videoQueue")

do{
assetWriter = try AVAssetWriter(outputURL: outputURL, fileType: AVFileType.mov)
assetWriter?.movieTimeScale = 1
}catch{
assetWriter = nil
}
guard let writer = assetWriter else{
fatalError("assetWriter was nil")
}

writer.shouldOptimizeForNetworkUse = true
writer.add(videoInput)


writer.startWriting()
reader.startReading()
writer.startSession(atSourceTime: CMTime.zero)


let closeWriter:()->Void = {
// if (audioFinished && videoFinished){
if (videoFinished){
assetWriter?.finishWriting(completionHandler: {

checkFileSize(sizeUrl: (assetWriter?.outputURL)!, message: "The file size of the compressed file is: ")
completion((assetWriter?.outputURL)!)

})

assetReader?.cancelReading()

}
}

videoInput.requestMediaDataWhenReady(on: videoInputQueue) {
while(videoInput.isReadyForMoreMediaData){
let sample = assetReaderVideoOutput.copyNextSampleBuffer()

if (sample != nil){
videoInput.append(sample!)
}else{
videoInput.markAsFinished()

DispatchQueue.main.async {
videoFinished = true
closeWriter()
}
break;
}
}

}


}

func checkFileSize(sizeUrl: URL, message:String){
let data = NSData(contentsOf: sizeUrl)!
print(message, (Double(data.length) / 1048576.0), " mb")
}


compressFile(urlToCompress: URL(fileURLWithPath: "/Users/koraybirand/Desktop/video.mp4"), outputURL: URL(fileURLWithPath: "/Users/koraybirand/Desktop/video5.mp4")) { (URL) in

}

最佳答案

尝试在视频设置中添加AVVideoAverageNonDroppableFrameRateKey

关于swift - 在 Swift 中降低视频帧率以获得 GIF 感觉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com