我正在尝试使用AVAssetExportSession合并(追加)3个视频,但我一直收到此错误.奇怪的是它有1或2个视频.
Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x458120 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}
Run Code Online (Sandbox Code Playgroud)
我甚至尝试重做函数以防出错,但我得到的只是无限错误信息.这是我的代码片段.
AVMutableComposition *mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError * error = nil;
NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count];
NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count];
for (int i=0; i<[arrayMovieUrl count]; i++) {
AVURLAsset *assetClip = [arrayMovieUrl objectAtIndex:i];
AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
[tracks addObject:clipVideoTrackB];
}
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720]; …Run Code Online (Sandbox Code Playgroud) 要求:
听起来有点不同,但这就是我想要实现的目标.我想反过来制作电影(.mov)文件.就像我们如何回放电影文件一样.我还希望保持与我的视频包含相同的帧速率.
注意:我不只是想以相反的顺序播放视频文件.我想以相反的顺序生成新的电影文件.
我的探索:
我想到了以下步骤来执行相同的操作.
AVAssetExportSessionAVMutableComposition和将所有这些视频块合并为单个电影文件AVAssetExportSession.使用上述步骤,我能够反过来实现生成的视频文件,但我有以下问题.
有没有人有任何其他优化的方法来实现这一目标?任何建议将不胜感激.
我曾尝试过以下代码.
-(void)mediaItemToData : (MPMediaItem * ) curItem
{
NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
presetName:AVAssetExportPresetPassthrough];
exporter.outputFileType = @"public.mpeg-4";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * myDocumentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *exportFile = [myDocumentsDirectory stringByAppendingPathComponent:
@"exported.mp4"];
NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
// do the export
// (completion handler block omitted)
[exporter exportAsynchronouslyWithCompletionHandler:
^{
NSData *data = …Run Code Online (Sandbox Code Playgroud) 我正试图在一个独特的output.mov中合并一些视频.导出的文件无法播放,我现在不知道为什么.有人能帮我吗?
func exportVideo2(path:String, outputPath:String, nMovie:Int) -> Bool{
var composition = AVMutableComposition()
let track:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
var insertTime = kCMTimeZero
var movie = movieOfProject(path)
if movie.count == nMovie{
for (index,mov) in enumerate(movie){
let moviePath = path.stringByAppendingPathComponent(mov)
// moviePath = path to the .mov file
println(moviePath)
let moviePathUrl = NSURL(fileURLWithPath: moviePath)
let sourceAsset = AVURLAsset(URL: moviePathUrl, options: nil)
println(sourceAsset)
let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
println(sourceAsset.playable) // print true
println(sourceAsset.exportable) // print true
println(sourceAsset.readable) // print true
if tracks.count > 0{
let …Run Code Online (Sandbox Code Playgroud) 我需要能够合并在 iOS 上的相机应用程序中使用延时拍摄功能拍摄的视频并导出为单个视频。
但是,即使我尝试将单个不变的延时视频导出到照片库,它也会保存为全黑视频(具有正确的持续时间)。这是我编写的示例代码,用于导出单个未更改的视频(其中大部分改编自 Ray Wenderlich 教程):
@IBAction func saveVideo(_ sender: UIBarButtonItem) {
// 1 - Early exit if there's no video file selected
guard let videoAsset = self.avAsset else {
let alert = UIAlertController(title: "Error", message: "Failed to load video asset.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
return
}
// 2 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
print("Preparing AVMutableComposition...")
let mixComposition = AVMutableComposition() …Run Code Online (Sandbox Code Playgroud) 更新6:我已经设法完全解决了我的问题,但我仍然想要一个更好的解释,而不是我猜的是如果我不正确它不起作用的原因
我一直试图在视频上制作精灵表,但每次导出视频时,最终结果都是我开始的示例视频.
这是我的代码:
首先我的自定义CALayer来处理我自己的精灵表
class SpriteLayer: CALayer {
var frameIndex: Int
override init() {
// Using 0 as a default state
self.frameIndex = 0
super.init()
}
required init?(coder aDecoder: NSCoder) {
self.frameIndex = 0
super.init(coder: aDecoder)
}
override func display() {
let currentFrameIndex = self.frameIndex
if currentFrameIndex == 0 {
return
}
let frameSize = self.contentsRect.size
self.contentsRect = CGRect(x: 0, y: CGFloat(currentFrameIndex - 1) * frameSize.height, width: frameSize.width, height: frameSize.height)
}
override func action(forKey event: String) -> CAAction? {
if …Run Code Online (Sandbox Code Playgroud) calayer cabasicanimation avvideocomposition avassetexportsession swift
当我通过AVAssetExportSession导出视频资源时,结果文件处于LANDpace模式.(通过itune抓取文件 - > apps->文件共享 - >我的应用程序).如何以纵向模式导出视频资源(旋转它)?
AVAssetExportSession输出文件应该如何?我正在尝试从ALAsset项目压缩视频,但它不起作用.我猜输出文件与它有关.
这是我正在使用的代码:
NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie"];
[self convertVideoToLowQualityWithInputURL:asset.defaultRepresentation.url outputURL:[NSURL URLWithString:destinationPath]];
- (void)convertVideoToLowQualityWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL {
if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithContentsOfURL:outputURL encoding: 0 error:Nil]]) [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *assetAV = [AVURLAsset URLAssetWithURL:inputURL options:nil];
NSLog(@"url string from asset: %@", assetAV.URL);
NSLog(@"output url: %@", [outputURL absoluteString]);
[[NSFileManager defaultManager] createFileAtPath:[outputURL path] contents:nil attributes:nil];
NSLog(@"duration: %lld", assetAV.duration.value); //it logs a valid value, so it's all good so far
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:assetAV presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用带有音频参数的AVAudioRecorder逐个记录两个音频文件:
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],AVEncoderBitDepthHintKey,
[NSNumber numberWithInt:128000], AVEncoderBitRateKey,
nil];
Run Code Online (Sandbox Code Playgroud)
我需要在file1的末尾添加file2.我正在使用那里的解决方案(通过在添加两个文件并使用exportAsynchronouslyWithCompletionHandler:AVAssetExportSession方法导出合成后创建AVMutableCompositionTrack来附加两个音频文件).
它有效,但我有两个输入文件128kbs 44.1kHz 16bit单声道,输出文件格式为:219.4kbs 44.1kHz 16bit立体声.
有没有办法为AVAssetExportSession配置输出音频文件参数?
我有这段代码可以将视频的大小从 1280 x 720 调整为 640 x 360 但我想要一个不裁剪的调整大小。
有没有办法完全调整不裁剪的大小?
这是代码
class func resizer(inputURL : NSURL , completion: (outPutURL : NSURL?) -> Void ){
let videoAsset = AVAsset(URL: inputURL) as AVAsset
let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack
let composition = AVMutableComposition()
composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSizeMake(360,640)
videoComposition.frameDuration = CMTimeMake(1, 30)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))
let transformer : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let t1 = CGAffineTransformMakeTranslation(360, 0)
let t2 = CGAffineTransformRotate(t1, …Run Code Online (Sandbox Code Playgroud) ios ×6
avfoundation ×5
iphone ×4
swift ×4
video ×3
objective-c ×2
aac ×1
assets ×1
audio ×1
calayer ×1
compression ×1
ios5 ×1
ios6 ×1
mpmediaitem ×1