【问题标题】:AVAssetExportSession not exporting time rangeAVAssetExportSession 不导出时间范围
【发布时间】:2013-11-25 21:19:22
【问题描述】:

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

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

谁知道,我做错了什么?

【问题讨论】:

  • 您确定您正确使用了 CMTimeRangeMake 吗?您将开始时间和持续时间传递给它,而不是开始和结束时间。
  • 是的,我这样做:CMTime startTime = CMTimeMultiplyByFloat64(asset.duration, fromPerc); CMTime stopTime = CMTimeMultiplyByFloat64(asset.duration, toPerc); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
  • 你解决过这个问题吗?

标签: ios audio trim avassetexportsession


【解决方案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;
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      Unable to trim a video using AVAssetExportSession

      Creating a time range for AVAssetExportSession

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        相关资源
        最近更新 更多