在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.status 和 exportSession.error |
擴(kuò)展建議
- 多片段拼接:可結(jié)合
AVMutableComposition實(shí)現(xiàn)多段裁剪后的內(nèi)容拼接。 - 后臺(tái)導(dǎo)出:大文件建議在后臺(tái)線(xiàn)程執(zhí)行,避免阻塞主線(xiàn)程。
- 第三方庫(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)文章
iOS中sqlite數(shù)據(jù)庫(kù)的原生用法
這篇文章主要為大家詳細(xì)介紹了iOS中sqlite數(shù)據(jù)庫(kù)的原生用法,sqlite數(shù)據(jù)庫(kù)相信各位早已耳聞,非常輕巧的一個(gè)數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)僅一個(gè)文件,即建即用,感興趣的小伙伴們可以參考一下32016-05-05
iOS10 適配-Xcode8問(wèn)題總結(jié)及解決方案
這篇文章主要介紹了iOS10 適配-Xcode8問(wèn)題總結(jié)的相關(guān)資料,這里整理了遇到的幾種問(wèn)題,并給出解決方案,需要的朋友可以參考下2016-11-11
IOS 數(shù)據(jù)存儲(chǔ)詳解及實(shí)例代碼
這篇文章主要介紹了IOS 數(shù)據(jù)存儲(chǔ)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
IOS ObjectC與javascript交互詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS OC與js交互詳解及實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果
這篇文章主要為大家詳細(xì)介紹了iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
iOS開(kāi)發(fā)中使用Quartz2D繪制上下文棧和矩陣的方法
這篇文章主要介紹了iOS開(kāi)發(fā)中使用Quartz2D繪制上下文棧和矩陣的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11

