- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想根据自己的情况制作视频。用户将从库中选择视频。看看我的屏幕。有一个背景是UIView
。它的颜色是多变的。该 View 上方有一个 AVPlayer。我已根据所选视频的宽高比调整了 AVPlayer
的大小。现在我想构图整个场景并将其保存到照片库中。到目前为止我已经做到了。保存的视频应该像第二张图片一样。但它不提供随机视频。
- (IBAction)videoOutput:(id)sender
{
// 2 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
// 3 - Video track
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration) ofTrack:[[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
// 3.1 - Create AVMutableVideoCompositionInstruction
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration);
// 3.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp;
BOOL isVideoAssetPortrait_ = NO;
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) {
videoAssetOrientation_ = UIImageOrientationRight;
isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) {
videoAssetOrientation_ = UIImageOrientationLeft;
isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) {
videoAssetOrientation_ = UIImageOrientationUp;
}
if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {
videoAssetOrientation_ = UIImageOrientationDown;
}
CGSize naturalSize;
if(isVideoAssetPortrait_){
naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
} else {
naturalSize = videoAssetTrack.naturalSize;
}
NSLog(@"%@",NSStringFromCGSize(naturalSize));
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
// 3.3 - Add instructions
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.renderSize = CGSizeMake(320, 320);
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);
[self applyVideoEffectsToComposition:mainCompositionInst size:naturalSize withColor:[array objectAtIndex:colorTag]];
// 4 - Get path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = mainCompositionInst;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self exportDidFinish:exporter];
});
}];
}
- (void)exportDidFinish:(AVAssetExportSession*)session
{
if (session.status == AVAssetExportSessionStatusCompleted)
{
NSURL *outputURL = session.outputURL;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:outputURL completionBlock:^(NSURL *assetURL, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
});
}];
}
}
}
- (UIImage *)imageWithColor:(UIColor *)color rectSize:(CGRect)imageSize {
CGRect rect = imageSize;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)applyVideoEffectsToComposition:(AVMutableVideoComposition *)composition size:(CGSize)size withColor:(UIColor *)color
{
CGFloat temp = 1280;
CGSize videoSize = size;
CGFloat width = ((videoSize.width/videoSize.height)*temp);
CGFloat x = (temp - width)/2;
UIImage *borderImage = nil;
borderImage = [self imageWithColor:color rectSize:CGRectMake(0, 0, size.width, size.height)];
CALayer *backgroundLayer = [CALayer layer];
[backgroundLayer setContents:(id)[borderImage CGImage]];
backgroundLayer.frame = CGRectMake(0, 0, 720, 1280);
[backgroundLayer setMasksToBounds:YES];
CALayer *videoLayer = [CALayer layer];
videoLayer.frame = CGRectMake(x, 0, width-00, temp);
NSLog(@"video layer >> %@",NSStringFromCGRect(videoLayer.frame));
videoLayer.contentsGravity = kCAGravityBottomRight;
CALayer *parentLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, 720, 1280);
parentLayer.contentsGravity = kCAGravityBottomRight;
parentLayer.backgroundColor = [UIColor redColor].CGColor;
[parentLayer addSublayer:backgroundLayer];
[parentLayer addSublayer:videoLayer];
composition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
}
- (IBAction)btn_play_pressed:(id)sender
{
[self.player play];
}
- (void)btn_pressed:(id)sender
{
colorTag = [(UIButton *)sender tag];
NSLog(@" %d",colorTag);
self.backgroundView.backgroundColor = [array objectAtIndex:colorTag];
}
最佳答案
我认为您从未设置过 AVMutableVideoCompositionInstruction.backgroundColor。
UIColor *redColor = [UIColor redColor];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration);
mainInstruction.backgroundColor = redColor.CGColor;
这会将背景设置为红色,您可能希望使用用户指定的颜色。
关于ios - 使用 AVFoundation 编写视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200162/
我有一个 AVMutableVideoComposition,我正在使用 AVAssetExportSession 导出它,我正在尝试覆盖一个每秒更新一次的自定义时间戳。我应该使用 AVAssetWr
我是 MAC OSX 开发的新手。我想在 OSX 10.7 上使用 AVFoundation 将视频捕获为原始帧。我不明白为相机设备设置特定的视频分辨率,不知何故我使用 VideoSettings 设
使用 AVFoundation/QTKit 如何将多个视频设备同时录制到一个文件中? 我知道如何分别记录它们,但是尝试同时记录它们会导致诸如... “无法添加到 session 中,因为源和目标媒体类
我有一个关于 CoreAudio 和 AVFoundation 的问题。 我使用 CoreAudio 和 AUGraph 和 AudioUnit 构建了一个专业音频应用程序。 我想切换到看起来非常棒的
Apple Watch 能用吗AVFoundation ?更具体地说,可以AVAudioPlayer和 AVAudioRecorder工作? 我正在尝试制作一个应用程序,让您可以将声音录制到 Appl
Apple Watch 可以使用 AVFoundation 吗?更具体地说,AVAudioPlayer 和 AVAudioRecorder 可以工作吗? 我正在尝试制作一个应用程序,让您可以将声音录制
当我创建一个 iOS 项目和“Build Phases -> Link binary with Libraries”时,我添加了 AVFoundation.framework 库并使用 #import
我正在尝试创建一个格式正确的视频文件,用于 Apple 的 HTTP Live Streaming。这是创建文件的代码: // Init the device inputs AVCaptureDevi
我检查了答案,但找不到任何代码..我还登录以查看是否丢失了某些东西,但两个Logg都出现了,但声音没有播放...我也导入了 框架...我不知道我在做什么错我是新手,请帮忙...谢谢 - (void)v
对于这个问题,我只询问在没有外部库的情况下使用 Xcode 和 iOS 的可能性。我已经在探索在另一个 question 中使用 libtiff 的可能性. 问题 数周以来,我一直在筛选堆栈溢出,并为
我正在尝试采用不同的方法来组合视频。我正在为每个转换创建一个新轨道。 此代码的问题在于显示了第一个视频,而其他所有视频都是黑色的。 整个片段的音频覆盖都是正确的。看起来视频没有带入合成中,因为文件的大
回到使用AVAudioPlayer时,使用currentPosition来获取并设置位置,但是当使用AVAudioEngine和AVAudioPlayerNode时会使用什么呢? 最佳答案 AVAud
有人可以帮我将一个图标合并到一个视频中,该视频应该在视频播放时摆动。我已将动画添加到摆动图标,但只有静态图标会合并到视频中。如果有人可以帮助我,我将不胜感激。 我给你我正在使用的代码 //- (voi
Example 你好, 努力旋转此视频以显示在正确的方向并填满整个屏幕。 我无法使用 videocompisition 获取 avasset,但无法使其正常工作。 let videoAsset
如何一次在屏幕上显示多个相机预览? 如果我开始一个新的 AVCaptureSession,另一个就会停止。如果我使用与另一个 session 相同的 session 初始化一个新的 AVCapture
以下是我的录制按钮方法的内容。它创建了所需的文件,但无论我记录多长时间,创建的文件的长度始终为 4kb 和 0 秒,我无法弄清楚为什么它无效。对于averagePowerPerChannel,计量也返
我已经研究这个太久了。 我正在尝试获取 MacOS 网络摄像头数据并在网络摄像头输出的帧上运行 CIDetect。 我知道我需要: 连接 AVCaptureDevice (如输入)到AVCapture
我正在尝试从 CMSampleBufferRef 检索 CVPixelBufferRef,以便更改 CVPixelBufferRef 以动态覆盖水印。 我正在使用 CMSampleBufferGetI
捕获图像时,输出图像的颜色与我在预览图层上看到的颜色不同。出于某种原因,颜色略有变化。有没有人遇到过这个问题?我怎样才能解决这个问题? 当我从 didFinishProcessingPhotoSamp
对于 iOS5 来说可能是一个简单的问题他们将 AVFoundation 的eekToTime 方法更改为如下所示: [avPlayer seekToTime:startTime toleranceB
我是一名优秀的程序员,十分优秀!