4

I have this problem. I'm trimming a sound file via AVAssetExportSession. I set the time range and then export asynchronously. I'm saving the output file under different name than the input file.

It works fine BUT only for the first time. When I try to trim the trimmed file, it exports it with whole duration, but CMTimeRangeShow shows right time range.

Anyone knows, what I'm doing wrong?

Skiny
  • 405
  • 1
  • 4
  • 16
  • Did you ever solve this? – socca1157 Aug 20 '16 at 02:47
  • Are you sure you're using CMTimeRangeMake correctly? You pass it a start time and a duration, NOT a start and end time. – Nick Dec 04 '13 at 18:35
  • Yes, I do it like this: CMTime startTime = CMTimeMultiplyByFloat64(asset.duration, fromPerc); CMTime stopTime = CMTimeMultiplyByFloat64(asset.duration, toPerc); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); – Skiny Dec 07 '13 at 19:08

3 Answers3

1

I am not sure my code is available for you right now because it worked on iOS7. I hope this will help you.

- (BOOL)trimAudio :(NSURL *) url
{
    float vocalStartMarker = timeFrom;
    float vocalEndMarker = timeTo;
    NSURL *audioFileInput = url_Audio;
    NSURL *audioFileOutput = url;

    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...
             [[[UIAlertView alloc]initWithTitle:@"Unknown Error" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]show];
         }
     }];

    return YES;
}
AppHero2
  • 681
  • 7
  • 25
1

You should check if the output file already exists.

"exportAsynchronouslyWithCompletionHandler" does not overwrite outputfile.

Sparky
  • 119
  • 1
  • 4
0

Check the export session's state and error info within exportAsynchronouslyWithCompletionHandler, make sure the output URL file not exists.

Refer to these two threads, good luck!

Unable to trim a video using AVAssetExportSession

Creating a time range for AVAssetExportSession

Itachi
  • 5,777
  • 2
  • 37
  • 69