我試圖只從靜態圖像寫入兩幀來構建視頻。我一直在徘徊在時間參數上。似乎最後一步finishWritingWithCompletionHandler從來沒有被稱爲(完成寫作...從來沒有輸出)。只有一個零字節.mp4視頻創建。並沒有發生錯誤。無法弄清楚原因。下面是我使用的代碼:iOS Swift:嘗試使用寫入圖像緩衝區來構建視頻,finishWritingWithCompletionHandler未被調用。輸出視頻包含零字節
func createBackgroundVideo(CompletionHandler: (path: String)->Void) {
var maybeError: NSError?
let fileMgr = NSFileManager.defaultManager()
let docDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents")
let videoOutputPath = docDirectory.stringByAppendingPathComponent(BgVideoName)
if (!fileMgr.removeItemAtPath(videoOutputPath, error: &maybeError)) {
NSLog("Umable to delete file: %@", maybeError!.localizedDescription)
}
println(videoOutputPath)
let videoWriter = AVAssetWriter(
URL: NSURL(fileURLWithPath: videoOutputPath),
fileType: AVFileTypeQuickTimeMovie,
error: &maybeError
)
var videoSettings = [
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: NSNumber(float: Float(videoWidth)),
AVVideoHeightKey: NSNumber(float: Float(videoHeight))
]
var avAssetInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)
avAssetInput.expectsMediaDataInRealTime = true
var adaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: avAssetInput, sourcePixelBufferAttributes: nil)
videoWriter.addInput(avAssetInput)
videoWriter.startWriting()
videoWriter.startSessionAtSourceTime(kCMTimeZero)
var frameCount: Int64 = 0;
var buffer: CVPixelBufferRef
//buffer = PixelBuffer.pixelBufferFromCGImage2(self.bgImage.CGImage, andSize: CGSizeMake(videoWidth, videoHeight)).takeUnretainedValue()
for i in 1...2 {
buffer = PixelBuffer.pixelBufferFromCGImage2(self.bgImage.CGImage, andSize: CGSizeMake(videoWidth, videoHeight)).takeUnretainedValue()
var appendOk = false
var retries: Int = 0
while (!appendOk && retries < 30) {
if (adaptor.assetWriterInput.readyForMoreMediaData) {
let frameTime = CMTimeMake(frameCount, 1);
appendOk = adaptor.appendPixelBuffer(buffer, withPresentationTime: frameTime)
if (!appendOk) {
println("some erorr occurred", videoWriter.error)
} else {
println("pixel written")
}
} else {
println("adaptor is not ready....")
NSThread.sleepForTimeInterval(0.1)
}
retries++
}
if (!appendOk) {
println("Error appending image....")
}
frameCount++
}
avAssetInput.markAsFinished()
videoWriter.finishWritingWithCompletionHandler({() -> Void in
println("finished writing...")
CompletionHandler(path: videoOutputPath)
})
}
我打電話從用的OBJ-C CGImage方法像素緩衝器,(我已經添加頁眉和橋接頭,似乎工作正常):
+ (CVPixelBufferRef) pixelBufferFromCGImage2: (CGImageRef) image andSize:(CGSize) size {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
size.width,
size.height,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef) options,
&pxbuffer);
if (status != kCVReturnSuccess){
NSLog(@"Failed to create pixel buffer");
}
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
size.height, 8, 4*size.width, rgbColorSpace,
kCGImageAlphaPremultipliedFirst);
float offsetY = size.height/2 - CGImageGetHeight(image)/2;
float offsetX = size.width/2 - CGImageGetWidth(image)/2;
CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
CGContextDrawImage(context, CGRectMake(offsetX, offsetY, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
感謝您的閱讀。
問題是什麼? – abhi1992 2016-01-13 07:36:46