2

我正在尝试修剪现有的视频剪辑并将剪辑重新保存在与原始文件相同的位置。但是,当我运行我的应用程序时,出现此错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无效的输出文件类型”

我找到了建议,但它们要求我更改 AVMediaTypeVideo 的输出文件类型。我想保留 AVMediaTypeVideo 因为这是原始视频文件保存的内容。

这是我到目前为止所拥有的:

AVMutableComposition *finalClip = [[AVMutableComposition alloc]init];

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];

NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];

AVURLAsset *videoclip = [AVURLAsset URLAssetWithURL:outputURL options:nil];

AVMutableCompositionTrack *finalClipTrack = [finalClip addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[finalClipTrack insertTimeRange:CMTimeRangeMake(CMTimeMake((duration*indexNum), 1), CMTimeMake(duration,1)) ofTrack:[[videoclip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

NSString *outputPathwe = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"outputwe.mov"];

NSURL *outputURLwe = [[NSURL alloc] initFileURLWithPath:outputPathwe];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputPathwe])
    [[NSFileManager defaultManager] removeItemAtPath:outputPathwe error:nil];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:finalClip presetName:AVAssetExportPresetHighestQuality];

exporter.outputFileType = AVMediaTypeVideo;

exporter.outputURL=outputURLwe;

[exporter exportAsynchronouslyWithCompletionHandler:^{

    dispatch_async(dispatch_get_main_queue(), ^{

        [self exportDidFinish:exporter];

    });
}];

我觉得我只是想念它真的很容易。这是我第一次使用 AVFoundation,所以任何帮助将不胜感激!

4

1 回答 1

5

AVMediaTypeVideo是“媒体类型”而不是“输出文件类型”。您的原始视频包含AVMediaTypeVideo 类型的轨道原始视频不是类型AVMediaTypeVideo

outputFileTypeAVAssetExportSession 是类型的常量NSString。允许的值在 AVFoundation/AVMediaFormat.h 中列出。对于视频,它们是:

  • AVFileTypeQuickTimeMovie
  • AVFileTypeMPEG4
  • AV文件类型AppleM4V

您必须选择一个允许的值用于您AVAssetExportSessionoutputFileType.

于 2014-09-24T13:47:53.557 回答