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

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

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

核心思路

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

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

視頻截取示例(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)建導(dǎo)出會(huì)話(huà)
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
    
    // 2. 設(shè)置輸出路徑和文件格式
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"trimmedVideo.mp4"];
    exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    
    // 3. 設(shè)置時(shí)間范圍(start ~ start + duration)
    CMTime startCMTime = CMTimeMakeWithSeconds(startTime, 600);
    CMTime durationCMTime = CMTimeMakeWithSeconds(duration, 600);
    CMTimeRange timeRange = CMTimeRangeMake(startCMTime, durationCMTime);
    exportSession.timeRange = timeRange;
    
    // 4. 異步導(dǎo)出
    [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);
    }
}];

注意事項(xiàng)

項(xiàng)目說(shuō)明
時(shí)間單位使用 CMTimeMakeWithSeconds 將秒數(shù)轉(zhuǎn)換為 CMTime
輸出路徑使用 NSTemporaryDirectory() 可避免存儲(chǔ)問(wèn)題
輸出格式視頻推薦 .mp4,音頻推薦 .m4a.caf
導(dǎo)出性能使用 AVAssetExportPresetLowQuality 可提升處理速度
錯(cuò)誤處理檢查 exportSession.statusexportSession.error

擴(kuò)展建議

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

總結(jié)

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

以上就是在iOS中截取和分割音視頻的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于iOS截取和分割音視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

运城市| 北碚区| 富锦市| 东兰县| 海淀区| 德惠市| 吉首市| 鄂托克前旗| 柏乡县| 张北县| 中山市| 安仁县| 涡阳县| 兴隆县| 东山县| 贡觉县| 光山县| 肥乡县| 平湖市| 三门峡市| 塔河县| 桃源县| 稷山县| 芦溪县| 大关县| 清镇市| 兴业县| 习水县| 南澳县| 昂仁县| 班戈县| 河东区| 滦平县| 苗栗县| 皮山县| 广宗县| 米脂县| 会泽县| 万宁市| 临邑县| 轮台县|