Ios 是否在不显示UIVideoEditorController的情况下修剪视频?
目前我正在开发一个处理视频的应用程序。 在我的应用程序中,用户可以修剪视频,我有一个自定义控件用于选择开始时间和结束时间。我需要用这两个值来修剪视频。我使用Ios 是否在不显示UIVideoEditorController的情况下修剪视频?,ios,xcode,video,uivideoeditorcontroller,Ios,Xcode,Video,Uivideoeditorcontroller,目前我正在开发一个处理视频的应用程序。 在我的应用程序中,用户可以修剪视频,我有一个自定义控件用于选择开始时间和结束时间。我需要用这两个值来修剪视频。我使用UIVideoEditorController进行了如下尝试 UIVideoEditorController* videoEditor = [[[UIVideoEditorController alloc] init] autorelease]; videoEditor.delegate = self; NSStrin
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 name],[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作为参数传递。我们可以导入AVFoundation/AVFoundation.h
-(BOOL)trimVideofile
{
float videoStartTime;//define start time of video
float videoEndTime;//define end time of video
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];
NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];
NSURL *videoFileOutput = [NSURL fileURLWithPath:OutputFilePath];
NSURL *videoFileInput;//<Path of orignal Video file>
if (!videoFileInput || !videoFileOutput)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:videoFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:videoFileInput];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetLowQuality];
if (exportSession == nil)
{
return NO;
}
CMTime startTime = CMTimeMake((int)(floor(videoStartTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(videoEndTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = videoFileOutput;
exportSession.timeRange = exportTimeRange;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
NSLog(@"Export OK");
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
}
}];
return YES;
}
-(BOOL)视频文件
{
float videoStartTime;//定义视频的开始时间
浮点videoEndTime;//定义视频的结束时间
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyy-MM-dd_HH-MM-ss”];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,是);
NSString*libraryCachesDirectory=[paths objectAtIndex:0];
libraryCachesDirectory=[libraryCachesDirectory stringByAppendingPathComponent:@“Caches”];
NSString*OutputFilePath=[LibraryCachesDirectoryStringByAppendingFormat:@”/output_uz%@.mov',[dateFormatter stringFromDate:[NSDate date]];
NSURL*videoFileOutput=[NSURL fileURLWithPath:OutputFilePath];
NSURL*视频文件输入//
如果(!videoFileInput | |!videoFileOutput)
{
返回否;
}
[[NSFileManager defaultManager]RemoveItemAttribute:videoFileOutput错误:NULL];
AVAsset*asset=[AVAsset AssetWithur:videoFileInput];
AVAssetExportSession*exportSession=[AvassetExportSessionWithAsset:asset
预设名称:AVAssetExportPresetLowQuality];
if(exportSession==nil)
{
返回否;
}
CMTime startTime=CMTimeMake((int)(楼层(videoStartTime*100)),100);
CMTime stopTime=CMTimeMake((int)(ceil(videoEndTime*100)),100);
CMTimeRange exportTimeRange=CMTimeRangeFromTimeToTime(开始时间、停止时间);
exportSession.outputURL=视频文件输出;
exportSession.timeRange=exportTimeRange;
exportSession.outputFileType=AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
if(AVAssetExportSessionStatusCompleted==exportSession.status)
{
NSLog(“导出正常”);
}
else if(AVAssetExportSessionStatusFailed==exportSession.status)
{
NSLog(@“导出失败:%@,[[exportSession error]localizedDescription]);
}
}];
返回YES;
}
@khool:outputUrl用于编写修剪过的视频。它是文档目录中的一个文件路径,我正在使用相同的代码,但收到错误:导出NSString*路径时出错=[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask,YES)objectAtIndex:0];path=[path stringByAppendingPathComponent:@“new.mov”];NSLog(@“保存视频的路径为%@”,路径);[自拆分视频:路径];你在开始和停止时间里传递什么。我认为这将是剪辑视频开始或结束。正确的?除了出口,一切正常video@Khoool:分割视频的时间,视频开始和停止时间我可以为服务器上的视频使用相同的代码吗?我只有视频的url。请指导方法此答案与上面相同,复制答案有什么好处?不是复制答案。在这种情况下,我们正在使用另一种不同的解决方案。在上面的回答中,它也使用了AVAssetExportSession
。这里也有同样的东西,那么有什么区别呢?所有函数都在一个方法中组合,在“AVAssetExportSession”中使用“Cache”组合输出Url。这对你没有帮助,但对所有其他新程序员都没有帮助。你能给我提供你的视频剪辑代码吗?用户可以选择开始和结束时间?