簡體   English   中英

AVAssetExportSession不導出時間范圍

[英]AVAssetExportSession not exporting time range

我有這個問題。 我正在通過AVAssetExportSession修剪聲音文件。 我設置時間范圍,然后異步導出。 我將輸出文件保存在與輸入文件不同的名稱下。

它工作正常,但這是第一次。 當我嘗試修剪修剪過的文件時,它會以整個持續時間導出它,但CMTimeRangeShow顯示正確的時間范圍。

誰知道,我做錯了什么?

我不確定我的代碼現在可用,因為它適用於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;
}

您應該檢查輸出文件是否已存在。

“exportAsynchronouslyWithCompletionHandler”不會覆蓋outputfile。

exportAsynchronouslyWithCompletionHandler檢查導出會話的stateerror信息,確保輸出URL文件不存在。

參考這兩個主題,祝你好運!

無法使用AVAssetExportSession修剪視頻

為AVAssetExportSession創建時間范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM