iOS Audio Trimming - PullRequest
       50

iOS Audio Trimming

10 голосов
/ 14 марта 2012

Я много искал и не смог найти ничего подходящего ... Я работаю над аудио-файлами iOS, и вот что я хочу сделать ...

  1. Запись аудио и сохранение клипа (Проверено, я сделал это с помощью AVAudioRecorder)
  2. Изменить высоту тона (Проверено, Это с помощью Dirac)
  3. Подгонка: (

У меня есть два маркера, т.е.начальное и конечное смещение, и используя эту информацию, я хочу обрезать записанный файл и сохранить его обратно. Я не хочу использовать «поиск», потому что позже я хочу воспроизвести все записанные файлы синхронно (так же, как клипы во флэш-фильмах вграфик), а затем, наконец, я хочу экспортировать как один аудиофайл.

Ответы [ 4 ]

27 голосов
/ 28 мая 2012

Вот код, который я использовал для обрезки аудио из уже существующего файла.Вам нужно изменить константы, связанные с 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 , что дает немного более подробный подход.

2 голосов
/ 29 июля 2014

импорт следующих двух библиотек в .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);
           }
       }];

}
2 голосов
/ 10 июня 2013

Вот пример кода, который обрезает аудиофайл от начального и конечного смещения и сохраняет его обратно.Пожалуйста, отметьте это iOS Audio Trimming .

// 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");
    }
}];
0 голосов
/ 08 февраля 2019

// 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

...