我将视频与其他视频合并后将其上传到服务器,但录制的视频顺时针旋转
Posted
技术标签:
【中文标题】我将视频与其他视频合并后将其上传到服务器,但录制的视频顺时针旋转【英文标题】:I am uploading video to server after merging it with other video but the recorded video is rotated clockwise 【发布时间】:2017-02-28 09:33:00 【问题描述】:合并代码-
AVAsset *firstAsset=[AVAsset assetWithURL:urlIntroVideo];资产 *secondAsset=[AVAssetassetWithURL:recordedVideoUrl];
if (firstAsset !=nil && secondAsset!=nil) //[[AppDelegate Getdelegate] showIndicator]; // 1 - 创建 AVMutableComposition 对象。该对象将保存您的 AVMutableCompositionTrack 实例。 AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; // 2 - 视频轨道
/******************************************************************************** --------------->> VIDEO MERGING TRACK <<-------------------- ********************************************************************************/ AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; NSArray *videoTracks = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeVideo]]; NSLog(@"Video Tracks count 1st Assest=> %ld",[videoTracks count]); NSArray *audioTracks = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeAudio]]; NSLog(@"Auido Tracks count => %ld",[audioTracks count]); [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero 错误:nil];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:firstAsset.duration error:nil];
NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];
NSString *potrait_intro = [bundleDirectory stringByAppendingPathComponent:@"silent08s.mp3"]; NSURL *potrait_intro_url = [NSURL fileURLWithPath:potrait_intro]; AVAsset *audioAssest=[AVAsset assetWithURL:potrait_intro_url]; AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
NSArray *audioTracksSilent = [NSArray arrayWithArray: [audioAssest tracksWithMediaType:AVMediaTypeAudio]]; [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssest.duration) ofTrack:([audioTracksSilent count]>0)?[audioTracksSilent objectAtIndex:0]:nil atTime:kCMTimeZero error:nil]; NSArray *audioTracks2 = [NSArray arrayWithArray: [secondAsset tracksWithMediaType:AVMediaTypeAudio]]; NSLog(@"Auido Tracks count => %ld",[audioTracks2 count]); [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:audioAssest.duration error:nil]; // // NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString
stringWithFormat:@"final_merged_video-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs]; // 5 - Create exporter AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; exporter.outputURL=url; exporter.outputFileType = AVFileTypeQuickTimeMovie; exporter.shouldOptimizeForNetworkUse = YES; [exporter exportAsynchronouslyWithCompletionHandler:^ dispatch_async(dispatch_get_main_queue(), ^ [[AppDelegate Getdelegate] hideIndicator]; [self exportDidFinish:exporter]; ); ];
请帮我解决这个问题
【问题讨论】:
【参考方案1】:当您合并两个视频和/或您的服务器无法处理它时,您可能会丢失 AVAsset 上的轮播信息。 您需要做的是获取旋转信息(例如:https://gist.github.com/lukabernardi/5020724),然后在将视频发送到服务器之前相应地旋转视频。
【讨论】:
感谢您的回答,它帮助我获得了方向,但现在如何将该 AVasset 旋转 90 度? 您可以按照这个答案来帮助解决您的问题:***.com/questions/12136841/… 它没有醒来!!合并两个视频后,录制的一个旋转 90 度,另一个的方向很好 您需要更具体地了解您的问题 我在 potrait 模式下合并两个视频。第一个视频是 potrait 视频中的普通 mp4 视频,另一个是我在 potrait 中从我的相机录制的视频。现在当我合并两者时视频第一个视频方向很好,但第二个视频旋转了 90 度以上是关于我将视频与其他视频合并后将其上传到服务器,但录制的视频顺时针旋转的主要内容,如果未能解决你的问题,请参考以下文章
上传到 PHP 服务器后,录制的 iPhone X 视频将无法播放