4

我有这个问题。我正在通过 AVAssetExportSession 修剪声音文件。我设置了时间范围,然后异步导出。我将输出文件保存在与输入文件不同的名称下。

它工作正常,但只是第一次。当我尝试修剪修剪的文件时,它会以整个持续时间导出它,但 CMTimeRangeShow 显示正确的时间范围。

任何人都知道,我做错了什么?

4

3 回答 3

1

我不确定我的代码现在是否可供您使用,因为它适用于 iOS7。我希望这能帮到您。

- (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;
}
于 2014-10-27T21:41:16.753 回答
0

您应该检查输出文件是否已经存在。

“exportAsynchronouslyWithCompletionHandler”不会覆盖输出文件。

于 2016-08-08T08:20:56.410 回答
0

检查导出会话state和其中的error信息exportAsynchronouslyWithCompletionHandler,确保输出 URL 文件不存在。

参考这两个线程,祝你好运!

无法使用 AVAssetExportSession 修剪视频

为 AVAssetExportSession 创建时间范围

于 2017-07-05T15:53:29.687 回答