简体   繁体   English

AVAssetExportSession不导出时间范围

[英]AVAssetExportSession not exporting time range

I have this problem. 我有这个问题。 I'm trimming a sound file via AVAssetExportSession. 我正在通过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. 当我尝试修剪修剪过的文件时,它会以整个持续时间导出它,但CMTimeRangeShow显示正确的时间范围。

Anyone knows, what I'm doing wrong? 谁知道,我做错了什么?

I am not sure my code is available for you right now because it worked on iOS7. 我不确定我的代码现在可用,因为它适用于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;
}

You should check if the output file already exists. 您应该检查输出文件是否已存在。

"exportAsynchronouslyWithCompletionHandler" does not overwrite outputfile. “exportAsynchronouslyWithCompletionHandler”不会覆盖outputfile。

Check the export session's state and error info within exportAsynchronouslyWithCompletionHandler , make sure the output URL file not exists. exportAsynchronouslyWithCompletionHandler检查导出会话的stateerror信息,确保输出URL文件不存在。

Refer to these two threads, good luck! 参考这两个主题,祝你好运!

Unable to trim a video using AVAssetExportSession 无法使用AVAssetExportSession修剪视频

Creating a time range for AVAssetExportSession 为AVAssetExportSession创建时间范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM