AVAssetExportSession剪切视频转换问题
创始人
2024-11-12 20:00:33
0

使用AVMutableComposition、AVMutableVideoComposition和AVMutableAudioMix创建新的AVAsset,并将其导出为视频文件。下面是示例代码:

AVURLAsset *originalAsset = [[AVURLAsset alloc] initWithURL:originalURL options:nil];
AVMutableComposition *composition = [AVMutableComposition composition];

AVAssetTrack *videoTrack = [[originalAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, originalAsset.duration)
                            ofTrack:videoTrack
                             atTime:kCMTimeZero
                              error:nil];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(640, 640);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);

AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
[layerInstruction setTransform:CGAffineTransformMakeTranslation(64, 64) atTime:kCMTimeZero];

instruction.layerInstructions = @[layerInstruction];
videoComposition.instructions = @[instruction];
exportSession.videoComposition = videoComposition;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = outputFileURL;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    // Handle export completion or errors
}];

该代码将从原始视频文件中提取视频轨道,并使用AVMutableComposition创建新的AVAsset。AVMutableVideoComposition和AVMutableAudioMix对象用于对导出视频进行剪切和转换。最终,AVAssetExportSession用于将新的AVAsset导出为MPEG-4文件。

相关内容

热门资讯

透视真的!sohoo辅助(透视... 透视真的!sohoo辅助(透视)总是真的是有挂(详细辅助力荐教程);辅助器中分为三种模型:软件透明挂...
实测交流!hhpoker作弊码... 实测交流!hhpoker作弊码,pokemmo辅助脚本,微扑克教程(有挂辅助);最新版2024是一款...
透视挂!hh poker软件(... 透视挂!hh poker软件(透视)真是存在有挂(详细辅助必备教程)所有人都在同一条线上,像星星一样...
一分钟了解!wepoker怎么... 1、一分钟了解!wepoker怎么挂底牌,德州hhpoker脚本,透视教程(有挂透视);详细教程。2...
透视存在!菠萝辅助器免费版的功... 透视存在!菠萝辅助器免费版的功能介绍(透视)果然有挂(详细辅助2025新版);一、软件透明挂的定义与...
实测分享!wepoker怎么开... 实测分享!wepoker怎么开辅助,wpk作弊,wepoke教程(有挂攻略);实战中需综合运用上述技...
透视计算!wpk模拟器多开(透... 透视计算!wpk模拟器多开(透视)一直是有挂(详细辅助曝光教程)在进入辅助挂后,参与本局比赛的八名玩...
推荐几款新版!佛手在线是不是有... 推荐几款新版!佛手在线是不是有挂,aapoker透视插件,新2025版(有挂教程);支持多人共享记分...
透视存在!wepoker私人局... 透视存在!wepoker私人局俱乐部辅助(透视)确实是真的有挂(详细辅助规律教程)1、实时开挂更新:...
重大通报!wepoker私人局... 重大通报!wepoker私人局透视教程,wpk刷入池率脚本,解说技巧(有挂透明)这是由厦门游乐互动科...