0
此方法有什麼問題。傳遞的filePath是來自tmp文件夾的電影文件的nsstring路徑,outputPath是文檔文件夾中的重命名文件路徑。 exportSession失敗,狀態碼爲4,描述爲 - 未知。我想要最後5秒的視頻。修剪後AVAssetExportSession失敗
-(BOOL)trimVideofileFile:(NSString*)filePath toFileURL:(NSString*)outputPath
{
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
Float64 videoLength = CMTimeGetSeconds(sourceAsset.duration);
NSLog(@"Duration of Video is %f",videoLength);
float videoStartTime = 0.0f;//define start time of video
float videoEndTime = 5.0f;//define end time of video
videoEndTime = videoLength ;
NSLog(@"Video end times - %f",videoEndTime);
if (videoLength>5.000f) {
videoStartTime = videoEndTime - 5.000f;
NSLog(@"Video Start Time - %f",videoStartTime);
}
NSURL *videoFileInput = [NSURL fileURLWithPath:filePath];//<Path of orignal Video file>
NSURL *videoFileOutPut = [NSURL fileURLWithPath:outputPath];
NSLog(@"Original File - %@",videoFileInput);
NSLog(@"Output Path - %@",videoFileOutPut);
if (!videoFileInput || !videoFileOutPut)
{
return NO;
}
[[NSFileManager defaultManager] removeItemAtURL:[NSURL URLWithString:filePath] error:NULL];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:filePath]];
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: %@ - %ld", [[exportSession error] localizedDescription],(long)exportSession.status);
}
}];
return YES;
}
錯誤詳細信息: -
Export failed: Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLStringKey=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov, NSErrorFailingURLKey=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov, NSLocalizedDescription=unknown error, NSUnderlyingError=0x15d7a990 {Error Domain=NSOSStatusErrorDomain Code=-12935 "(null)"}, NSURL=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov}
當你運行這段代碼時,你看過'startTime'和'stopTime'的值嗎? – ChrisH
是的。我現在也嘗試了3種其他方式。我得到這個錯誤..請參閱編輯錯誤的詳細信息@ChrisH – Rijiva
看看這裏:http://stackoverflow.com/questions/4001755/trying-to-understand-cmtime-and-cmtimemake – ChrisH