iOS音频剪辑

14

我进行了大量搜索,但没有找到相关的信息... 我现在正在处理iOS音频文件,以下是我的想法...

  1. 录制音频并保存剪辑(已检查,我使用了AVAudioRecorder
  2. 改变音调(已检查,我使用了Dirac)
  3. 剪辑音频 :(

我有两个标记,即开始和结束偏移量,并使用这些信息来剪辑记录的文件并将其保存回去。 我不想使用“seek”,因为稍后我想同步播放所有录制的文件(就像时间轴中的Flash电影剪辑),最后我想将其导出为一个音频文件。


感谢mattjgalloway的编辑... - Muhammad Usama
4个回答

31

以下是我用来修剪预先存在文件的音频的代码。如果你已保存或正在保存到其他格式,则需要更改与M4A相关的常量。

- (BOOL)trimAudio
{
    float vocalStartMarker = <starting time>;
    float vocalEndMarker = <ending time>;

    NSURL *audioFileInput = <your pre-existing file>;
    NSURL *audioFileOutput = <the file you want to create>;

    if (!audioFileInput || !audioFileOutput)
    {
        return NO;
    }

    [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
    AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                            presetName:AVAssetExportPresetAppleM4A];

    if (exportSession == nil)
    {        
        return NO;
    }

    CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100);
    CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    exportSession.outputURL = audioFileOutput;
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.timeRange = exportTimeRange;

    [exportSession exportAsynchronouslyWithCompletionHandler:^
     {
         if (AVAssetExportSessionStatusCompleted == exportSession.status)
         {
             // It worked!
         } 
         else if (AVAssetExportSessionStatusFailed == exportSession.status)
         {
             // It failed...
         }
     }];

    return YES;
}

还有技术问答 1730,提供了稍微详细一些的方法。


vocalStartMarker和vocalEndMarker的范围是[0,1],对吗? - Hai Hw
1
vocalStartMarker和vocalEndMarker以秒为单位。例如,如果要剪辑一个10秒的片段中间的一秒钟,则应将vocalStartMarker设置为4.5,vocalEndMarker设置为5.5。 - kyleobrien
我认为我们只需使用:CMTime startTime = CMTimeMake(vocalStartMarker, 1); - Hai Hw
当我导出为m4a格式时,如何指定音频比特率? - moligaloo
我正在使用相同的代码来修剪15秒音频,但每次得到的时间都不同,例如19、16、15秒。以下是代码: float vocalStartMarker = 0.0f; float vocalEndMarker = 15.0f; CMTime startTime = CMTimeMake((int)(floor(vocalStartMarker * 100)), 100); CMTime stopTime = CMTimeMake((int)(ceil(vocalEndMarker * 100)), 100); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); - iVarun
我找不到如何从音频文件中删除内容的答案,例如如果我想从第3秒到第10秒删除。你能帮忙看一下这个问题吗? https://stackoverflow.com/questions/53928988/swift-how-to-delete-part-of-audio - mahdi

3

在 .m 文件中导入以下两个库:

#import "BJRangeSliderWithProgress.h"
#import  < AVFoundation/AVFoundation.h >

在此之后,粘贴以下代码,您就能够使用两个拇指来剪辑音频文件。

- (void) viewDidLoad
{
      [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

       mySlider = [[BJRangeSliderWithProgress alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];

      [mySlider setDisplayMode:BJRSWPAudioSetTrimMode];

      [mySlider addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];

      [mySlider setMinValue:0.0];

      NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"];

      NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];

      audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audioFileInput error:nil];

      [mySlider setMaxValue:audioPlayer.duration];

      [self.view addSubview:mySlider];
}

-(void)valueChanged {
      NSLog(@"%f %f", mySlider.leftValue, mySlider.rightValue);
}


