我已使用以下代码从图像成功创建了视频
-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration
{
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];
//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
//Write samples:
for (int i = …Run Code Online (Sandbox Code Playgroud) 似乎在iOS 6模拟器上打破了finishWriting - 它永远挂起.它现在已被弃用,并被新的finishWritingWithCompletionHandler取代: - 它也永远不会调用处理程序.
在运行iOS 6的真实设备上,这样可以正常工作.同样在之前的iOS模拟器中它运行得很好.看起来像iOS 6模拟器中的错误.
有其他人遇到这个或可以证明我错了吗?
我正在尝试将单个视频与单个图像组合在一起.这不是试图将许多图像组合成单个视频,例如
我AVMutableComposition用来组合曲目.我的应用程序能够组合视频和图像(但就目前而言,视频组合很好!)我尝试使用AVAssetWriter将单个图像转换为视频(我相信这是我的问题,但不是100%肯定) .然后我将其保存到app(documents directory).从那里,我在合并中访问它,并将视频和现在变成视频的图像组合在一起.
流程:
用户选择图像 - >
将图像转换为AVAssetWriter以更改为视频 - >
合并我已预设的视频视频 - >
结果:从所选图像和预设视频制作1个视频.
我的问题:我的代码提供了一个空白区域,视频内部的图像应该是.就像在我的ImageConverter文件中一样,它会将它转换为视频,但我只会看到最后一帧作为图像,而每隔一帧都是透明的,好像图片不存在一样.因此,如果我将图像转换为视频5秒钟(假设是30帧/秒),那么我将看到(30*5)-1帧的空白区域,然后是最后一帧,最终会出现图片.我正在寻找有关如何将单个图像制作成视频或将视频和图像组合在一起而不将图像转换为视频的指导.谢谢!
合并文件在这里
func merge() {
if let firstAsset = controller.firstAsset, secondAsset = self.asset {
// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
let mixComposition = AVMutableComposition()
let firstTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
try firstTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, CMTime(seconds: 8, preferredTimescale: 600)),
ofTrack: firstAsset.tracksWithMediaType(AVMediaTypeVideo)[0] ,
atTime: kCMTimeZero)
} …Run Code Online (Sandbox Code Playgroud) 将视频保存到iPhone库时出现问题.我有一个UIImages数组,还有两个按钮,"convertToVideo"和"saveToiPhoneLib"
-(IBAction) convertToVideo
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *savedVideoPath = [documentsDirectory stringByAppendingPathComponent:@"videoOutput"];
printf(" \n\n\n-Video file == %s--\n\n\n",[savedVideoPath UTF8String]);
[self writeImageAsMovie:imageArray toPath:savedVideoPath size:self.view.frame.size duration:3];
}
here i'm passing the imageArray and savedVideoPath to the function below
-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration
{
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey, … 我正在为视频写一些帧AVAssetWriterInputPixelBufferAdaptor,而且时间上的行为并不是我所期望的.
如果我只写一帧:
[videoWriter startSessionAtSourceTime:kCMTimeZero];
[adaptor appendPixelBuffer:pxBuffer withPresentationTime:kCMTimeZero];
Run Code Online (Sandbox Code Playgroud)
这给我一个长度为零的视频,这是我所期待的.
但是,如果我继续添加第二帧:
// 3000/600 = 5 sec, right?
CMTime nextFrame = CMTimeMake(3000, 600);
[adaptor appendPixelBuffer:pxBuffer withPresentationTime:nextFrame];
Run Code Online (Sandbox Code Playgroud)
我得到十秒的视频,我期待五个.
这里发生了什么?难道withPresentationTime莫名其妙定置了框架和持续时间都开始?
请注意,我没有要求endSessionAtSourceTime,只是finishWriting.
我正在开发一个功能上的视频压缩; 我的想法如下:
我有几个问题:
我尝试下面的代码压缩视频,但我不知道它压缩到哪个分辨率:
- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler
{
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: urlAsset presetName:AVAssetExportPresetLowQuality];
session.outputURL = outputURL;
session.outputFileType = AVFileTypeQuickTimeMovie;
[session exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(session);
}];
}
Run Code Online (Sandbox Code Playgroud)
请给我一些建议.提前致谢.
我正在尝试使用AVAssetWriter将CGImages写入文件以从图像创建视频.
我已经在模拟器上以三种不同的方式成功地工作了,但是在运行iOS 4.3的iPhone 4上,每种方法都失败了.
这一切都与像素缓冲区有关.
我的第一种方法是在不使用池的情况下根据需要创建像素缓冲区.这可行,但在内存密集,无法在设备上工作.
我的第二种方法是使用推荐的AVAssetWriterInputPixelBufferAdaptor,然后使用CVPixelBufferPoolCreatePixelBuffer从适配器pixelBufferPool中拉出像素缓冲区.
这也适用于模拟器,但在设备上失败,因为永远不会分配适配器的像素缓冲池.我没有收到任何错误消息.
最后,我尝试使用CVPixelBufferPoolCreate创建自己的像素缓冲池.这也适用于模拟器,但在设备上,一切正常,直到我尝试使用appendPixelBuffer追加像素缓冲区,每次都失败.
我在网上找到了非常少的信息.我的代码基于我发现的例子,但现在几天都没有运气.如果ANYONE有成功使用AVAssetWriter的经验,请查看并告诉我您是否看到任何不合适的地方.
注意:您将看到注释掉的尝试块.
首先,设置
- (BOOL) openVideoFile: (NSString *) path withSize:(CGSize)imageSize {
size = CGSizeMake (480.0, 320.0);//imageSize;
NSError *error = nil;
videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
if (error != nil)
return NO;
NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:size.width], AVVideoCleanApertureWidthKey,
[NSNumber numberWithDouble:size.height], AVVideoCleanApertureHeightKey,
[NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey,
[NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey,
nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], AVVideoPixelAspectRatioHorizontalSpacingKey,
[NSNumber numberWithInt:1],AVVideoPixelAspectRatioVerticalSpacingKey,
nil];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
//[NSNumber numberWithInt:960000], …Run Code Online (Sandbox Code Playgroud) 我试图在我的iOS设备上传之前旋转视频,因为其他平台(例如android)无法正确解释iOS录制的视频中的旋转信息,因此,播放它们是不正确旋转的.
我查看了以下堆栈帖子,但没有成功将其中任何一个应用于我的案例:
我处理了Apple AVSimpleEditor项目示例,但遗憾的是,在创建AVAssetExportSession并调用exportAsynchronouslyWithCompletionHandler时,没有执行任何旋转,更糟糕的是,旋转元数据从生成的文件中被删除.
以下是运行导出的代码:
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:[_mutableComposition copy] presetName:AVAssetExportPresetPassthrough];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileType3GPP;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.videoComposition = _mutableVideoComposition;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
NSLog(@"Status is %d %@", exportSession.status, exportSession.error);
handler(exportSession);
[exportSession release];
}];
Run Code Online (Sandbox Code Playgroud)
此方法在此处初始化值_mutableComposition和_mutableVideoComposition:
- (void) getVideoComposition:(AVAsset*)asset
{
AVMutableComposition *mutableComposition = nil;
AVMutableVideoComposition *mutableVideoComposition = nil;
AVMutableVideoCompositionInstruction *instruction = nil;
AVMutableVideoCompositionLayerInstruction *layerInstruction = nil;
CGAffineTransform t1;
CGAffineTransform t2;
AVAssetTrack *assetVideoTrack = nil;
AVAssetTrack *assetAudioTrack = nil;
// Check …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个项目,将物理模拟转换为iPhone本身的视频.
为此,我现在使用两个不同的循环.第一个循环在块中运行,其中AVAssetWriterInput对象轮询EAGLView以获取更多图像.EAGLView提供存储它们的数组中的图像.
另一个循环是实际模拟.我已经关闭了模拟计时器,并且每次都使用预先指定的时间差来调用tick.每次调用tick时,我都会在交换缓冲区后在EAGLView的交换缓冲区方法中创建一个新图像.然后将此图像放在AVAssetWriter轮询的数组中.
还有一些杂项代码,以确保数组不会太大
所有这一切都很好,但非常慢.
我在做什么,从概念上讲,导致整个过程比它可能更慢?另外,有没有人知道从glReadPixels获取Open GL图像的更快方法?
我正在使用AVCaptureSession从设备麦克风和摄像头捕获音频和视频样本.
然后我尝试编写通过AVCaptureSessions委托方法返回的CMSampleBuffers(使用AVAssetWriter和AVAssetWriterInputs)
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer: (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
Run Code Online (Sandbox Code Playgroud)
当我的音频AVAssetWriterInput配置为以Apple Lossless格式(kAudioFormatAppleLossless)写出数据时,这可以正常工作但是如果我尝试配置音频AVAssetWriterInput以使用AAC(kAudioFormatMPEG4AAC)它成功写入视频和音频样本大约500ms然后失败跟随错误
writer has failed with Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode" UserInfo=0x4b2630 {NSLocalizedFailureReason=The media data could not be decoded. It may be damaged., NSUnderlyingError=0x4ad0f0 "The operation couldn’t be completed. (OSStatus error 560226676.)", NSLocalizedDescription=Cannot Decode}
Run Code Online (Sandbox Code Playgroud)
这是我用来创建AVAssetWriter和AVAssetWriterInputs的代码
NSError *error = nil;
m_VideoCaputurePath = [[NSString stringWithFormat:@"%@/%@.mp4",[UserData getSavePath],[UserData getUniqueFilename]] retain];
if( USE_AAC_AUDIO )
{
m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeMPEG4 error:&error];
}
else
{
m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeQuickTimeMovie error:&error];
}
//\Configure …Run Code Online (Sandbox Code Playgroud) avassetwriter ×10
ios ×6
avfoundation ×5
iphone ×4
objective-c ×3
uiimage ×2
audio ×1
buffer ×1
cmtime ×1
cocoa-touch ×1
export ×1
ios4 ×1
opengl-es ×1
orientation ×1
swift ×1
video ×1