在iOS中截取和分割音视频的代码示例
目录
- 核心思路
- 视频截取示例(Objective-C)
- 使用方法
- 音频截取示例(Objective-C)
- 使用方法
- 注意事项
- 扩展建议
- 总结
核心思路
截取或分割音视频的核心步骤如下:
- 加载原始音视频文件(
AVURLAsset) - 设置时间范围(
CMTimeRange)指定要截取的起始时间与持续时间 - 创建导出会话(
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. 创建导出会话
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(startCMTimewww.devze.com, 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 durationChttp://www.devze.comMTime = CMTimeMakeWithSeconds(duration, 600);
CMTimeRange timeRange = CMTimeRangeMake(startCMTime, durationCMTime);
exportSession.timeRange = timeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSL编程客栈og(@"音频截取成功: %@", 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 将秒数转换为 CMTime |
| 输出路径 | 使用 NSTemporaryDirectory() 可避免存储问题 |
| 输出格式 | 视频推荐 .mp4,音频推荐 .m4a 或 .caf |
| 导出性能 | 使用 AVAssetExportPresetLowQuality 可提升处理速度 |
| 错误处理 | 检查 exportSession.status 和 exportSession.error |
扩展建议
- 多片段拼接:可结合
AVMutableComposition实现多段裁剪后的内容拼接。 - 后台导出:大文件建议在后台线程执行,避免阻塞主线程。
- 第三方库:如需更复杂剪辑功能,可使用 FFmpeg-IOS 或 GPUImage。
总结
通过 AVAssetExportSession 的 timeRange 属性,你可以轻松地从编程客栈音视频文件中截取任意时间段的内容。这个方法既适用于音频也适用于视频,具有良好的兼容性和性能表现,是 iOS 音视频处理中的基础技能之一。
以上就是在iOS中截取和分割音视频的代码示例的详细内容,更多关于iOS截取和分割音视频的资料请关注编程客栈(www.devze.com)其它相关文章!
加载中,请稍侯......
精彩评论