5

I have got two audio tracks on me that I combine with one another like this:

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack setPreferredVolume:1.0];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:originalContentURL options:nil];
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack1 setPreferredVolume:0.01];
NSString *soundOne1  =[[NSBundle mainBundle]pathForResource:@"jingle1" ofType:@"m4a"];
NSURL *url1 = [NSURL fileURLWithPath:soundOne1];
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];

As you can see, the first AVAsset is the base length for the second track, meaning that if I have a long second track it will be cut, which is just the way that I want.

However, I need to be able to loop the second track so that if the first one is too long, the second one goes on and on. I later have to save the resulting track on disc which is an important factor as well.

After a research I've made I found out there's no convenient way to actually loop a track in iOS. One of the ways out would be inserting the second track at AVMutableComposition multiple times over and over but that sounds quite strange to me. Any ideas based on the topic would be really useful.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120

4 Answers4

13

I think it should work:

CMTime videoDuration = avAsset.duration;
if(CMTimeCompare(videoDuration, audioAsset.duration) == -1){
    [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];
}else if(CMTimeCompare(videoDuration, audioAsset.duration) == 1){
     CMTime currentTime = kCMTimeZero;
     while(YES){
           CMTime audioDuration = audioAsset.duration;
           CMTime totalDuration = CMTimeAdd(currentTime,audioDuration);
           if(CMTimeCompare(totalDuration, videoDuration)==1){
              audioDuration = CMTimeSubtract(totalDuration,videoDuration);

           }
           [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioDuration) ofTrack:clipAudioTrack1 atTime:currentTime error:nil];
           currentTime = CMTimeAdd(currentTime, audioDuration);
           if(CMTimeCompare(currentTime, videoDuration) == 1 || CMTimeCompare(currentTime, videoDuration) == 0){
               break;
           }
     }
}
Gaurav Singh
  • 1,897
  • 14
  • 22
  • Worked like charm, great! – Sergey Grischyov Nov 26 '13 at 09:51
  • @GauravSingh I tried the code above. But get the error from `AVAssetExportSession` **The video could not be composed** error code: -11841. Any idea why it would fail? If the video duration is shorter than the mp3 it's fine. – resting Mar 05 '14 at 08:18
  • @resting please check your code and try logging the compositionAudiotrack duration in each loop iteration to see if everything is going fine or not – Gaurav Singh Mar 05 '14 at 08:40
  • This's good codes and thank author a lot. But in some situation, such as: the length of video is larger than audio duration, and the audio will loop play during video playing. I think this code: "audioDuration = CMTimeSubtract(totalDuration, timeDuringMerge);" should be replaced by "audioDuration = CMTimeSubtract(timeDuringMerge, currentTime);" – Gallery Apr 15 '16 at 09:40
  • I followed and developed same logic to repeat a video track for base duration of audio track, i.e audio track is longer than video so I need to repeat video for whole audio duration. My first range of video is perfectly added but next ranges/chunks of video are black. Not sure why, the logic is simple to add video track at current time and increase current time to video duration. – Zahid Usman Cheema Mar 02 '23 at 19:29
8

On the Gaurav response you should change this line:
audioDuration = CMTimeSubtract(totalDuration,videoDuration);

to:
audioDuration = CMTimeSubtract(videoDuration,currentTime);

Otherwise the audiotrack will be longer than the video ( that will be just black )

SPatel
  • 4,768
  • 4
  • 32
  • 51
MindTrip
  • 283
  • 6
  • 12
2

In swift with repeat whiel Loop.

fix video length isuue

let audioLength = CMTime(value: 100, timescale: 1)
let videoLength = CMTime(value: 220, timescale: 1)

func getAudioRepeatInfo(audioLength:CMTime, videoLength:CMTime) -> [(at:CMTime, duration:CMTime)] {
    var at = [(at:CMTime, duration:CMTime)](), start = CMTime.zero
    repeat {
        let info = (at:start, duration: min(CMTimeSubtract(videoLength, start), audioLength) )
        at.append(info)
        start = CMTimeAdd(start, info.duration)
    } while start < videoLength
    return at
}

var ranges =  getAudioRepeatInfo(audioLength: audioLength, videoLength: videoLength)

Playground Output:

[(at: __C.CMTime(value: 0, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0), duration: __C.CMTime(value: 100, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)),
 (at: __C.CMTime(value: 100, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0), duration: __C.CMTime(value: 100, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)),
 (at: __C.CMTime(value: 200, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0), duration: __C.CMTime(value: 20, timescale: 1, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0))]

Use:

for info in repeatInfo {
    try compositionAudioTrack1?.insertTimeRange(.init(start: .zero, duration: info.duration), of: assetTrack, at: info.at)
}
SPatel
  • 4,768
  • 4
  • 32
  • 51
1

Gaurav answer in Swift 5.5 (his solution is correct and working perfectly).

  let videoDuration = avAsset.duration

  if (CMTimeCompare(videoDuration, audioAsset.duration) == -1) {
    // Handle insertTimeRange
  } else if (CMTimeCompare(videoDuration, audioAsset.duration) == 1) {

    var currentTime = CMTime.zero

    while(true) {
      let audioDuration = audioAsset.duration
      let totalDuration = CMTimeAdd(currentTime, audioDuration)

      if (CMTimeCompare(totalDuration, videoDuration) == 1) {
        audioDuration = CMTimeSubtract(videoDuration, currentTime)
      }
  
      // Handle insertTimeRange
  
      currentTime = CMTimeAdd(currentTime, audioDuration)
      if(CMTimeCompare(currentTime, videoDuration) == 1 || CMTimeCompare(currentTime, videoDuration) == 0){
        break
      }
    }
  }
SwiftyLifestyle
  • 416
  • 5
  • 17