我想修剪一个视频文件。我只想从图库中挑选视频并将其转换为15秒的视频。如果我使用pickerviewcontroller
进行正常修剪,它不会指定时间并只显示帧,但我需要固定15秒。我怎样才能做到这一点?如何使用iOS SDK修剪视频文件并将其转换为15秒视频?
5
A
回答
9
-(void)cropVideo:(NSURL*)videoToTrimURL{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *outputURL = paths[0];
NSFileManager *manager = [NSFileManager defaultManager];
[manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
// Remove Existing File
[manager removeItemAtPath:outputURL error:nil];
exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here
CMTime duration = CMTimeMakeWithSeconds(15.0, 600);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:
[self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]];
NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Failed:%@",exportSession.error);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Canceled:%@",exportSession.error);
break;
default:
break;
}
//[exportSession release];
}];
}
0
上面的答案在我们需要设置开始时间和结束时间来修剪时稍作改动。
我改变了这个:
CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here
CMTime duration = CMTimeMakeWithSeconds(15.0, 600);
CMTimeRange range = CMTimeRangeMake(start, duration);
要这样:
CMTime start = CMTimeMakeWithSeconds(self.StartTime, 600); // you will modify time range here
CMTime duration = CMTimeSubtract(CMTimeMakeWithSeconds(self.EndTime, 600), start);
CMTimeRange range = CMTimeRangeMake(start, duration);
它为我工作。
相关问题
- 1. 如何将Bystream视频转换为java中的文件视频
- 2. 使用ffmpeg修剪和合并音频和视频文件
- 3. 如何使用FFMPEG将视频转换为flv并将其转换为flv?
- 4. Android SDK中剪切/修剪视频文件
- 5. 如何使用AVFoundation修剪视频
- 6. IOS 5:UIView转换为视频并导致视频损坏
- 7. 使用MediaMuxer和MediaCodec将wmv视频文件转换为mp4视频文件
- 8. 将CMSampleBufferRef转换为视频
- 9. 如何使用JavaScript将视频文件转换为字符串?
- 10. 修剪视频文件WPF C#
- 11. 如何使用Android SDK/NDK分割/修剪视频?
- 12. React-native修剪视频
- 13. 将视频转换为.flv?
- 14. 如何在Laravel上传视频并将其转换为异步?
- 15. 在MATLAB中将音频和视频合并为视频文件
- 16. iOS - 将两个视频合并为单个带有音频的视频文件
- 17. 将视频转换为MP3
- 18. 如何将Flash视频转换为html5视频?
- 19. 视频转换为photocapturedevice的视频
- 20. iOS AVExportSession失败,仅修剪视频
- 21. iOS修剪视频至固定长度
- 22. 使用GStreamer将视频转换为WebM
- 23. 使用FFMPEG将视频转换为FLV
- 24. 将音频和视频转换为NSData
- 25. 'C'程序将音频文件转换为FFMPEG并生成视频文件
- 26. 如何将blob转换为视频?
- 27. 如何将YUV帧转换为视频?
- 28. 如何将位图转换为视频?
- 29. 使用iOS修剪音频
- 30. Python视频编辑 - 如何修剪视频
如果我们改变时间开始和时间,有时它不修剪视频right.Can请检查这一个。 – Imran
@ram我的视频持续时间是60秒我想要从15秒到30秒 – sohil
CMTime start = CMTimeMakeWithSeconds(15,600); //你将修改CMTime持续时间= CMTimeMakeWithSeconds(30,600); @sohil它会为你工作 – ram