如何录制没有任何声音的视频? [关闭]

Posted

技术标签:

【中文标题】如何录制没有任何声音的视频? [关闭]【英文标题】:How to record video without any sounds? [closed] 【发布时间】:2015-03-26 08:25:42 【问题描述】:

我想在应用程序中录制没有任何背景声音或噪音的视频。 有什么好的流程可以做到这一点,以及如何裁剪音频并与录制的视频合并?。

【问题讨论】:

【参考方案1】:

拍摄没有音频的视频

 //----- SETUP CAPTURE SESSION -----
        //---------------------------------
        NSLog(@"Setting up capture session");
        CaptureSession = [[AVCaptureSession alloc] init];

        //----- ADD INPUTS -----
        NSLog(@"Adding video input");

        //ADD VIDEO INPUT
        AVCaptureDevice *VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if (VideoDevice)
        
            NSError *error;
            VideoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
            if (!error)
            
                if ([CaptureSession canAddInput:VideoInputDevice])
                    [CaptureSession addInput:VideoInputDevice];
                else
                    NSLog(@"Couldn't add video input");
            
            else
            
                NSLog(@"Couldn't create video input");
            
        
        else
        
            NSLog(@"Couldn't create video capture device");
        


    //ADD VIDEO PREVIEW LAYER
        NSLog(@"Adding video preview layer");
        [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:CaptureSession] autorelease]];

        PreviewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;     //<<SET ORIENTATION.  You can deliberatly set this wrong to flip the image and may actually need to set it wrong to get the right image

        [[self PreviewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

NSLog(@"Adding movie file output");
    MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

    Float64 TotalSeconds = 60;          //Total seconds
    int32_t preferredTimeScale = 30;    //Frames per second
    CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);   //<<SET MAX DURATION
    MovieFileOutput.maxRecordedDuration = maxDuration;

    MovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;                        //<<SET MIN FREE SPACE IN BYTES FOR RECORDING TO CONTINUE ON A VOLUME

    if ([CaptureSession canAddOutput:MovieFileOutput])
        [CaptureSession addOutput:MovieFileOutput];

    //SET THE CONNECTION PROPERTIES (output properties)
    [self CameraSetOutputProperties];           //(We call a method as it also has to be done after changing camera)



    //----- SET THE IMAGE QUALITY / RESOLUTION -----
    //Options:
    //  AVCaptureSessionPresetHigh - Highest recording quality (varies per device)
    //  AVCaptureSessionPresetMedium - Suitable for WiFi sharing (actual values may change)
    //  AVCaptureSessionPresetLow - Suitable for 3G sharing (actual values may change)
    //  AVCaptureSessionPreset640x480 - 640x480 VGA (check its supported before setting it)
    //  AVCaptureSessionPreset1280x720 - 1280x720 720p HD (check its supported before setting it)
    //  AVCaptureSessionPresetPhoto - Full photo resolution (not supported for video output)
    NSLog(@"Setting image quality");
    [CaptureSession setSessionPreset:AVCaptureSessionPresetMedium];
    if ([CaptureSession canSetSessionPreset:AVCaptureSessionPreset640x480])     //Check size based configs are supported before setting them
        [CaptureSession setSessionPreset:AVCaptureSessionPreset640x480];



    //----- DISPLAY THE PREVIEW LAYER -----
    //Display it full screen under out view controller existing controls
    NSLog(@"Display the preview layer");
    CGRect layerRect = [[[self view] layer] bounds];
    [PreviewLayer setBounds:layerRect];
    [PreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                                  CGRectGetMidY(layerRect))];
    //[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
    //We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):
    UIView *CameraView = [[[UIView alloc] init] autorelease];
    [[self view] addSubview:CameraView];
    [self.view sendSubviewToBack:CameraView];

    [[CameraView layer] addSublayer:PreviewLayer];


    //----- START THE CAPTURE SESSION RUNNING -----
    [CaptureSession startRunning];

合并音频

 //Create AVMutableComposition Object which will hold our multiple AVMutableCompositionTrack or we can say it will hold our video and audio files.
    AVMutableComposition* mixComposition = [AVMutableComposition composition];

    //Now first load your audio file using AVURLAsset. Make sure you give the correct path of your videos.
    NSURL *audio_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Asteroid_Sound" ofType:@"mp3"]];
    AVURLAsset  *audioAsset = [[AVURLAsset alloc]initWithURL:audio_url options:nil];
    CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

    //Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

    //Now we will load video file.
    NSURL *video_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Asteroid_Video" ofType:@"m4v"]];
    AVURLAsset  *videoAsset = [[AVURLAsset alloc]initWithURL:video_url options:nil];
    CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,audioAsset.duration);

    //Now we are creating the second AVMutableCompositionTrack containing our video and add it to our AVMutableComposition object.
    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

    //decide the path where you want to store the final video created with audio and video merge.
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"FinalVideo.mov"]];
    NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
        [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

    //Now create an AVAssetExportSession object that will save your final video at specified path.
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    _assetExport.outputURL = outputFileUrl;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) 

         dispatch_async(dispatch_get_main_queue(), ^
             [self exportDidFinish:_assetExport];
         );
     
     ];

【讨论】:

这是不合适的代码 Aju 仍然会出现录音中的噪音和音频。我认为您刚刚复制并粘贴了来自 - ios-developer.net/iphone-ipad-programmer/development/camera/… 的代码。这也没有帮助 我已经解决了我的问题,这是静音背景声音或噪音的最终代码 - AVCaptureConnection* audioConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeAudio]; if(audioConnection) audioConnection.enabled=NO; 【参考方案2】:

AVCapturesession 让您录制没有声音的视频,然后使用 AVMutableComposition 插入单独的轨道。

【讨论】:

以上是关于如何录制没有任何声音的视频? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

手机录制视频没有声音怎么办

WIN10 如何彻底关闭游戏录制功能 Xbox已完全卸载 但还是有录制功能 有大神知道怎么弄么

如何使用 WaveOut\waveIn Api 录制和播放声音 [关闭]

将音频和视频文件合并为一个 [C++] [关闭]

视频仅在第一次尝试时不录制音频

如何从音频文件中隔离语音? [关闭]