我正在使用UIImagePickerController捕获视频。将视频导出到mp4。代码在这里:
- (NSString *)convertMOVToMp4:(NSURL *)url : (NSString *)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"/doctorphoto"];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *videoPath1 = [dataPath stringByAppendingPathComponent:@"xyz2.mov"]; //Add the file name
NSString *movfilepath = videoPath1;
NSURL *videoURL = url;
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
[videoData writeToFile:videoPath1 atomically:NO];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:movfilepath] options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
videoPath1 = [self getLocalVideoPath:filename];
exportSession.outputURL = [NSURL fileURLWithPath:videoPath1];
NSLog(@"videopath of your mp4 file = %@",videoPath1); // PATH OF YOUR .mp4 FILE
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
NSLog(@"Export success.");
[self.delegate onCompleteConvert: videoPath1 : anyobj];
break;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:movfilepath error:NULL];
}];
}
return videoPath1;
}
它转换成mp4。然后上传到AWS服务器。此链接正在流入iPhone 8,但不能在iPhone 5S,iPad mini2中播放。在网站上,音频正在播放,但显示黑屏。如果我按照iPhone 5S或iPad mini的相同程序,它的工作正常。有谁能够帮我。提前谢谢。
答案 0 :(得分:0)
这一改变解决了这个问题:
- (NSString *)convertMOVToMp4:(NSURL *)url : (NSString *)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"/doctorphoto"];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *videoPath1 = [dataPath stringByAppendingPathComponent:@"xyz2.mov"]; //Add the file name
NSString *movfilepath = videoPath1;
NSURL *videoURL = url;
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
[videoData writeToFile:videoPath1 atomically:NO];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:movfilepath] options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
NSString *preset = AVAssetExportPreset1920x1080;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:preset];
videoPath1 = [self getLocalVideoPath:filename];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// videoPath1 = [NSString stringWithFormat:@"%@/doctorphoto/%@", [paths objectAtIndex:0], filename];
exportSession.outputURL = [NSURL fileURLWithPath:videoPath1];
NSLog(@"videopath of your mp4 file = %@",videoPath1); // PATH OF YOUR .mp4 FILE
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = true;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
NSLog(@"Export success.");
[self.delegate onCompleteConvert: videoPath1 : anyobj];
break;
}
// UISaveVideoAtPathToSavedPhotosAlbum(videoPath1, self, nil, nil);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:movfilepath error:NULL];
}];
}
return videoPath1;
}