0

可能重复:
如何使用 AVFoundation 裁剪视频

最终目标是将我的 480x640 .mov 视频裁剪为精确分辨率 480x360 的 4:3 比例横向视频。

我知道此链接提出的近乎确切的问题: 如何使用 AVFoundation 裁剪视频

我相信显示已经裁剪的预览图层会更容易,然后在最后裁剪最终(未裁剪的纵向 480x640)视频。我一直在玩下面的代码。目前,我只是想让它转换任何东西 - 那里有多余的代码,我稍后会修剪。下面的代码尝试将视频缩放到 70%:

-(void) convertVideoToSize{
AVURLAsset *videoToConvert = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempURL] options:nil];
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoToConvert.duration) ofTrack:[[videoToConvert tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,videoToConvert.duration);
AVMutableVideoCompositionLayerInstruction *firstLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];
//WE PERFORM VIDEO EDITING OPERATIONS HERE

CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation( 0,-420); 

CGAffineTransform Rotate = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform Scale = CGAffineTransformMakeScale(1.0f,1.0f);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(1, 1);
CGAffineTransform finalTransform = CGAffineTransformConcat(shrinkWidth, CGAffineTransformConcat(translateToCenter, Rotate) );
[firstLayerInstruction setTransform:finalTransform atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:CGAffineTransformConcat(Rotate,Scale) atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:Scale atTime:kCMTimeZero];
mainInstruction.layerInstructions = [NSArray arrayWithObject:firstLayerInstruction];

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1,30);
mainCompositionInst.renderSize = CGSizeMake(480, 360);

//AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:mixComposition];
//newPlayerItem.videoComposition = mainCompositionInst;


AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = [NSURL fileURLWithPath:tempURL3];
exportSession.videoComposition = mainCompositionInst;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     switch (exportSession.status)
     {
         case AVAssetExportSessionStatusFailed:
         {
             NSLog (@"FAIL");

             break;
         }
         case AVAssetExportSessionStatusCompleted:
         {
             NSLog (@"SUCCESS");

             break;
         }
         case AVAssetExportSessionStatusCancelled:
         {
             NSLog (@"CANCELED");

             break;
         }
     };
 }];

}

它不起作用,并输出一个不可读的 .mov 文件。我想知道是否有人可以为我指出一个更简单的方向,或者让我了解更改/缩放视频的最佳方法。

更新:将视频保存到文件 transform.mov 后,我尝试播放它,但在 QuickTime 中收到“无法识别电影的文件格式”错误。任何可能的腐败来源?

更新 2:我已成功设置导出会话和组合以编写转换。现在我只需要特定的转换,将我的 480x640 转换为 480x360。那里有什么帮助吗?

4

0 回答 0