-(IBAction)playTheSong
{
      // Path of your source audio file
      NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"saewill.mp3"];
      NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];

      // Path of your destination save audio file
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
      NSString *libraryCachesDirectory = [paths objectAtIndex:0];
      //libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];

      NSString *strOutputFilePath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.mov"];
      NSString *requiredOutputPath = [libraryCachesDirectory stringByAppendingPathComponent:@"output.m4a"];
      NSURL *audioFileOutput = [NSURL fileURLWithPath:requiredOutputPath];

      [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
      AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

      AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                              presetName:AVAssetExportPresetAppleM4A];


      float startTrimTime = mySlider.leftValue;
      float endTrimTime = mySlider.rightValue;

      CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100);
      CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100);
      CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

      exportSession.outputURL = audioFileOutput;
      exportSession.outputFileType = AVFileTypeAppleM4A;
      exportSession.timeRange = exportTimeRange;

      [exportSession exportAsynchronouslyWithCompletionHandler:^
       {
           if (AVAssetExportSessionStatusCompleted == exportSession.status)
           {
               NSLog(@"Success!");
               NSLog(@" OUtput path is \n %@", requiredOutputPath);
               NSFileManager * fm = [[NSFileManager alloc] init];
               [fm moveItemAtPath:strOutputFilePath toPath:requiredOutputPath error:nil];
               //[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];

               NSURL *url=[NSURL fileURLWithPath:requiredOutputPath];
               NSError *error;
               audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
               audioPlayer.numberOfLoops=0;
               [audioPlayer play];

           }
           else if (AVAssetExportSessionStatusFailed == exportSession.status)
           {
               NSLog(@"failed with error: %@", exportSession.error.localizedDescription);
           }
       }];

}

你可以通过添加一些关于正在发生的事情的注释来改善这个答案。 - arghtype
屏幕上会有一个滑块和两个拇指。通过拇指的帮助,您将能够修剪音频(.m4a)。 - Sumit Srivastava

3

// Swift 4.2

如果有人仍在寻找Swift的答案,这里有一个。

//音频剪辑

func trimAudio(asset: AVAsset, startTime: Double, stopTime: Double, finished:@escaping (URL) -> ())
{

        let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith:asset)

        if compatiblePresets.contains(AVAssetExportPresetMediumQuality) {

        guard let exportSession = AVAssetExportSession(asset: asset, 
        presetName: AVAssetExportPresetAppleM4A) else{return}

        // Creating new output File url and removing it if already exists.
        let furl = createUrlInAppDD("trimmedAudio.m4a") //Custom Function
        removeFileIfExists(fileURL: furl) //Custom Function

        exportSession.outputURL = furl
        exportSession.outputFileType = AVFileType.m4a

        let start: CMTime = CMTimeMakeWithSeconds(startTime, preferredTimescale: asset.duration.timescale)
        let stop: CMTime = CMTimeMakeWithSeconds(stopTime, preferredTimescale: asset.duration.timescale)
        let range: CMTimeRange = CMTimeRangeFromTimeToTime(start: start, end: stop)
        exportSession.timeRange = range

        exportSession.exportAsynchronously(completionHandler: {

            switch exportSession.status {
            case .failed:
                print("Export failed: \(exportSession.error!.localizedDescription)")
            case .cancelled:
                print("Export canceled")
            default:
                print("Successfully trimmed audio")
                DispatchQueue.main.async(execute: {
                    finished(furl)
                })
            }
        })
    }
}

你也可以将其用于视频裁剪。要进行视频裁剪,请将导出会话的值替换为以下内容:

guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else{return}

文件类型更改为 mp4

exportSession.outputFileType = AVFileType.mp4


嘿,我遇到了一个错误:“操作无法完成”。请帮我解决一下。 - Yogesh Patel

2
这里是一个示例代码,可以从音频文件的起始和结束偏移量进行修剪,并将其保存回去。 请查看这个iOS音频修剪
// Path of your source audio file
NSString *strInputFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"abc.mp3"];
NSURL *audioFileInput = [NSURL fileURLWithPath:strInputFilePath];

// Path of your destination save audio file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryCachesDirectory = [paths objectAtIndex:0];
    libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];

NSString *strOutputFilePath = [NSString stringWithFormat:@"%@%@",libraryCachesDirectory,@"/abc.mp4"];
NSURL *audioFileOutput = [NSURL fileURLWithPath:strOutputFilePath];

if (!audioFileInput || !audioFileOutput)
{
    return NO;
}

[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVAsset *asset = [AVAsset assetWithURL:audioFileInput];

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A];

if (exportSession == nil)
{
    return NO;
}
float startTrimTime = 0; 
float endTrimTime = 5;

CMTime startTime = CMTimeMake((int)(floor(startTrimTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(endTrimTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = exportTimeRange;

[exportSession exportAsynchronouslyWithCompletionHandler:^
{
    if (AVAssetExportSessionStatusCompleted == exportSession.status)
    {
        NSLog(@"Success!");
    }
    else if (AVAssetExportSessionStatusFailed == exportSession.status)
    {
        NSLog(@"failed");
    }
}];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接