簡體   English   中英

靜音視頻+轉換視頻使用AVAssetExportSession的單一導出操作

[英]Mute Video + Transform Video Using single export operation of AVAssetExportSession

我有以下代碼來修復視頻的Tranform

    - (AVVideoComposition *)squareVideoCompositionFor:(AVAsset *)asset {

    AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;

    CGFloat length = MAX(track.naturalSize.width, track.naturalSize.height);

    CGSize size = track.naturalSize;

    CGFloat scale = 0;

    CGAffineTransform transform = track.preferredTransform;

    if (transform.a == 0 && transform.b == 1 && transform.c == -1 && transform.d == 0) {
        scale = -1;
    }
    else if (transform.a == 0 && transform.b == -1 && transform.c == 1 && transform.d == 0) {
        scale = -1;
    }
    else if (transform.a == 1 && transform.b == 0 && transform.c == 0 && transform.d == 1) {
        scale = 1;
    }
    else if (transform.a == -1 && transform.b == 0 && transform.c == 0 && transform.d == -1) {
        scale = -1;
    }

    transform = CGAffineTransformTranslate(transform, scale * -(size.width - length) / 2, scale * -(size.height - length) / 2);




    AVMutableVideoCompositionLayerInstruction *transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:track];
    [transformer setTransform:transform atTime:kCMTimeZero];

//    CGAffineTransform finalTransform = t2;
//    [transformer setTransform:finalTransform atTime:kCMTimeZero];

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity);
    instruction.layerInstructions = @[transformer];


    AVMutableVideoComposition *composition = [AVMutableVideoComposition videoComposition];
    composition.frameDuration = CMTimeMake(1, 30);
    composition.renderSize =  CGSizeMake(length, length);
    composition.instructions = @[instruction];
    composition.renderScale = 1.0;


    return composition;
    }

並遵循靜音音頻的代碼

- (AVMutableComposition *) removeAudioFromVideoFileFor:(AVAsset *)asset  {
    AVMutableComposition *composition_Mix = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositionVideoTrack = [composition_Mix addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    BOOL ok = NO;

    AVAssetTrack * sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [asset duration]);
    NSError *error;
    ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];

    return composition_Mix;
}

我在這里如何調用該函數

    AVAsset *asset = [AVAsset assetWithURL:inputURL];

    AVMutableComposition *composition = [self  removeAudioFromVideoFileFor:asset];

    AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
    session.videoComposition = [self squareVideoCompositionFor:asset];
    session.outputURL = outputURL;
    session.outputFileType = AVFileTypeMPEG4;
    session.shouldOptimizeForNetworkUse = true;
    session.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

但如果我同時使用了composition[self squareVideoCompositionFor:asset]則會顯示錯誤

Error Domain = AVFoundationErrorDomain Code = -11841“Operation Stopped”UserInfo = {NSLocalizedDescription = Operation Stopped,NSLocalizedFailureReason =視頻無法合成。}

如果我省略一個然后它工作正常意味着一個AVAssetExportSession可以靜音來自視頻或squareVideo

有沒有辦法可以使用AVAssetExportSession導出的單個進度來實現這兩種方法?

您的代碼看起來不錯,但我已對代碼進行了更改以使其正常工作。

inputURLoutputURL應該以file://https://作為前綴(因為它是url,在你的情況下它應該以file://開頭)

如果您的無效,那么您將無法獲得所需的輸出。

//FOR OUTPUT URL
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

//the output video will be written to file final.mp4
NSURL *outputURL = [NSURL fileURLWithPath:path];
outputURL = [outputURL URLByAppendingPathComponent:@"final.mp4"];
NSLog(@"outputURL = %@", outputURL);


//FOR INPUT URL
//This is the path of the bundle resource that is going to be used
NSURL *inputURL = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"];
NSLog(@"inputURL = %@", inputURL);

導出構圖

//this will export the composition with the specified configuration
[session exportAsynchronouslyWithCompletionHandler:^{
    NSLog(@"Success");
}];

當您在控制台中看到“成功”日志時,請檢查應用程序的文檔目錄。 視頻將在outptURL上寫。

注意:使用CMD + SHIFT + G並調整outputURL。 您將被重定向到應用程序的文檔文件夾(僅適用於模擬器)。 對於設備,您需要下載應用容器並查看包內容。

完整的代碼

removeAudioFromVideoFileFor:squareVideoCompositionFor:方法看起來不錯。 只需要改變以下內容。

這里“video”是app bundle中資源文件的名稱。

- (void)viewDidLoad {
[super viewDidLoad];


   NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
   path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  NSURL *outputURL = [NSURL fileURLWithPath:path];
outputURL = [outputURL URLByAppendingPathComponent:@"final.mp4"];
  NSLog(@"outputURL = %@", outputURL);


  NSURL *inputURL = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"];
  NSLog(@"inputURL = %@", inputURL);

  AVAsset *asset = [AVAsset assetWithURL:inputURL];

  AVMutableComposition *composition = [self removeAudioFromVideoFileFor: asset];

  AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
  session.videoComposition = [self squareVideoCompositionFor:asset];
  session.outputURL = outputURL;
  session.outputFileType = AVFileTypeMPEG4;
  session.shouldOptimizeForNetworkUse = true;
  session.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

  [session exportAsynchronouslyWithCompletionHandler:^{
     NSLog(@"Success:");
  }];
}

希望它會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM