在iOS中截取和分割音視頻的代碼示例
核心思路
截取或分割音視頻的核心步驟如下:
- 加載原始音視頻文件(
AVURLAsset
) - 設(shè)置時(shí)間范圍(
CMTimeRange
)指定要截取的起始時(shí)間與持續(xù)時(shí)間 - 創(chuàng)建導(dǎo)出會(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ì)話 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.status 和 exportSession.error |
擴(kuò)展建議
- 多片段拼接:可結(jié)合
AVMutableComposition
實(shí)現(xiàn)多段裁剪后的內(nèi)容拼接。 - 后臺(tái)導(dǎo)出:大文件建議在后臺(tái)線程執(zhí)行,避免阻塞主線程。
- 第三方庫(kù):如需更復(fù)雜剪輯功能,可使用 FFmpeg-iOS 或 GPUImage。
總結(jié)
通過(guò) AVAssetExportSession
的 timeRange
屬性,你可以輕松地從音視頻文件中截取任意時(shí)間段的內(nèi)容。這個(gè)方法既適用于音頻也適用于視頻,具有良好的兼容性和性能表現(xiàn),是 iOS 音視頻處理中的基礎(chǔ)技能之一。
以上就是在iOS中截取和分割音視頻的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于iOS截取和分割音視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化
本篇文章主要介紹了使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07iOS利用UIScrollView實(shí)現(xiàn)無(wú)限滾動(dòng)效果
這篇文章主要給大家介紹了iOS如何利用UIScrollView實(shí)現(xiàn)無(wú)限滾動(dòng)的效果,首先需要說(shuō)明的是,文本所講的是一種"笨辦法",但是好理解且容易實(shí)現(xiàn),在圖片不多的時(shí)候用它也無(wú)妨。感興趣的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2016-12-12詳解IOS開(kāi)發(fā)中圖片上傳時(shí)兩種圖片壓縮方式的比較
這篇文章主要介紹了IOS開(kāi)發(fā)中圖片上傳時(shí)兩種圖片壓縮方式的比較,需要的朋友可以參考下2017-03-03iOS開(kāi)發(fā)技能weak和strong修飾符的規(guī)范使用詳解
這篇文章主要為大家介紹了iOS開(kāi)發(fā)技能weak和strong修飾符的規(guī)范使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07IOS開(kāi)發(fā)中NSURL的基本操作及用法詳解
NSURL其實(shí)就是我們?cè)跒g覽器上看到的網(wǎng)站地址,這不就是一個(gè)字符串么,為什么還要在寫(xiě)一個(gè)NSURL呢,主要是因?yàn)榫W(wǎng)站地址的字符串都比較復(fù)雜,包括很多請(qǐng)求參數(shù),這樣在請(qǐng)求過(guò)程中需要解析出來(lái)每個(gè)部門(mén),所以封裝一個(gè)NSURL,操作很方便2015-12-12