最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在iOS中截取和分割音視頻的代碼示例

 更新時間:2025年05月20日 08:26:47   作者:90后晨仔  
在 iOS 開發(fā)中,截取或分割音視頻是常見需求,適用于短視頻剪輯、語音消息裁剪、媒體內容編輯等場景,使用 AVFoundation 框架可以高效實現(xiàn)這一功能,下面將詳細介紹如何在 iOS 中截取或分割音視頻,并提供完整的代碼示例和使用方法,需要的朋友可以參考下

核心思路

截取或分割音視頻的核心步驟如下:

  • 加載原始音視頻文件AVURLAsset
  • 設置時間范圍CMTimeRange)指定要截取的起始時間與持續(xù)時間
  • 創(chuàng)建導出會話AVAssetExportSession
  • 導出目標文件(支持 .mp4、.m4a 等格式)
  • 處理異步導出完成回調

視頻截取示例(Objective-C)

- (void)trimVideoFromURL:(NSURL *)inputURL startTime:(NSTimeInterval)startTime duration:(NSTimeInterval)duration completion:(void (^)(NSURL *outputURL, NSError *error))completion {
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    
    // 1. 創(chuàng)建導出會話
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
    
    // 2. 設置輸出路徑和文件格式
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"trimmedVideo.mp4"];
    exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    
    // 3. 設置時間范圍(start ~ start + duration)
    CMTime startCMTime = CMTimeMakeWithSeconds(startTime, 600);
    CMTime durationCMTime = CMTimeMakeWithSeconds(duration, 600);
    CMTimeRange timeRange = CMTimeRangeMake(startCMTime, durationCMTime);
    exportSession.timeRange = timeRange;
    
    // 4. 異步導出
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"視頻截取成功: %@", outputPath);
            if (completion) completion([NSURL fileURLWithPath:outputPath], nil);
        } else {
            NSError *error = exportSession.error;
            NSLog(@"視頻截取失敗: %@", error.localizedDescription);
            if (completion) completion(nil, error);
        }
    }];
}

使用方法

NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4"]];
[self trimVideoFromURL:videoURL startTime:5.0 duration:10.0 completion:^(NSURL *outputURL, NSError *error) {
    if (outputURL) {
        NSLog(@"截取后的視頻路徑: %@", outputURL.path);
    }
}];

音頻截取示例(Objective-C)

- (void)trimAudioFromURL:(NSURL *)inputURL startTime:(NSTimeInterval)startTime duration:(NSTimeInterval)duration completion:(void (^)(NSURL *outputURL, NSError *error))completion {
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
    
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"trimmedAudio.m4a"];
    exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
    exportSession.outputFileType = AVFileTypeAppleM4A;
    
    CMTime startCMTime = CMTimeMakeWithSeconds(startTime, 600);
    CMTime durationCMTime = CMTimeMakeWithSeconds(duration, 600);
    CMTimeRange timeRange = CMTimeRangeMake(startCMTime, durationCMTime);
    exportSession.timeRange = timeRange;
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"音頻截取成功: %@", outputPath);
            if (completion) completion([NSURL fileURLWithPath:outputPath], nil);
        } else {
            NSError *error = exportSession.error;
            NSLog(@"音頻截取失敗: %@", error.localizedDescription);
            if (completion) completion(nil, error);
        }
    }];
}

使用方法

NSURL *audioURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myAudio" ofType:@"mp3"]];
[self trimAudioFromURL:audioURL startTime:3.0 duration:5.0 completion:^(NSURL *outputURL, NSError *error) {
    if (outputURL) {
        NSLog(@"截取后的音頻路徑: %@", outputURL.path);
    }
}];

注意事項

項目說明
時間單位使用 CMTimeMakeWithSeconds 將秒數(shù)轉換為 CMTime
輸出路徑使用 NSTemporaryDirectory() 可避免存儲問題
輸出格式視頻推薦 .mp4,音頻推薦 .m4a.caf
導出性能使用 AVAssetExportPresetLowQuality 可提升處理速度
錯誤處理檢查 exportSession.statusexportSession.error

擴展建議

  • 多片段拼接:可結合 AVMutableComposition 實現(xiàn)多段裁剪后的內容拼接。
  • 后臺導出:大文件建議在后臺線程執(zhí)行,避免阻塞主線程。
  • 第三方庫:如需更復雜剪輯功能,可使用 FFmpeg-iOSGPUImage。

總結

通過 AVAssetExportSessiontimeRange 屬性,你可以輕松地從音視頻文件中截取任意時間段的內容。這個方法既適用于音頻也適用于視頻,具有良好的兼容性和性能表現(xiàn),是 iOS 音視頻處理中的基礎技能之一。

以上就是在iOS中截取和分割音視頻的代碼示例的詳細內容,更多關于iOS截取和分割音視頻的資料請關注腳本之家其它相關文章!

相關文章

最新評論

宝清县| 龙山县| 永平县| 石泉县| 区。| 林周县| 河曲县| 忻城县| 密山市| 长宁区| 开原市| 广丰县| 固镇县| 鄂温| 花垣县| 清徐县| 巴林左旗| 蓬溪县| 阜康市| 察雅县| 商洛市| 万年县| 丁青县| 和硕县| 如东县| 博爱县| 康平县| 普定县| 汉川市| 卢氏县| 察隅县| 溧水县| 普定县| 青州市| 台南市| 克山县| 饶河县| 铜鼓县| 舞阳县| 牙克石市| 于田县|