CGAffineTransform 视频旋转(转)
记录下视频旋转
//////////////////////////////////////////////
- (void)test:(NSURL *)url transformUrl:(NSURL *)exportUrl {
[self rotateVideoAssetWithFileURL:url dstFileURL:exportUrl];
} - (void)rotateVideoAssetWithFileURL:(NSURL *)fileURL dstFileURL:(NSURL *)dstFileURL { NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:options]; AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:];
AVAssetTrack *audioAssetTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:];
if (videoAssetTrack == nil || audioAssetTrack == nil) {
NSLog(@"error is %@", @"video or audio assetTrack is nil");
return;
} AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = videoAssetTrack.minFrameDuration;
CGSize renderSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
videoComposition.renderSize = renderSize; //create a video instruction
AVMutableVideoCompositionInstruction *videoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration); AVMutableVideoCompositionLayerInstruction *videoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack]; //仿射变换的坐标为iOS的屏幕坐标x向右为正y向下为正
CGAffineTransform transform = [self videoAssetTrackTransform:videoAssetTrack];
[videoCompositionLayerInstruction setTransform:transform atTime:kCMTimeZero]; //add the transformer layer instructions, then add to video composition
videoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:videoCompositionLayerInstruction];
videoComposition.instructions = [NSArray arrayWithObject: videoCompositionInstruction]; AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
#warning when use (not AVAssetExportPresetPassthrough) AVAssetExportSession export video which is contain video and audio must add video track first,
#warning when add audio track frist error is -11841.
AVMutableCompositionTrack *videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error];
if (error) {
NSLog(@"error is %@", error);
return;
}
error = nil;
AVMutableCompositionTrack *audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:&error];
if (error) {
NSLog(@"error is %@", error);
return;
}
NSLog(@"the assetDuration is %lld", asset.duration.value/asset.duration.timescale); AVAssetExportSession *assetExportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality] ;
assetExportSession.shouldOptimizeForNetworkUse = YES;
assetExportSession.videoComposition = videoComposition;
assetExportSession.outputURL = dstFileURL;
assetExportSession.outputFileType = AVFileTypeMPEG4; __weak AVAssetExportSession *weakAssetExportSession = assetExportSession;
__weak typeof(self)weakSelf = self;
[assetExportSession exportAsynchronouslyWithCompletionHandler:^
{
if ([weakAssetExportSession status] != AVAssetExportSessionStatusCompleted) {
NSLog(@"the error is %@", [weakAssetExportSession error]);
NSLog(@"the status is %ld", (long)[weakAssetExportSession status]);
NSLog(@"the outPutPath is %@", [weakAssetExportSession.outputURL absoluteString]);
NSLog(@"the error is %@", [weakAssetExportSession error].userInfo);
}
dispatch_async(dispatch_get_main_queue(), ^{
#warning here can not use weakAssetExportSession.outputURL weakAssetExportSession.outputURL some time is null but video is exit.
[weakSelf exportDidFinish:dstFileURL];
});
}];
} - (CGAffineTransform)videoAssetTrackTransform:(AVAssetTrack *)videoAssetTrack {
int degrees = -;//[self degressFromVideoFileWithVideoAssetTrack:videoAssetTrack];
CGAffineTransform transform = CGAffineTransformIdentity;
if (degrees != ) {
CGAffineTransform translateToCenter = CGAffineTransformIdentity;
if (degrees == ) {
// 顺时针旋转90°
translateToCenter = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, 0.0);
transform = CGAffineTransformRotate(translateToCenter, M_PI_2);
} else if(degrees == ){
// 顺时针旋转180°
translateToCenter = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.width, videoAssetTrack.naturalSize.height);
transform = CGAffineTransformRotate(translateToCenter, M_PI);
} else if(degrees == ){
// 顺时针旋转270°
translateToCenter = CGAffineTransformMakeTranslation(0.0, videoAssetTrack.naturalSize.width);
transform = CGAffineTransformRotate(translateToCenter, M_PI_2 + M_PI);
}else if(degrees == -){
// 绕x轴旋转180度
//仿射变换的坐标为iOS的屏幕坐标x向右为正y向下为正
#if 1
// transform = CGAffineTransformTranslate(transform, videoAssetTrack.naturalSize.width, videoAssetTrack.naturalSize.height);
//transform = CGAffineTransformRotate(transform, 90/180.0f*M_PI); // 旋转90度
//transform = CGAffineTransformScale(transform, 1.0, -1.0); // 上下颠倒视频 // translateToCenter = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, 0.0);
// transform = CGAffineTransformRotate(translateToCenter, -M_PI_2); transform = CGAffineTransformScale(transform, -1.0, 1.0); // 左右颠倒视频
transform = CGAffineTransformTranslate(transform, -videoAssetTrack.naturalSize.width, );
// transform = CGAffineTransformRotate(translateToCenter, -M_PI_2); //transform = CGAffineTransformScale(transform, 1.0, 1.0); // 使用原始大小 //原始视频
// ___
// | |
// | |
// -------------------- +x
// |
// |
// |
// |
// |
// |
// |
// +y //transform = CGAffineTransformScale(transform, 1.0, -1.0); // 上下颠倒视频 // -------------------- +x
// | | |
// | |___|
// |
// |
// |
// |
// |
// +y //transform = CGAffineTransformTranslate(transform, 0, -videoAssetTrack.naturalSize.height);// 将视频平移到原始位置 // ___
// | |
// | |
// -------------------- +x
// |
// |
// |
// |
// |
// |
// |
// +y // transform = CGAffineTransformScale(transform, 1.0, -1.0); // 上下颠倒视频
// transform = CGAffineTransformTranslate(transform, 0, -videoAssetTrack.naturalSize.height);
#else
transform = videoAssetTrack.preferredTransform;
transform = CGAffineTransformTranslate(transform, , -videoAssetTrack.naturalSize.height);
#endif
}
} #if 0 - cropVideo
//Here we shift the viewing square up to the TOP of the video so we only see the top
CGAffineTransform t1 = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, ); //Use this code if you want the viewing square to be in the middle of the video
//CGAffineTransform t1 = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height, -(videoAssetTrack.naturalSize.width - videoAssetTrack.naturalSize.height) /2 ); //Make sure the square is portrait
transform = CGAffineTransformRotate(t1, M_PI_2);
#endif return transform;
} - (int)degressFromVideoFileWithVideoAssetTrack:(AVAssetTrack *)videoAssetTrack {
int degress = ;
CGAffineTransform t = videoAssetTrack.preferredTransform;
if(t.a == && t.b == 1.0 && t.c == -1.0 && t.d == ){
// Portrait
degress = ;
} else if(t.a == && t.b == -1.0 && t.c == 1.0 && t.d == ){
// PortraitUpsideDown
degress = ;
} else if(t.a == 1.0 && t.b == && t.c == && t.d == 1.0){
// LandscapeRight
degress = ;
} else if(t.a == -1.0 && t.b == && t.c == && t.d == -1.0){
// LandscapeLeft
degress = ;
} else if(t.a == -1.0 && t.b == && t.c == && t.d == -1.0){
// LandscapeLeft
degress = ;
} else if(t.a == 1.0 && t.b == && t.c == && t.d == -1.0){
// x-axis
degress = -;
} return degress;
} - (void)exportDidFinish:(NSURL *)fileURL {
NSLog(@"fileURL is %@", fileURL); dispatch_async(dispatch_get_main_queue(), ^{ if ([XCFileManager isExistsAtPath:[self.videoUrl path]]) { NSURL *outputURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.mov"]]; [self convertVideoToLowQuailtyWithInputURL:fileURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.recordState = FMRecordStateFinish;
}); if (exportSession.status == AVAssetExportSessionStatusCompleted) { self.videoUrl = outputURL;
self.videoFirstImage = firstImage; }else {
printf("error\n");
}
}]; } }); }
转载:https://blog.csdn.net/jeffasd/article/details/51887064
CGAffineTransform 视频旋转(转)的更多相关文章
- Android WebRTC视频旋转问题
最近在对接WebRTC到安卓手机上,有个需求就是手机横屏时将对方图像进行旋转,研究了WebRTC video_render的代码后发现远端的视频渲染使用opengles20或surfaceview实现 ...
- CGAffineTransform 缩放 / 旋转 / 平移
CGAffineTransform此类是一个3*3矩阵的变换. - (void)transformImageView { CGAffineTransform t = CGAffineTransform ...
- CGAffineTransform函数旋转操作
本文转载至 http://blog.sina.com.cn/s/blog_923fdd9b0101ahyx.html 首先获取UITableView的CGAffineTransform函数:CG ...
- ffmpeg -视频旋转和高清转码示例
手头有一个竖屏拍摄的视频(真诚建议不要这么做..),导入到电脑上以后势必要把它旋转90°,可是没想到就这样简单的一个功能,尝试了N个非编软件(openshot, pitivi,还有坑爹的lives)后 ...
- FFMPEG 视频旋转设置
fmpeg -i inputfile.mp4 -vf "transpose=1" outputfile.mp4 0=90CounterCLockwise and Vertical ...
- ffmpeg实现视频的翻转与旋转(ffmpeg4.2.2)
一,ffmpeg的安装 请参见: https://www.cnblogs.com/architectforest/p/12807683.html 说明:刘宏缔的架构森林是一个专注架构的博客,地址:ht ...
- FFmpeg滤镜实现区域视频增强 及 D3D实现视频播放区的拉大缩小
1.区域视频增强 FFmpeg滤镜功能十分强大,用滤镜可以实现视频的区域增强功能. 用eq滤镜就可以实现亮度.对比度.饱和度等的常用视频增强功能. 推荐两篇写得不错的博文: (1)ffmpeg综合应用 ...
- 视频处理控件TVideoGrabber如何重新编码视频
TVideoGrabber中可以对音频.视频剪辑进行重新编码剪辑,多的朋友知道这个功能更点,但是具体操作上还是不是很熟悉,这里总结一下,主要步骤如下: 1.通过指定开始和停止的时间,可以简单的剪辑视频 ...
- 代码记录:使用Aforge.net让视频图像反转180度
private void CameraConn() { videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedI ...
随机推荐
- HTML5特效~3D立方体旋转
先欣赏一下该特效的最终效果 本文源码参考自http://www.cnblogs.com/ECJTUACM-873284962/进行一点点优化,下面是对此特效原理上的的剖析. 该特效是基于Css3的一些 ...
- mysql if函数使用例子
1.场景一 有时查询数量a 同时查询过滤后的数量b 2. 代码 SELECT count(id) as total_count, count( IF ( date(order_time) = DATE ...
- 原生js模拟jquery写法
function $_custom(fun) { document.onreadystatechange = function() { if (document.readyState == " ...
- 11.1NOIP模拟赛解题报告
心路历程 预计得分:\(100 + 100 + 50\) 实际得分:\(100 + 100 + 50\) 感觉老师找的题有点水呀. 上来看T1,woc?裸的等比数列求和?然而我不会公式呀..感觉要凉 ...
- 07_zookeeper的客户端工具curator_基本api
[zk原生api的不足之处] * 不能自动超时重连,需要手动操作 * watcher事件注册一次后就会失效 * 不支持递归创建节点 [ Apache curator ] * 解决了watcher的注册 ...
- 九、background及相关所有属性
先看看如下所示的视效图应该如何显示背景阴影? #header { height: 180px; background: url(../images./bg.png) no-repeat center ...
- DELPHI 小结
//十六进制(S)-->>十进制(I) [重写:Jey]function hextoint(s: string): Integer; begin //$代表16进制 ...
- Maximum Subarray解题报告zz
http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...
- 【转】winrar命令行详解
从命令行也可以运行 WinRAR 命令,常规的命令行语法描述如下: WinRAR <命令> -<开关1> -<开关N> <压缩文件> <文件.. ...
- QT的QPropertyAnimation讲解
m_pAnimation->setEasingCurve(QEasingCurve::Linear); //直线风格 m_pAnimation->setLoopCount(-1); //无 ...