gpt4 book ai didi

iOS - 混合两个音频文件

转载 作者:行者123 更新时间:2023-11-29 11:45:53 51 4
gpt4 key购买 nike

我正在尝试将 2 个音轨混合为一个(视频的声音和一些音乐)。

借助 AVFoundation,我可以使用这种方法将两个轨道设置为一个视频文件:

https://stackoverflow.com/a/16316985/5120292

这是结果输出文件,在我的电脑上用 ffmpeg 检查:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'IMG_3789.mp4':
Metadata:
major_brand : mp42
minor_version : 1
compatible_brands: mp41mp42isom
creation_time : 2017-05-11T15:46:52.000000Z
Duration: 00:00:22.63, start: 0.000000, bitrate: 38210 kb/s
Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1696x848, 37782 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2017-05-11T15:46:52.000000Z
handler_name : Core Media Video
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 250 kb/s (default)
Metadata:
creation_time : 2017-05-11T15:46:52.000000Z
handler_name : Core Media Audio
Stream #0:2(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 189 kb/s (default)
Metadata:
creation_time : 2017-05-11T15:46:52.000000Z
handler_name : Core Media Audio

您可以看到该文件包含两个音轨。我需要将两个轨道合并为一个轨道,因为有些播放器只播放第一首轨道。

是否可以将它们混合成一个轨道而不是 2 个单独的轨道?

提前致谢!任何帮助将不胜感激

最佳答案

此代码将音轨合并为一个 - 我认为这要归功于 AVMutableComposition(而不是 AVAssetExportSession):

AVAssetTrack *videoTrack = ...;
AVAssetTrack *audioTrack1 = ...;
AVAssetTrack *audioTrack2 = ...;

AVMutableComposition *composition = [AVMutableComposition composition];

for (AVAssetTrack* inputTrack in @[videoTrack, audioTrack1, audioTrack2]) {
AVMutableCompositionTrack* outputTrack;
outputTrack = [composition addMutableTrackWithMediaType:inputTrack.mediaType preferredTrackID:kCMPersistentTrackID_Invalid];

NSError* error;
if (![outputTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, inputTrack.timeRange.duration) ofTrack:inputTrack atTime:kCMTimeZero error:&error]) {
NSLog(@"insertTimeRange error: %@", error);
}
}

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

NSURL *outputURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0] URLByAppendingPathComponent:@"output.mp4"];
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = outputURL;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"success %@", outputURL);
} else {
NSLog(@"failed %li", (long)exportSession.status);
}
}];

关于iOS - 混合两个音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43921686/

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