我AVCaptureMovieFileOutput用来录制一些视频.我显示了预览图层,使用AVLayerVideoGravityResizeAspectFill它稍微放大.我遇到的问题是最终视频较大,包含在预览期间不适合屏幕的额外图像.
这是预览和结果视频

有没有办法可以指定CGRect我想要从视频中删除的内容AVAssetExportSession?
编辑----
当我应用它CGAffineTransformScale时,AVAssetTrack它放大到视频,并AVMutableVideoComposition renderSize设置为view.bounds它裁剪结束.太棒了,还剩下一个问题.视频的宽度不会拉伸到正确的宽度,只是填充黑色.
编辑2 ----建议的问题/答案是不完整的..
我的一些代码:
在我的- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error方法中,我有这个来裁剪和调整视频大小.
- (void)flipAndSave:(NSURL *)videoURL withCompletionBlock:(void(^)(NSURL *returnURL))completionBlock
{
AVURLAsset *firstAsset = [AVURLAsset assetWithURL:videoURL];
// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
// 2 - Video track
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) …Run Code Online (Sandbox Code Playgroud) 这是代码:
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = outputUrl;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.videoComposition = mainComposition;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
//completion
}];
Run Code Online (Sandbox Code Playgroud)
我尝试过不同的质量设置.无论我想渲染什么视频,我总是在视频右侧和底部都有一个1-2像素的边框.可能导致这种情况的原因以及如何解决?
编辑:我没有在任何地方使用任何绿色,所以这必须以某种方式来自框架.
avfoundation ios avmutablecomposition avassetexportsession avasset
我试图在我的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) 我会制作缩小尺寸的视频,可能是50像素,75像素长.这些是物理方面.
你怎么设置的?在视频设计?我认为AVVideoWidthKey和AVVideoHeightKey更适用于我需要的物理尺寸分辨率.
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt: 320], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain
Run Code Online (Sandbox Code Playgroud) 我知道之前曾问过这个问题(例如这里和这里),但我无法弄清楚我做错了什么.
我有一个AVMutableComposition用于将一些视频剪辑与一些CALayer动画组合在一起的视频剪辑.
当我AVMutableComposition把它和它结合在一起AVSynchronizedLayer进行播放时,一切正常AVPlayerLayer.视频输出正确,一切都放在应有的位置.
我的问题是,当我尝试导出这个东西时,我尝试使用AVVideoCompositionCoreAnimationTool而不是AVSynchronizedLayer(这是文档中我们应该用于导出的东西)和AVAssetExportSession,但是出了点问题,因为在我导出的电影中,每个CALayer都有它的坐标系反转.
所以,如果在播放过程中0,0点是在左上角的屏幕一角时,我出口的电影的0,0点是在左下方的角落,因此我的动画发疯.
我阅读了有关此内容的任何可能的文章,我还从Apple下载了AVVideoEdit示例,但我无法看到发生了什么......
iphone core-animation avfoundation avmutablecomposition avassetexportsession
我试图在iOS7下使用AVAssetExportSession合并2个WAV文件.我已经确认文件在那里,似乎没有腐败或任何东西.这些是从设备本身完成的录制中获取的WAV文件,它们是相对较小的文件.
当它调用exportAsync方法时,它会立即在完成块中立即失败并出现"Operation Stopped"错误(原因描述为:"此媒体不支持该操作").这发生在模拟器和设备本身.请参阅下面的导出代码:
NSError *avError = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
AVURLAsset *tmpAsset = [[AVURLAsset alloc] initWithURL:_tmpRecordingUrl options:@{AVURLAssetPreferPreciseDurationAndTimingKey: @YES}];
AVURLAsset *permAsset = [[AVURLAsset alloc] initWithURL:_url options:@{AVURLAssetPreferPreciseDurationAndTimingKey: @YES}];
AVMutableComposition *composition = [AVMutableComposition composition];
[composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, permAsset.duration) ofAsset:permAsset atTime:kCMTimeZero error:&avError];
[composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, tmpAsset.duration) ofAsset:tmpAsset atTime:CMTimeMakeWithSeconds(_positionSlider.value, 1) error:&avError];
AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetPassthrough];
if ([fileManager fileExistsAtPath:[_workingUrl path]]) {
[fileManager removeItemAtURL:_workingUrl error:&avError];
}
export.outputFileType = AVFileTypeWAVE;
export.outputURL = _workingUrl;
[export exportAsynchronouslyWithCompletionHandler:^(void) {
// fails
}];
Run Code Online (Sandbox Code Playgroud)
我还确认"avError"永远不会被填充,因此插入timeRanges或创建导出会话似乎没有问题.我检查了资产,它们都是可读的,可播放的和可导出的(根据obj上的bool值).
我错过了一些明显的东西吗?此代码适用于iOS6.如果我需要提供更多信息,请随时告诉我,并提前感谢您提供的任何方向!
编辑#1:我尝试添加跟踪机制,类似于这篇文章的内容:AVAssetExportSession - …
我正在使用UIImagePickerController捕获视频,我可以使用以下代码裁剪视频,
AVAsset *asset = [AVAsset assetWithURL:url];
//create an avassetrack with our asset
AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
//create a video composition and preset some settings
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = CMTimeMake(1, 30);
//here we are setting its render size to its height x height (Square)
videoComposition.renderSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height);
//create a video instruction
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30));
AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
//Here we shift the viewing square up to the …Run Code Online (Sandbox Code Playgroud) 我正在处理一项任务,我必须将录制的视频从特定的起始点修剪为用户输入或选择的特定终点.我该怎么做呢 正如我UIVideoEditorController之前使用的,但我不想使用默认视图,我想直接修剪视频.
let FinalUrlTosave = NSURL(string: "\(newURL)")
exportSession!.outputURL=FinalUrlTosave
exportSession!.shouldOptimizeForNetworkUse = true
// exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession!.outputFileType = AVFileTypeQuickTimeMovie;
let start:CMTime
let duration:CMTime
var st = starttime.doubleValue
var ed = endTime.doubleValue
start = CMTimeMakeWithSeconds(st, 600)
duration = CMTimeMakeWithSeconds(ed, 600)
// let timeRangeForCurrentSlice = CMTimeRangeMake(start, duration)
let range = CMTimeRangeMake(start, duration);
exportSession!.timeRange = range
exportSession!.exportAsynchronouslyWithCompletionHandler({
switch exportSession!.status{
case AVAssetExportSessionStatus.Failed:
print("failed \(exportSession!.error)")
case AVAssetExportSessionStatus.Cancelled:
print("cancelled \(exportSession!.error)")
default:
print("complete....complete")
// self.SaveVideoToPhotoLibrary(destinationURL1!)
}
})
Run Code Online (Sandbox Code Playgroud)
我试图用这个来实现我的目标,但没有成功.
错误信息:
failed可选(错误域= NSURLErrorDomain代码= -1100"在此服务器上找不到请求的URL."UserInfo = {NSErrorFailingURLStringKey = file:/// …
此代码曾用于在iOS9上为视频添加水印和文本,但由于iOS10它不再起作用.已经提交了一个iOS 10错误,但Apple没有回答.我无法实现任何解决方法来在视频上添加水印和文字.使用此代码有时会成功导出视频,但大部分时间都不会导出.
我应该如何使用,AVVideoCompositionCoreAnimationTool以便它像在iOS9上一样工作.
let videoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 60)
videoComposition.renderSize = CGSize(width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.height)
let instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
// transformer is applied to set the video in portrait otherwise it is rotated by 90 degrees
let transformer: AVMutableVideoCompositionLayerInstruction =
AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let t1: CGAffineTransform = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: -(clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height)/2)
let t2: CGAffineTransform = t1.rotated(by: CGFloat(M_PI_2))
var finalTransform: CGAffineTransform = t2
transformer.setTransform(finalTransform, at: kCMTimeZero)
instruction.layerInstructions …Run Code Online (Sandbox Code Playgroud) 与此问题类似,
我有问题,我的应用程序以同样的方式崩溃.我会假设与另一个问题相同的答案:记忆问题; 除了我在AVAssetExportSession调用期间遇到崩溃.
guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = url
exporter.videoComposition = mainComposition
print("done")
exporter.exportAsynchronously(completionHandler: {
DispatchQueue.main.async(execute: {
self.exportDidFinish(exporter)
print("removing AI")
self.removeAI()
print("removed AI")
completion()
})
})
func exportDidFinish(_ exporter:AVAssetExportSession) {
if(exporter.status == AVAssetExportSessionStatus.completed) {
print("cool")
}
else if(exporter.status == AVAssetExportSessionStatus.failed) {
print(exporter.error as Any)
}
}
Run Code Online (Sandbox Code Playgroud)
它打印"完成"但它从不打印"删除AI".它也不打印"酷"或"(错误)"; 它崩溃并在XCode的顶部说"失去与iPhone的连接......"就像另一个问题所说的那样.
我认为这是一个内存问题,但在异步导出期间(我知道它是如何工作的)之间没有任何事情发生,因为我只是在等待调用完成处理程序.但是之间没有任何东西被调用,我不确定如何处理这个问题.有什么想法吗?
ios ×9
video ×5
avfoundation ×4
swift ×3
iphone ×2
objective-c ×2
avasset ×1
ios7 ×1
orientation ×1
trim ×1
xcode ×1