将所有视频网址转换为.mp4扩展名,而不使用Avasset

时间:2017-02-03 12:56:19

标签: objective-c

我的代码将特定网址更改为.mp4扩展名,但在转换后,它会为路径提供不具有.mp4扩展名的特定网址

VideoURL = [NSURL URLWithString:@"https://www.youtube.com/watch?v=9uLiwb9q0nc"];

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:VideoURL options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
    AVAssetExportSession *exportSession = [[AVAssetExportSession       alloc]initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality];

    NSString* documentsDirectory=     [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"temp.mp4"];
    url = [[NSURL alloc] initFileURLWithPath:myDocumentPath];

    if ([[NSFileManager defaultManager]fileExistsAtPath:myDocumentPath])
    {
        [[NSFileManager defaultManager]removeItemAtPath:myDocumentPath error:nil];
    }
    exportSession.outputURL = url;
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.shouldOptimizeForNetworkUse = YES;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch ([exportSession status])
        {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export session failed");
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");
                break;
            case AVAssetExportSessionStatusCompleted:
            {
                NSLog(@"Successful!");
            }
                break;
            default:
                break;
        }
    }];
}
else
{
    NSLog(@"Video file not supported!");
}

1 个答案:

答案 0 :(得分:0)

您需要AVMutableComposition才能执行此操作。因为资产无法直接在iOS 5.0下转码为MP4。

- (BOOL)encodeVideo:(NSURL *)videoURL
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

// Create the composition and tracks
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSArray *assetVideoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
if (assetVideoTracks.count <= 0)
{
        NSLog(@"Error reading the transformed video track");
        return NO;
}

// Insert the tracks in the composition's tracks
AVAssetTrack *assetVideoTrack = [assetVideoTracks firstObject];
[videoTrack insertTimeRange:assetVideoTrack.timeRange ofTrack:assetVideoTrack atTime:CMTimeMake(0, 1) error:nil];
[videoTrack setPreferredTransform:assetVideoTrack.preferredTransform];

AVAssetTrack *assetAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[audioTrack insertTimeRange:assetAudioTrack.timeRange ofTrack:assetAudioTrack atTime:CMTimeMake(0, 1) error:nil];

// Export to mp4
NSString *mp4Quality = [MGPublic isIOSAbove:@"6.0"] ? AVAssetExportPresetMediumQuality : AVAssetExportPresetPassthrough;
NSString *exportPath = [NSString stringWithFormat:@"%@/%@.mp4",
                                 [NSHomeDirectory() stringByAppendingString:@"/tmp"],
                                 [BSCommon uuidString]];

NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:mp4Quality];
exportSession.outputURL = exportUrl;
CMTime start = CMTimeMakeWithSeconds(0.0, 0);
CMTimeRange range = CMTimeRangeMake(start, [asset duration]);
exportSession.timeRange = range;
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch ([exportSession status])
        {
            case AVAssetExportSessionStatusCompleted:
                   NSLog(@"MP4 Successful!");
                   break;
            case AVAssetExportSessionStatusFailed:
                   NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                   break;
            case AVAssetExportSessionStatusCancelled:
                   NSLog(@"Export canceled");
                   break;
            default:
                   break;
        }
}];

return YES;
}