Я хочу обрезать видеофайл. Я хочу просто выбрать видео из галереи и преобразовать его в 15-секундное видео. Если я использую обычную обрезку с pickerviewcontroller
, она не указывает время и просто показывает кадры, но мне нужно исправить 15 секунд. Как я могу это достичь?Как обрезать видеофайл и конвертировать в 15 секунд видео с помощью SDK для iOS?
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);
Он работал для меня.
Если мы изменим время начала и продолжительности, то иногда это не обрезание видео. Вы можете проверить это. – Imran
@ram мое видео длительность 60 секунд я хочу удалить с 15 до 30 секунд – sohil
CMTime start = CMTimeMakeWithSeconds (15, 600); // вы будете изменять продолжительность CMTime = CMTimeMakeWithSeconds (30, 600); @sohil, это сработает для вас – ram