微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – Trim视频没有显示UIVideoEditorController?

目前,我正在处理一个处理视频的应用程序.
在我的应用程序中,用户可以修剪视频,我有一个自定义控件来选择开始时间和结束时间.我需要通过这两个值修剪视频.我试过UIVideoEditorController如下.
UIVideoEditorController* videoEditor = [[[UIVideoEditorController alloc] init] autorelease];
    videoEditor.delegate = self;
    Nsstring* videoPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"];
    if ( [UIVideoEditorController canEditVideoAtPath:videoPath] )
    {
      videoEditor.videoPath = videoPath;
      [self presentModalViewController:videoEditor animated:YES];
    }
    else
    {
      NSLog( @"can't edit video at %@",videoPath );
    }

但是问题是上面的代码显示苹果的视频编辑器控件,用户可以对该视图进行一些操作.我不想显示此视图,因为我已经在MPMoviePlayer上显示了视频,并收到用户输入(开始时间和结束时间)以修改自定义控件上的视频.
如何在不显示UIVideoEditorController的情况下修剪视频?

解决方法

最后我找到了解决方案.

我们可以使用AVAssetExportSession修剪视频而不显示UIVideoEditorController.

我的代码如下:

- (void)splitVideo:(Nsstring *)outputURL
{

    @try
    {
        Nsstring *videoBundleURL = [[NSBundle mainBundle] pathForResource:@"Video_Album" ofType:@"mp4"];

        AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoBundleURL] options:nil];

        NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];

        if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
        {

            [self trimVideo:outputURL assetobject:asset];

        }
        videoBundleURL = nil;

        [asset release];
        asset = nil;

        compatiblePresets = nil;
    }
    @catch (NSException * e)
    {
        NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]);
    }
}

方法修剪视频

- (void)trimVideo:(Nsstring *)outputURL assetobject:(AVAsset *)asset
  {

    @try
    {

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

        exportSession.outputURL = [NSURL fileURLWithPath:outputURL];

        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        CMTime start = CMTimeMakeWithSeconds(splitedDetails.startTime,1);

        CMTime duration = CMTimeMakeWithSeconds((splitedDetails.stopTime - splitedDetails.startTime),1);

        CMTimeRange range = CMTimeRangeMake(start,duration);

        exportSession.timeRange = range;

        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        [self checkExportSessionStatus:exportSession];

        [exportSession release];
        exportSession = nil;

    }
    @catch (NSException * e)
    {
        NSLog(@"Exception Name:%@ Reason:%@",[e reason]);
    }
}

方法检查修剪的状态:

- (void)checkExportSessionStatus:(AVAssetExportSession *)exportSession
  {

    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
    {

        switch ([exportSession status])
            {

            case AVAssetExportSessionStatusCompleted:

                NSLog(@"Export Completed");
                break;

            case AVAssetExportSessionStatusFailed:

                NSLog(@"Error in exporting");
                break;

            default:
                break;

        }
    }];
}

我从导出按钮操作方法调用splitVideo方法,并将输出URL作为参数传递.

原文地址:https://www.jb51.cc/iOS/335014.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