1

我正在开发目标 C 中的 iOS 应用程序。

根据我的要求,我想将视频分成多个部分。

假设我有一个 50 秒的视频,我想将它分成 5 个部分,每个部分 10 秒。

如果你们对此有任何想法,请给我建议。

4

1 回答 1

1

很好的问题。解决方案在这里...

-(void)splitSecondVideo
{
if (did<splitdivide){

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_videourl options:nil];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs;
CMTime starttime;
CMTime duration;

    myPathDocs =  [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"SplitFinalVideo%d.mov",did]];
    double endt=CMTimeGetSeconds([asset duration]);
    NSLog(@"All Duration : %f",endt);
    double divide=CMTimeGetSeconds([asset duration])/splitdivide;
    NSLog(@"All Duration : %f",divide);

    starttime = CMTimeMakeWithSeconds(divide*did, 1);
    duration = CMTimeMakeWithSeconds(divide, 1);

NSFileManager *fileManager=[[NSFileManager alloc]init];
NSError *error;
if ([fileManager fileExistsAtPath:myPathDocs] == YES) {
    [fileManager removeItemAtPath:myPathDocs error:&error];
}

exportSession.outputURL = [NSURL fileURLWithPath:myPathDocs];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
// Trim to half duration

CMTimeRange secondrange = CMTimeRangeMake(starttime, duration);

exportSession.timeRange = secondrange;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {
     [self exportDidFinish:exportSession];
     did++;

     [self splitSecondVideo];

 }];

}} 

- (void)exportDidFinish:(AVAssetExportSession*)session {
if (session.status == AVAssetExportSessionStatusCompleted) {
    NSURL *outputURL = session.outputURL;
    NSLog(@"Before Exported");
    [self SaveVideoAtPathWithURL:outputURL];
}} 

在viewDidLoad 方法中did=0.0的位置。& splitdivide是您要创建视频部分的值。在您的问题中 splitdivide=5注意: did 和 split 的除法都是Integer Value

希望这是有帮助的......享受......

于 2015-03-16T09:49:06.100 回答