我有一个严重的问题:我有NSArray几个UIImage对象.我现在想做的是从那些创作电影UIImages.但我不知道该怎么做.
我希望有人可以帮助我或给我一个代码片段,它可以做我喜欢的事情.
编辑:供将来参考 - 应用解决方案后,如果视频看起来扭曲,请确保您捕获的图像/区域的宽度是16的倍数.在这里经过几个小时的努力后发现:
为什么我的UIImages电影得到了扭曲?
这是完整的解决方案(只需确保宽度是16的倍数)
http://codethink.no-ip.org/wordpress/archives/673
我想知道是否有人可以告诉我如何实现这一目标.如果想到几个解决方案:
从捕获的视频创建单独的图像,然后在每个图像中合并它们,之后创建一个新的AVAsset ...听起来有点复杂你不觉得吗?
合并2个视频,一个是透明的(一个是水印),另一个是用相机拍摄的资产.
我现在已经完全设置了使用AVFoundation框架录制视频的功能,这一切都很好,但现在我希望在录制期间添加叠加层(也可以在AVCaptureVideoPreviewLayer层上看到)
我可以将此叠加UIView对象添加到VideoPreviewLayer,但我正在努力如何在录制的视频上获得相同的视图.这UIView可能包含从UILabels到UIImageViews的任何内容.
我正在尝试使用iOS 4.3上的AVMutableComposition,AVMutableVideoComposition和AVVideoCompositionCoreAnimationTool将CALayer烘焙成纵向模式视频(导出时).这一切都在景观中起作用.但是,如果我以纵向捕捉视频,AVVideoCompositionCoreAnimationTool将忽略视频轨道上的变换.也就是说,对于纵向模式视频,我将AVMutableCompositionTrack.preferredTransform设置为原始资产视频轨道中的preferredTransform值.只要我不使用AVVideoCompositionCoreAnimationTool,这就可以了,视频以纵向模式显示.但是,只要我添加AVVideoCompositionCoreAnimationTool和CALayer,该文件就会出现.(CALayer显示正确,但背后的视频位于其侧面,并且文件的宽高比关闭).我尝试将变换应用于CALayer,并在ACVideoComposition中设置变换.这些都不会改变生成的文件的方向(它仍然是480x369,而不是360x480).有没有办法用AVVideoCompositionCoreAnimationTool渲染肖像模式视频?
首先,我设置了一个AVMutableComposition和AVMutableVideoComposition
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize videoSize = CGSizeApplyAffineTransform(clipVideoTrack.naturalSize, clipVideoTrack.preferredTransform);
videoSize.width = fabs(videoSize.width);
videoSize.height = fabs(videoSize.height);
CMTime titleDuration = CMTimeMakeWithSeconds(5, 600);
CMTimeRange titleRange = CMTimeRangeMake(kCMTimeZero, titleDuration);
[compositionVideoTrack insertTimeRange:titleRange ofTrack:nil atTime:kCMTimeZero error:nil];
[compositionVideoTrack insertTimeRange:timeRange ofTrack:clipVideoTrack atTime:titleDuration error:nil];
compositionVideoTrack.preferredTransform = clipVideoTrack.preferredTransform;
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, [composition …Run Code Online (Sandbox Code Playgroud) 编辑:最奇怪的事情:似乎从完整的应用程序运行此代码一切正常,但我总是从我的单元测试运行电影的创建,只有它没有工作.试图弄清楚为什么......
我正在尝试使用AVMutableComposition合并视频+音频+文本并将其导出到新视频.
我的代码基于WWDC '10的AVEditDemo
我在CATextLayer中添加了一个紫色背景,所以我可以知道它被导出到电影中,但没有显示文字......我尝试使用各种字体,位置,颜色定义,但没有任何帮助,所以我决定在这里发布代码,看看是否有人偶然发现了类似的东西,并告诉我我错过了什么.
这是代码(self.audio和self.video是AVURLAssets):
CMTime exportDuration = self.audio.duration;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *videoTrack = [[self.video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// add the video in loop until the audio ends
CMTime currStartTime = kCMTimeZero;
while (CMTimeCompare(currStartTime, exportDuration) < 0) {
CMTime timeRemaining = CMTimeSubtract(exportDuration, currStartTime);
CMTime currLoopDuration = self.video.duration;
if (CMTimeCompare(currLoopDuration, timeRemaining) > 0) {
currLoopDuration = timeRemaining;
}
CMTimeRange currLoopTimeRange = CMTimeRangeMake(kCMTimeZero, currLoopDuration);
[compositionVideoTrack insertTimeRange:currLoopTimeRange ofTrack:videoTrack
atTime:currStartTime error:nil];
currStartTime = CMTimeAdd(currStartTime, …Run Code Online (Sandbox Code Playgroud) 我正在捕获视频并将其转换为CGImage以对其进行处理.它将工作约10秒,获取内存警告然后崩溃(通常它说数据格式化程序暂时不可用).有人可以帮我解决问题吗?
- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// CONVERT CMSAMPLEBUFFER INTO A CGIMAGE
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef cgimage = CGBitmapContextCreateImage(newContext);
UIImage *sourceImage= [UIImage imageWithCGImage:cgimage scale:1.0f orientation:UIImageOrientationLeftMirrored];
CGImageRelease(cgimage);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// ***
// Adding code after this point to do image transformation usually …Run Code Online (Sandbox Code Playgroud) 我正在业余时间为 iOS 开发一个视频编辑应用程序。
在参加其他 rpojects 几周后,我刚刚恢复工作,并且 - 即使我没有对代码进行任何重大更改 -现在每次我尝试导出我的视频作品时它都会崩溃。
我检查并构建了我当时成功上传到 TestFlight 的完全相同的提交(并且它在多个设备上工作而没有崩溃),所以这可能是我从那时起更新的最新 Xcode / iOS SDK 的问题?
代码在_xpc_api_misuse上崩溃,在一个线程上:
com.apple.coremedia.basicvideocompositor.output
Run Code Online (Sandbox Code Playgroud)
调试导航器:
在崩溃时,调试导航器上有 70 多个线程,所以可能出现问题并且应用程序使用了太多线程(从未见过这么多)。
我的应用程序使用文本层在导出的视频上覆盖“水印”。玩了一圈后,我发现如果我注释掉水印代码,可以避免崩溃:
guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else {
return failure(ProjectError.failedToCreateExportSession)
}
guard let documents = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
return failure(ProjectError.temporaryOutputDirectoryNotFound)
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd_HHmmss"
let fileName = dateFormatter.string(from: Date())
let fileExtension = "mov"
let fileURL = …Run Code Online (Sandbox Code Playgroud) 我找到了一些示例,说明如何在视频上添加文本叠加层.
Ray的教程 - http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos
这个答案 - 如何在iOS上捕获的视频中添加水印
我还引用了其他几个.我的代码看起来与答案几乎完全相同,我一直在尝试调整它以使文本覆盖仅在视频开头显示一两秒钟.有关如何实现这一目标的任何帮助?
这是我的代码.这样可以导出视频,并在整个持续时间内显示叠加层.
if(vA) {
videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoCompositionTrack insertTimeRange:videoTimerange ofTrack:[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:&error];
[videoCompositionTrack setPreferredTransform:[[[vA tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];
if(error)
NSLog(@"%@", error);
}
if(aA) {
audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioCompositionTrack insertTimeRange:audioTimerange ofTrack:[[aA tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error];
if(error)
NSLog(@"%@", error);
}
CGSize videoSize = [videoCompositionTrack naturalSize];
UIImage *myImage = [UIImage imageNamed:@"cover.png"];
CALayer *aLayer = [CALayer layer];
aLayer.contents = (id)myImage.CGImage;
aLayer.frame = CGRectMake(5, 25, 100, 56);
aLayer.opacity = 0.7;
CATextLayer *titleLayer = …Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何方法可以为当前正在录制的视频添加水印并将其与水印一起保存。(我知道将水印添加到应用程序包中已经可用的视频文件并使用水印导出)。
我检查了这个链接。接受的答案不是好的答案。仅当您的捆绑包中已有视频文件时,投票最多的答案才适用。(请在建议之前阅读答案。)
提前致谢
avfoundation ×8
ios ×8
iphone ×4
avasset ×1
camera ×1
catextlayer ×1
export ×1
ipad ×1
objective-c ×1
uiimage ×1
xcode ×1