Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios:从视频中删除音频_Ios_Iphone_Audio_Video - Fatal编程技术网

Ios:从视频中删除音频

Ios:从视频中删除音频,ios,iphone,audio,video,Ios,Iphone,Audio,Video,我有一些时候要求静音视频发送终端用户,所以我需要删除音频文件的视频。 为静音的单个进程传递视频 在Swift 3中 // Remove audio from video - (void) RemoveAudioFromVideo:(NSString *)VideoLocalPath { NSString * initPath1 = VideoLocalPath; AVMutableComposition *composition = [AVMutableComposit

我有一些时候要求静音视频发送终端用户,所以我需要删除音频文件的视频。 为静音的单个进程传递视频

在Swift 3中

// Remove audio from video     
- (void) RemoveAudioFromVideo:(NSString *)VideoLocalPath {
    NSString * initPath1 = VideoLocalPath;
    AVMutableComposition *composition = [AVMutableComposition composition];

    NSString *inputVideoPath = initPath1;
    AVURLAsset * sourceAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:inputVideoPath] options:nil];

    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    BOOL ok = NO;

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

    CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]);

    ok = [compositionVideoTrack insertTimeRange:x ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil];

    if([[NSFileManager defaultManager] fileExistsAtPath:initPath1]) {
        [[NSFileManager defaultManager] removeItemAtPath:initPath1 error:nil];
    }

    NSURL *url = [[NSURL alloc] initFileURLWithPath: initPath1];

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

    exporter.outputURL=url;

    NSLog(@";%@", [exporter supportedFileTypes]);

    exporter.outputFileType = @"com.apple.quicktime-movie";
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        [self savefinalVideoFileToDocuments:exporter.outputURL];
    }];
}

// Write final Video
-(void)savefinalVideoFileToDocuments:(NSURL *)url {
    NSString *storePath = [[self applicationCacheDirectory] stringByAppendingPathComponent:@"Videos"];
    NSData * movieData = [NSData dataWithContentsOfURL:url];
    [movieData writeToFile:storePath atomically:YES];
}

// Directory Path
- (NSString *)applicationCacheDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return documentsDirectory;
}
func removeAudioFromVideo(_ videoPath: String) {
    let initPath1: String = videoPath
    let composition = AVMutableComposition()
    let inputVideoPath: String = initPath1
    let sourceAsset = AVURLAsset(url: URL(fileURLWithPath: inputVideoPath), options: nil)
    let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    let sourceVideoTrack: AVAssetTrack? = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
    let x: CMTimeRange = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration)
    _ = try? compositionVideoTrack!.insertTimeRange(x, of: sourceVideoTrack!, at: kCMTimeZero)
    if FileManager.default.fileExists(atPath: initPath1) {
        try? FileManager.default.removeItem(atPath: initPath1)
    }
    let url = URL(fileURLWithPath: initPath1)
    let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
    exporter?.outputURL = url
    exporter?.outputFileType = "com.apple.quicktime-movie"
    exporter?.exportAsynchronously(completionHandler: {() -> Void in
        self.saveFinalVideoFile(toDocuments: exporter!.outputURL!)
    })
}

func saveFinalVideoFile(toDocuments url: URL) {
    let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Videos")
    let movieData = try? Data(contentsOf: url)
    try? movieData?.write(to: fileURL, options: .atomic)
}