gpt4 book ai didi

ios - iOS AVFoundation导出 session 缺少音频

转载 作者:行者123 更新时间:2023-12-03 01:54:03 26 4
gpt4 key购买 nike

我正在使用iOS AVFoundation框架,并且能够成功合并视频轨道,图像叠加层和文本叠加层。但是,我的输出文件不能使原始源视频的音频保持完整。

如何确保我的一个视频中的音频源与我创建的新视频保持一致?

编辑

*使用此代码有一个很好的示例,说明如何完成创建视频(带有原始音频)的操作。对于我来说,在使用AVFoundation处理视频时需要单独包含音轨并不明显。希望这对其他人有帮助。

    AVAssetTrack *videoTrack = nil;
AVAssetTrack *audioTrack = nil;
CMTime insertionPoint = kCMTimeZero;

if([[url tracksWithMediaType:AVMediaTypeVideo] count] != 0) {
videoTrack = [url tracksWithMediaType:AVMediaTypeVideo][0];
}

if([[url tracksWithMediaType:AVMediaTypeAudio] count] != 0) {
audioTrack = [url tracksWithMediaType:AVMediaTypeAudio][0];
}

// Insert the video and audio tracks from AVAsset
if (videoTrack != nil) {
AVMutableCompositionTrack *compositionVideoTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration]) ofTrack:videoTrack atTime:insertionPoint error:&error];
}
if (audioTrack != nil) {
AVMutableCompositionTrack *compositionAudioTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration]) ofTrack:audioTrack atTime:insertionPoint error:&error];
}

最佳答案

这是解决此问题的完整代码,其中包含两个视频及其音频:-

AVURLAsset* video1 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path1] options:nil];

AVURLAsset* video2 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path2] options:nil];

if (video1 !=nil && video2!=nil) {

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
// 2 - Video track

AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *firstTrackAudio = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];

[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration)
ofTrack:[[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2.duration)
ofTrack:[[video2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:video1.duration error:nil];

//它有一个音轨
    if ([[video1 tracksWithMediaType:AVMediaTypeAudio] count] > 0)
{
AVAssetTrack *clipAudioTrack = [[video1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[firstTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
}

//它有一个音轨
    if ([[video2 tracksWithMediaType:AVMediaTypeAudio] count] > 0)
{
AVAssetTrack *clipAudioTrack = [[video2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[firstTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2.duration) ofTrack:clipAudioTrack atTime:video1.duration error:nil];
}

//导出 session
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];

//Creates the path to export to - Saving to temporary directory
NSString* filename = [NSString stringWithFormat:@"Video_%d.mov",arc4random() % 1000];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

//Checks if there is already a file at the output URL.
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"Removing item at path: %@", path);
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

exporter.outputURL = [NSURL fileURLWithPath:path];
//Set the output file type
exporter.outputFileType = AVFileTypeQuickTimeMovie;


path3=path;
[arr_StoredDocumentoryUrls addObject:path3];

//Exports!
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch (exporter.status) {
case AVAssetExportSessionStatusCompleted:{
NSLog(@"Export Complete");

break;
}
case AVAssetExportSessionStatusFailed:
NSLog(@"Export Error: %@", [exporter.error description]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export Cancelled");
break;
default:
break;
}
}];

}

关于ios - iOS AVFoundation导出 session 缺少音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com