7
AVMutableComposition을 사용하여 오디오 및 비디오 파일을 믹싱하고 있습니다. 아래 코드는 내 코드입니다.비디오 길이가 오디오 길이보다 길 경우 오디오 트랙을 반복하는 방법
enter code here
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];
NSString *audio_inputFilePath = [bundleDirectory stringByAppendingPathComponent:@"xyz.mp3"];//audio of 35 seconds
NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];//video of 60 seconds
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
enter code here
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
enter code here
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
enter code here
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
문제 내 오디오 파일의 길이가 비디오 재생 시간보다 짧습니다. 그래서 내가 원하는 것은 비디오가 끝날 때까지 오디오 파일을 반복하는 것입니다. 내 비디오는 60 초, 오디오는 35 초이므로 오디오는 25 초 동안 반복되어야합니다.
어떻게 이런 일을 할 수 있는지 알려주세요.
작동하지 않습니다. 당신의 논리는 정확하지만 여전히 오디오는 35 초 후에 멈춘다 – coder1010
나는 b_composition으로 이미 시도했다. 네 논리를 사용 했어. 같은 줄이 아니야. 믹스 콤포지션에 두 개의 오디오 트랙을 추가 할 수 있습니까? – coder1010
내 대답을 다시 읽고 다시 시도하십시오. –