gpt4 book ai didi

iphone - AVAssetExportSession 不导出音轨

转载 作者:行者123 更新时间:2023-12-03 18:56:40 27 4
gpt4 key购买 nike

我有一个应用程序,允许附加多个视频资源并向合成添加一个或多个音轨。一切似乎都有效,我可以使用 AVPlayer 播放生成的作品(尽管音频级别似乎很低)。将乐曲导出到文件后,音轨丢失。

我的代码主要基于 WWDC10 session 中的 AVEditDemo 示例代码。我已经根据 AVEditDemo 代码仔细检查了我的代码,但找不到可能存在的问题。我也检查过论坛,但没有太多 AVFoundation 相关的帖子/解决方案。

非常欢迎任何帮助。干杯,

让-皮埃尔

<小时/>

使用额外音轨构建作品的方法

注释:

compositionArray:包含构建合成的资源。
AssetView:包含 AVURLAsset 的对象。

- (AVMutableComposition *)buildCompositionObjects
{
// no assets available, return nil
if ([compositionArray count] < 1)
{
return nil;
}

// get the asset video size
AssetView * view = [compositionArray objectAtIndex:0];
AVURLAsset * asset = view.asset;

CGSize videoSize = [asset naturalSize];

// create new composition
AVMutableComposition * cmp = [AVMutableComposition composition];

// set the size
cmp.naturalSize = videoSize;

// build composition
[self buildComposition:cmp];

// add any extra audio track
[self addAudioTrackToComposition:cmp];

// return the new composition
return cmp;
}

构建基础组合的方法

- (void) buildComposition:(AVMutableComposition *)cmp
{
// set the start time of contiguous tracks
CMTime nextClipStartTime = kCMTimeZero;

// clear the composition
[cmp removeTimeRange:CMTimeRangeMake(CMTimeMake(0, 600), cmp.duration)];

// add audio and video tracks
AVMutableCompositionTrack *compositionVideoTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// loop through all available assets
for (AssetView * view in compositionArray)
{
AVURLAsset *asset = view.asset;

CMTimeRange timeRangeInAsset;
timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [asset duration]);

AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:timeRangeInAsset ofTrack:clipVideoTrack atTime:nextClipStartTime error:nil];

// make sure there is an audio track. Had to do this becaaause of this missing audio track issue. Crashes if check is not done (out of bounds).
if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0)
{
AVAssetTrack *clipAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:nil];
}

// adjust next asset start
nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
}
}

添加附加音轨的方法

- (void)addAudioTrackToComposition:(AVMutableComposition *)cmp
{
// no audio track, return
if ([audioTracks count] < 1)
{
return;
}

// base track ID for additional audio tracks
long baseTrackID = 100;

for (AVURLAsset * audioAsset in audioTracks)
{
// make sure the audio track fits in the composition
CMTimeRange commentaryTimeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

if (CMTIME_COMPARE_INLINE(CMTimeRangeGetEnd(commentaryTimeRange), >, [cmp duration]))
{
commentaryTimeRange.duration = CMTimeSubtract([cmp duration], commentaryTimeRange.start);
}

// Add the audio track.
AVMutableCompositionTrack *compositionCommentaryTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:baseTrackID++];

[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, commentaryTimeRange.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:commentaryTimeRange.start error:nil];
}

}

导出组合物的方法

- (void) save
{

NSString * eventFolder = [NSString stringWithFormat:@"%@/%@-%@",
DOCUMENTS_FOLDER,
event.title,
[StringUtils stringForDate:event.timeStamp]];


NSString * exportVideoPath = [NSString stringWithFormat:@"%@/Edits/%@.MOV", eventFolder, [StringUtils stringForDate:[NSDate date]]];

video.path = exportVideoPath;

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

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
{
NSLog (@"FAIL");
[self performSelectorOnMainThread:@selector (doPostExportFailed:)
withObject:nil
waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCompleted:
{
NSLog (@"SUCCESS");
[self performSelectorOnMainThread:@selector (doPostExportSuccess:)
withObject:nil
waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCancelled:
{
NSLog (@"CANCELED");
[self performSelectorOnMainThread:@selector (doPostExportCancelled:)
withObject:nil
waitUntilDone:NO];
break;
}
};
}];

}

最佳答案

没有得到任何回复。我通过添加一行代码就可以工作:

 exportSession.shouldOptimizeForNetworkUse = YES;

之前

[exportSession exportAsynchronouslyWithCompletionHandler:^

我不确定为什么这可以解决问题。因为这似乎与问题完全无关,但我能够毫无问题地导出十几个包含最多 5 个额外音轨的作品。

我希望这可以帮助其他几天来一直摸不着头脑的人。

干杯

关于iphone - AVAssetExportSession 不导出音轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5307285/

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